Hello, I am using a XMLHttpRequest to make a request to the server to get info about specific movies using a for loop. I am doing this to obtain the imdb id so that I can use another API to get the IMDb Ratings. I use a function to first retrieve all movies on the discover page, then use a for loop run a function which accesses each object in the array to set the IMDb Rating after a successful return of data from the server.
I am however getting this error:
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at XMLHttpRequest.movieReq.onreadystatechange (discover.js:34)
at loadIMDBDetails (discover.js:38)
at MainFunc (discover.js:71)
at discover.js:5
This is the XMLHttpRequest initialization statement:
movieReq.open('GET', 'https://api.themoviedb.org/3/movie/' + mO.id + '?api_key=<my_api_key>, false);
I am aware that this is returning data synchronously and will change that once I have fixed the issue of the unexpected end of JSON input error.
Please assist, TA
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by Travis Bell
on March 28, 2019 at 11:53 AM
Hi @MarineXII,
It's pretty hard for me to help troubleshoot this, without seeing the JSON you're trying to parse. Can you get a copy of the body that's being returned by the API when your code trips on this error?
Reply by MarineXII
on March 28, 2019 at 12:34 PM
This is the returned JSON. I found the error is caused by my missing if statement. I was missing the following if statement:
Adding this statement fixed my issue!
Reply by Travis Bell
on April 3, 2019 at 2:27 PM
Happy to hear you sorted this out.