Soporte de The Movie Database

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

3 respuestas (en la página 1 de 1)

Jump to last post

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?

{"adult":false,"backdrop_path":"/w2PMyoyLU22YvrGK3smVM9fW1jj.jpg","belongs_to_collection":null,"budget":152000000,"genres":[{"id":28,"name":"Action"},{"id":12,"name":"Adventure"},{"id":878,"name":"Science Fiction"}],"homepage":"https://www.marvel.com/movies/captain-marvel","id":299537,"imdb_id":"tt4154664","original_language":"en","original_title":"Captain Marvel","overview":"The story follows Carol Danvers as she becomes one of the universe’s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","popularity":419.096,"poster_path":"/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","production_companies":[{"id":420,"logo_path":"/hUzeosd33nzE5MCNsZxCGEKTXaQ.png","name":"Marvel Studios","origin_country":"US"}],"production_countries":[{"iso_3166_1":"US","name":"United States of America"}],"release_date":"2019-03-06","revenue":910298835,"runtime":124,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"Higher. Further. Faster.","title":"Captain Marvel","video":false,"vote_average":7.3,"vote_count":2885}

This is the returned JSON. I found the error is caused by my missing if statement. I was missing the following if statement:

if (this.readyState == 4 && this.status == 200) 

Adding this statement fixed my issue!

Happy to hear you sorted this out.

¿No encuentras una película o serie? Inicia sesión para crearla:

Global

s centrar la barra de búsqueda
p abrir menú de perfil
esc cierra una ventana abierta
? abrir la ventana de atajos del teclado

En las páginas multimedia

b retrocede (o a padre cuando sea aplicable)
e ir a la página de edición

En las páginas de temporada de televisión

(flecha derecha) ir a la temporada siguiente
(flecha izquierda) ir a la temporada anterior

En las páginas de episodio de televisión

(flecha derecha) ir al episodio siguiente
(flecha izquierda) ir al episodio anterior

En todas las páginas de imágenes

a abrir la ventana de añadir imagen

En todas las páginas de edición

t abrir la sección de traducción
ctrl+ s enviar formulario

En las páginas de debate

n crear nuevo debate
w cambiar el estado de visualización
p cambiar público/privado
c cambiar cerrar/abrir
a abrir actividad
r responder al debate
l ir a la última respuesta
ctrl+ enter enviar tu mensaje
(flecha derecha) página siguiente
(flecha izquierda) página anterior

Configuraciones

¿Quieres puntuar o añadir este elemento a una lista?

Iniciar sesión