1

I have a working AJAX request in my browser. Now I need to use GET request for my JSON data in TVML project. How it is better to do it?

I was trying XMLHttpRequest, but it doesn't work or maybe I did something wrong?

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

2 Answers 2

0

Your code seems to me to use jquery, not XMLHTTPRequest. Personally I did not succeed to run jquery with TVJS as it makes some suppositions about the Document class that TVJS does not satisfy. I did not analyze this issue in details, maybe you can find some workaround.

If you want to download a JSON resource with the native XMLHTTPRequest, try something like this:

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

Have you tried atvjs framework for building TVML apps? It lets you build and quickly prototype apps without much noise, abstracting the underlying hassles and complexities of a conventional TVML app.

In atvjs, ajax is implemented as a promise and you could do something like below:

ATV.Ajax.get('http://your_url')
        .then((response) => /* do something with response */)
        .catch((error) => /* handle errors */ );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.