2015-09-13 14 views
6

के माध्यम से कॉल करता है मैं टीवीओएस के साथ खेलने की कोशिश कर रहा हूं, और मेरे पास जेसन कॉल को संभालने के बारे में छोटा सवाल है। मैं एक एपीआई के माध्यम से कुछ डेटा प्राप्त करने के लिए,उपभोक्ता एपीआई जेएसओएन टीवीजेएस-टीवीओएस

http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%223015%22&format=json 

मैं कुछ संशोधन

function getDocument(url) { 
    var templateXHR = new XMLHttpRequest(); 
    templateXHR.responseType = "json"; 
    templateXHR.open("GET", url, true); 
    templateXHR.send(); 
    return templateXHR; 
} 

के साथ इस समारोह का उपयोग करने की कोशिश की, लेकिन काम नहीं किया के परीक्षण की खातिर है कि मैं इस लिंक बोल रहा हूँ के लिए मान लीजिए बाहर। कोई संकेत या मदद?

यदि मुझे नोडजेएस का उपयोग करने की आवश्यकता है, तो मैं यह कैसे कर सकता हूं?

उत्तर

1

आप 'App.onLaunch'

App.onLaunch = function(options) { 
    var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%223015%22&format=json'; 
    var doc = getDocument(url); 
    console.log(doc); 
} 

https://mathiasbynens.be/notes/xhr-responsetype-json

0

को देख के लायक हो सकता है आप को संभालने के लिए एक्सएचआर वस्तु पर onreadystatechange घटना लागू करने की आवश्यकता में अपने फ़ंक्शन को कॉल किया प्रतिक्रिया:

templateXHR.onreadystatechange = function() { 
    var status; 
    var data; 
    if (templateXHR.readyState == 4) { //request finished and response is ready 
    status = templateXHR.status; 
    if (status == 200) { 
     data = JSON.parse(templateXHR.responseText); 
     // pass the data to a handler 
    } else { 
     // handle the error 
    } 
    } 
}; 
4

यह वह है जिसे मैं काम कर रहा हूं। यह कई मामलों में आदर्श नहीं है, लेकिन आपको कुछ शुरू करने के लिए दिखाता है।

function jsonRequest(options) { 

    var url = options.url; 
    var method = options.method || 'GET'; 
    var headers = options.headers || {} ; 
    var body = options.body || ''; 
    var callback = options.callback || function(err, data) { 
    console.error("options.callback was missing for this request"); 
    }; 

    if (!url) { 
    throw 'loadURL requires a url argument'; 
    } 

    var xhr = new XMLHttpRequest(); 
    xhr.responseType = 'json'; 
    xhr.onreadystatechange = function() { 
    try { 
     if (xhr.readyState === 4) { 
     if (xhr.status === 200) { 
      callback(null, JSON.parse(xhr.responseText)); 
     } else { 
      callback(new Error("Error [" + xhr.status + "] making http request: " + url)); 
     } 
     } 
    } catch (err) { 
     console.error('Aborting request ' + url + '. Error: ' + err); 
     xhr.abort(); 
     callback(new Error("Error making request to: " + url + " error: " + err)); 
    } 
    }; 

    xhr.open(method, url, true); 

    Object.keys(headers).forEach(function(key) { 
    xhr.setRequestHeader(key, headers[key]); 
    }); 

    xhr.send(); 

    return xhr; 
} 

और आप निम्न उदाहरण से कॉल करने की कर सकते हैं:

jsonRequest({ 
    url: 'https://api.github.com/users/staxmanade/repos', 
    callback: function(err, data) { 
    console.log(JSON.stringify(data[0], null, ' ')); 
    } 
}); 

आशा इस मदद करता है।

+0

क्या आपने टीवीजेएस, टीवीओएस में इसे आजमाया था? – user3378649

+0

क्या AngularJS को टीवीएमएल प्रोजेक्ट में एकीकृत करना संभव है। और जेसन डेटा प्राप्त करने के लिए AngularJS $ http सेवा का उपयोग करें और फिर xml स्ट्रिंग उत्पन्न करने के लिए गतिशील रूप से टेम्पलेट को प्रस्तुत करने के लिए ng-repeat का उपयोग करें? वैलिया जेएस या jQuery में वास्तव में अच्छा नहीं है। एंगुलरजेएस और टीवीओएस का एक साथ उपयोग करने के निर्देश को इंगित करने के लिए किसी से प्यार होगा! –

+0

@ हूघौ क्या आप इससे बाहर निकल गए थे? –

0

आप, एप्लिकेशन लॉन्च पर अनुरोध कॉल करने के लिए सिर्फ application.js में जोड़ने चाहते हैं:

