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

时间:2015-11-02 01:27:46

标签: javascript json tvos apple-tv tvjs

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

    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 个答案:

答案 0 :(得分:0)

您可以使用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();
    };
相关问题