SyntaxError:JSON解析错误:使用TVJS框架的意外EOF

问题描述:

我正在为Apple TV构建TVML应用程序。当我运行此代码向远程服务器发出请求时,出现此错误:SyntaxError:JSON解析错误:意外的EOF。我试图从application.js文件运行代码并填充应用程序的视图。任何帮助表示赞赏。SyntaxError:JSON解析错误:使用TVJS框架的意外EOF

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" 
     } 
    ] 
} 
+1

你可以发布JSON输出或它的一个样本吗? – Griffith

+2

一旦你解决了无效的JSON,你将会遇到的下一个问题是你的'loadData'将会返回非undefined –

您可以使用Safari调试您的应用程序。 一旦你的应用程序运行,选择“开发/模拟器/ {你的应用程序}”。 你的代码部分看起来不错,但检查xhr.responseText是什么,它可能会返回空。 你其他错误是,当你使用下面的“真”,你正在一个异步请求:

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(); 
    };