我试图解析从电影数据库返回的JSON数据。我收到一条错误消息,告诉我我收到以下错误消息: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:68) at discover.js:5I am accessing individual movie information using the following XMLHttpRequest:movieReq.open('GET',' https: //api.themoviedb.org/3/movie/299537?api_key = ',false);我还有其他XMLHttpRequests可以返回JSON数据。这些请求正在使用API的发现功能。这是产生问题的功能function loadIMDBDetails(mO, i) { movieReq = new XMLHttpRequest(); movieReq.onreadystatechange = function () { var parsedObj = JSON.parse(movieReq.responseText); mO.imdbId = parsedObj['imdb_id']; }; movieReq.open('GET', 'https://api.themoviedb.org/3/movie/299537?api_key=34f8307d9addabf7924eab7f22cabb23', false); movieReq.send();}When using console.log to return the responseText, this is the result:
2 回答
慕哥6287543
TA贡献1831条经验 获得超10个赞
事实证明,我已经忘记为onreadystatechange事件添加检查了。我添加了以下检查,并在if语句中运行了json解析,这解决了我的问题!
movieReq.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var parsedObj = JSON.parse(movieReq.responseText);
mO.imdbId = parsedObj['imdb_id'];
}
};
添加回答
举报
0/150
提交
取消