The Movie Database Support

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 replies (on page 1 of 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.

Can't find a movie or TV show? Login to create it.

Global

s focus the search bar
p open profile menu
esc close an open window
? open keyboard shortcut window

On media pages

b go back (or to parent when applicable)
e go to edit page

On TV season pages

(right arrow) go to next season
(left arrow) go to previous season

On TV episode pages

(right arrow) go to next episode
(left arrow) go to previous episode

On all image pages

a open add image window

On all edit pages

t open translation selector
ctrl+ s submit form

On discussion pages

n create new discussion
w toggle watching status
p toggle public/private
c toggle close/open
a open activity
r reply to discussion
l go to last reply
ctrl+ enter submit your message
(right arrow) next page
(left arrow) previous page

Settings

Want to rate or add this item to a list?

Login