1

我的浏览器中有一个有效的 AJAX 请求。现在我需要在 TVML 项目中对我的 JSON 数据使用 GET 请求。怎么做比较好?

我正在尝试 XMLHttpRequest,但它不起作用或者我做错了什么?

function performRequest(type, route, data) {
        return $.ajax({
            type: type,
            dataType: 'json',
            url: url + route,
            data: data
        });
    }

    function getChannels() {
        log(' > get channels');
        return performRequest('GET', 'channel/list', {
            id: browserId
        }).then(function (response) {
            response.data.forEach(function (channel) {
                channels[channel.id] = channel;
            });
        });
    }
4

2 回答 2

0

在我看来,您的代码使用的是 jquery,而不是 XMLHTTPRequest。就个人而言,我没有成功使用 TVJS 运行 jquery,因为它对 TVJS 不满足的 Document 类做出了一些假设。我没有详细分析这个问题,也许你可以找到一些解决方法。

如果您想使用本机 XMLHTTPRequest 下载 JSON 资源,请尝试以下操作:

/**
 * Downloads a JSON resource
 *
 * @param {Object}      options                 The options parameter.
 * @param {string}      options.resourcePath    Path to the JSON
 * @param {Function}    options.success         On success, will be called with the resulting JSON object
 * @param {Function}    [options.error]         Error callback
 */
getJSON(options) {

    var resourceXHR = new XMLHttpRequest();
    resourceXHR.responseType = "json";
    resourceXHR.addEventListener("load", (function () {
        options.success(resourceXHR.response);
    }));
    resourceXHR.addEventListener("error", (function () {
        options.error({status: resourceXHR.status, statusText: resourceXHR.statusText});
    }));
    resourceXHR.open("GET", options.resourcePath, true);
    resourceXHR.send();
}
于 2016-06-06T13:40:46.223 回答
-1

您是否尝试过atvjs框架来构建 TVML 应用程序?它可以让您在没有太多噪音的情况下构建和快速原型化应用程序,从而抽象出传统 TVML 应用程序的底层麻烦和复杂性。

atvjs中,ajax 是作为一个 Promise 实现的,您可以执行以下操作:

ATV.Ajax.get('http://your_url')
        .then((response) => /* do something with response */)
        .catch((error) => /* handle errors */ );
于 2017-06-29T18:02:22.387 回答