consuming API JSon calls through TVJS-tvOS

前端 未结 6 725
终归单人心
终归单人心 2021-02-06 10:32

I am trying to play with tvOS, and I have small question regarding handling json call. I have to get some data through an API, let\'s say for sake of test that I am calling this

6条回答
  •  遥遥无期
    2021-02-06 11:11

    I tested this one out on the tvOS - works like a charm with jQuery's syntax (basic tests pass):

    var $ = {};
    $.ajax = function(options) {
    
      var url = options.url;
      var type = options.type || 'GET';
      var headers = options.headers || {} ;
      var body = options.data || null;
      var timeout = options.timeout || null;
      var success = options.success || function(err, data) {
        console.log("options.success was missing for this request");
      };
      var contentType = options.contentType || 'application/json';
      var error = options.error || function(err, data) {
        console.log("options.error was missing for this request");
      };
    
      if (!url) {
        throw 'loadURL requires a url argument';
      }
    
      var xhr = new XMLHttpRequest();
      xhr.responseType = 'json';
      xhr.timeout = timeout;
      xhr.onreadystatechange = function() {
        try {
          if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                if (xhr.responseType === 'json') {
                    success(null, xhr.response);
                } else {
                    success(null, JSON.parse(xhr.responseText));
                }
            } else {
              success(new Error("Error [" + xhr.status + "] making http request: " + url));
            }
          }
        } catch (err) {
          console.error('Aborting request ' + url + '. Error: ' + err);
          xhr.abort();
          error(new Error("Error making request to: " + url + " error: " + err));
        }
      };
    
      xhr.open(type, url, true);
    
      xhr.setRequestHeader("Content-Type", contentType);
      xhr.setRequestHeader("Accept", 'application/json, text/javascript, */*');
    
      Object.keys(headers).forEach(function(key) {
        xhr.setRequestHeader(key, headers[key]);
      });
    
      if(!body) {
        xhr.send();
        } else {
            xhr.send(body);
        }
    
      return xhr;
    }
    

    Example queries working on Apple TV:

    var testPut = function(){
    
        $.ajax({
            type: 'PUT',
            url: url,
            success: successFunc,
            error: errFunc,
            dataType: 'json',
            contentType: 'application/json',
            data: data2
        });
    }
    var testGet = function(){
        $.ajax({
            dataType: 'json',
            url: url,
            success: successFunc,
            error: errFunc,
            timeout: 2000
        });
    
    }
    
    var getLarge = function(){
        $.ajax({
            dataType: 'json',
            url: url,
            success: successFunc,
            error: errFunc,
            timeout: 2000
        });
    }
    

提交回复
热议问题