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
¿No encuentras una película o serie? Inicia sesión para crearla:
¿Quieres puntuar o añadir este elemento a una lista?
¿No eres miembro?
Contestado por Travis Bell
el 28 de marzo de 2019 a las 11:53
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?
Contestado por MarineXII
el 28 de marzo de 2019 a las 12:34
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!
Contestado por Travis Bell
el 3 de abril de 2019 a las 14:27
Happy to hear you sorted this out.