我正在为Apple TV构建一个TVML应用程序。当我运行这段代码向远程服务器发出请求时,我会得到以下错误: SyntaxError: JSON :意外EOF。我正在尝试从application.js文件中运行代码并填充应用程序inital视图。任何帮助都是非常感谢的。
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"
}
]
}发布于 2015-11-09 08:42:35
您可以使用Safari调试应用程序。一旦你的应用程序运行,选择“发展/模拟器/{你的应用程序}”。代码的这一部分看起来没问题,但是检查一下xhr.responseText是什么,它可能会返回空的。另一个错误是,当在下面使用"true“时,您正在发出异步请求:
xhr.open("GET", url, 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();
};https://stackoverflow.com/questions/33469351
复制相似问题