App.onLaunch = function(options) { 
    var javascriptFiles = [ 
    `${options.BASEURL}js/resourceLoader.js`, 
    `${options.BASEURL}js/presenter.js` 
    ]; 

evaluateScripts(javascriptFiles, function(success) { 
if(success) { 
    resourceLoader = new ResourceLoader(options.BASEURL); 
    var index = resourceLoader.loadResource(`${options.BASEURL}templates/weatherTemplate.xml.js`, function(resource) { 

    var doc = Presenter.makeDocument(resource); 

    doc.addEventListener("select", Presenter.load.bind(Presenter)); 

    doc.addEventListener('load', Presenter.request); 

    navigationDocument.pushDocument(doc); 

    }); 
} else { 
    var errorDoc = createAlert("Evaluate Scripts Error", "Error attempting to evaluate external JavaScript files."); 
    navigationDocument.presentModal(errorDoc); 
} 

});

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

: - jQuery के वाक्य रचना के साथ एक आकर्षण की तरह काम करता (बुनियादी परीक्षण पास)

request: function() { 

    var xmlhttp = new XMLHttpRequest() , method = 'GET' , url = 'your Api url'; 
    xmlhttp.open(method , url , true); 
    xmlhttp.onreadystatechange = function() { 
    var status; 
    var data; 
    if (xmlhttp.readyState == 4) { 
     status = xmlhttp.status; 
     if (status == 200) { 
     data = JSON.parse(xmlhttp.responseText); 
     console.log(data); 
     } else { 
     var errorDoc = createAlert("Evaluate Scripts Error", "Error attempting to evaluate external JavaScript files."); 
     navigationDocument.presentModal(errorDoc); 
     } 
    } 
    }; 
    xmlhttp.send(); 
}, 
+0

क्या आप अपने समाधान को आपके द्वारा प्रदान किए गए समाधान के बारे में थोड़ा और विवरण जोड़कर विस्तारित कर सकते हैं? – abarisone

+0

फिर आप अपने टेम्पलेट में प्रतिक्रिया का उपभोग कैसे कर रहे हैं? – Chris

2

मैं tvOS पर इस एक बाहर का परीक्षण किया: }

presenter.js में एक विधि जोड़ें ऐप्पल टीवी पर काम करने वाले उदाहरण प्रश्न:

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

मैं इस सवाल को पार करने के लिए देख रहा था एक ही बात mplish, और द्वारा @ JasonJerrett के जवाब प्रेरित था, लेकिन यह थोड़ा कमी क्योंकि मेरे उदाहरण में मैं इस तरह जावास्क्रिप्ट में बनाया गया एक एक्सएमएल टेम्पलेट का उपयोग कर रहा पाया:

// Index.xml.js 
var Template = function() { 
    return `very long xml string`; 
}; 

मुद्दा है कि आप नहीं कर सकते एक्सएचआर अनुरोध स्वयं टेम्पलेट के अंदर करें, क्योंकि एक्सएचआर अनुरोध वास्तव में पूरा होने से पहले टेम्पलेट स्ट्रिंग वापस लौटा दी जाएगी (एसिंक्रोनस कॉलबैक के अंदर से डेटा वापस करने का कोई तरीका नहीं है)।

ResourceLoader.prototype.loadResource = function(resource, dataEndpoint, callback) { 
    var self = this; 
    evaluateScripts([resource], function(success) { 
     if (success) { 
      // Here's the magic. Perform the API call and once it's complete, 
      // call template constructor and pass in API data 
      self.getJSON(dataEndpoint, function(data) { 
       var resource = Template.call(self, data); 
       callback.call(self, resource); 
      }); 
     } else { 
      var title = "Failed to load resources", 
       description = `There was an error attempting to load the resource. \n\n Please try again later.`, 
       alert = createAlert(title, description); 

      Presenter.removeLoadingIndicator(); 

      navigationDocument.presentModal(alert); 
     } 
    }); 
} 

// From: https://mathiasbynens.be/notes/xhr-responsetype-json 
ResourceLoader.prototype.getJSON = function(url, successHandler, errorHandler) { 
    var xhr = new XMLHttpRequest(); 
    xhr.open('get', url, true); 
    xhr.onreadystatechange = function() { 
    var status; 
    var data; 
    if (xhr.readyState == 4) { 
     status = xhr.status; 
     if (status == 200) { 
     data = JSON.parse(xhr.responseText); 
     successHandler && successHandler(data); 
     } else { 
     errorHandler && errorHandler(status); 
     } 
    } 
    }; 
    xhr.send(); 
}; 

फिर टेम्पलेट समारोह भेजे स्वीकार करने के लिए संशोधित करने की आवश्यकता: मेरा समाधान संसाधन लोडर को संशोधित करने और वहाँ , पूर्व टेम्पलेट बुला और टेम्पलेट समारोह में डेटा पारित करने के लिए एक्सएचआर अनुरोध करने के लिए था पैरामीटर के रूप में एपीआई डेटा:

// Index.xml.js 
var Template = function(data) { 
    return 'really long xml string with injected ${data}'; 
}; 
संबंधित मुद्दे