دعم الموقع

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 ردود (على هذه الصفحة 1 من 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.

لم تجد الفلم أو المسلسل ؟ سجل دخولك و انشئها

عام

s ركز شريط البحث
p افتح قائمة الملف الشخصي
esc اغلق النافذة المفتوحة
? افتح نافذة اختصارات لوحة المفاتيح

على كافة صفحات الوسائط

b ارجع للخلف (او للصفحة الام عند التطبيق)
e انتقل لصفحة التعديل

على كافة صفحات موسم المسلسل

(السهم الايمن) انتقل للموسم التالي
(السهم الايسر) انتقل للموسم السابق

على كافة صفحات حلقة المسلسل

(السهم الايمن) انتقل للحلقة التالية
(السهم الايسر) انتقل للحلقة السابقة

على كافة صفحات الصور

a افتح صفحة اضافة الصورة

على كافة صفحات التعديل

t افتح محدد الترجمة
ctrl+ s ارسال النموذج

على صفحات المناقشة

n انشى نقاش جديد
w تبديل حالة المتابعة
p تبديل عام / خاص
c تبديل اغلاق / فتح
a افتح الانشطة
r رد على النقاش
l انتقل لأخر رد
ctrl+ enter أرسل رسالتك
(السهم الايمن) الصفحة التالية
(السهم الايسر) الصفحة السابقة

الاعدادات

هل تريد تقييم او اضافة هذا العنصر للقائمة؟

تسجيل الدخول