Javascript SyntaxError:JSON解析错误:使用TVJS框架的意外EOF
我正在为Apple TV构建一个TVML应用程序。当我运行此代码向远程服务器发出请求时,我得到以下错误:SyntaxError:JSON解析错误:意外的EOF。我试图从application.js文件运行代码,并填充applications inital视图。感谢您的帮助Javascript SyntaxError:JSON解析错误:使用TVJS框架的意外EOF,javascript,json,tvos,apple-tv,tvjs,Javascript,Json,Tvos,Apple Tv,Tvjs,我正在为Apple TV构建一个TVML应用程序。当我运行此代码向远程服务器发出请求时,我得到以下错误:SyntaxError:JSON解析错误:意外的EOF。我试图从application.js文件运行代码,并填充applications inital视图。感谢您的帮助 loadData("https:/xxx.xxx.net") function loadData(url) { var xhr; var jsonData; xhr = n
loadData("https:/xxx.xxx.net")
function loadData(url) {
var xhr;
var jsonData;
xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.onreadystatechange = function() {
if (xhr.status == 200) {
jsonData = JSON.parse(xhr.responseText);
console.log(jsonData);
};
};
xhr.open("GET", url, true);
xhr.send();
if (jsonData != undefined) { return jsonData }
};
其他设备(如Roku)使用相同的api并正确运行
{
"playlists": [
{
"id": 8,
"title": "test",
"description": "new playlist test",
"cover_url": "http://598-1446309178.png"
},
{
"id": 9,
"title": "test1",
"description": "lives",
"cover_url": "http://754-1446309324.jpg"
},
{
"id": 10,
"title": "test2",
"description": "video games",
"cover_url": "http://6173-1446310649.jpg"
},
{
"id": 11,
"title": "test4",
"description": "little",
"cover_url": "http://dd6-1446312075.jpg"
}
]
}
您可以使用Safari调试应用程序。 应用程序运行后,选择“开发/模拟器/{your app}”。 代码的这一部分看起来不错,但检查xhr.responseText是什么,它可能会返回空。 您的另一个错误是,当您使用下面的“true”时,您正在发出异步请求: 您需要向函数提供回调并使用该回调。 我使用的是错误优先回调样式
function loadData(url, callback) {
var xhr;
var jsonData;
xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.onreadystatechange = function() {
if (xhr.status == 200) {
try{
jsonData = JSON.parse(xhr.responseText);
} catch(e) {
return callback('json parsing error');
}
callback(null, jsonData);
console.log(jsonData);
};
};
xhr.open("GET", url, true);
xhr.send();
};
您可以发布JSON输出或它的一个示例吗?一旦您解决了无效的JSON,下一个问题就是您的
loadData
将返回除未定义之外的任何内容
function loadData(url, callback) {
var xhr;
var jsonData;
xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.onreadystatechange = function() {
if (xhr.status == 200) {
try{
jsonData = JSON.parse(xhr.responseText);
} catch(e) {
return callback('json parsing error');
}
callback(null, jsonData);
console.log(jsonData);
};
};
xhr.open("GET", url, true);
xhr.send();
};