耗时JSON API通过TVJS,tvOS电话 [英] consuming API JSon calls through TVJS-tvOS
本文介绍了耗时JSON API通过TVJS,tvOS电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用tvOS玩,我有一个关于处理JSON调用的小问题。我通过一个API来获得一些数据,让我们说的测试我打电话这个链接的缘故
<$p$p><$c$c>http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%223015%22&format=json我试图用这个功能进行一些修改
函数getDocument(URL){
VAR templateXHR =新XMLHtt prequest();
templateXHR.responseType =JSON;
templateXHR.open(GET,网址,真实);
templateXHR.send();
返回templateXHR;
}
但没有成功。任何提示或帮助?
如果我需要使用的NodeJS,我该怎么办呢?
解决方案
这是一个我的工作。它在许多方面并不理想,但显示你的东西开始使用。
函数jsonRequest(选项){ VAR URL = options.url;
VaR方法= options.method || '得到';
VAR标题= options.headers || {};
VAR体= options.body || '';
VAR回调= options.callback ||功能(ERR,数据){
console.error(options.callback失踪了这个请求);
}; 如果(!URL){
扔使用loadURL需要url参数';
} VAR XHR =新XMLHtt prequest();
xhr.responseType ='JSON';
xhr.onreadystatechange =功能(){
尝试{
如果(xhr.readyState === 4){
如果(xhr.status === 200){
回调(NULL,JSON.parse(xhr.responseText));
}其他{
回调(新错误(错误[+ xhr.status +]制作的http请求:+网址));
}
}
}赶上(ERR){
console.error('中止请求'+ URL +'。错误:'+ ERR);
xhr.abort();
回调(新错误(错误作出要求:+网址+错误:+ ERR));
}
}; xhr.open(方法,URL,真正的); Object.keys(头).forEach(功能(键){
xhr.setRequestHeader(键,标题[关键]);
}); xhr.send(); 返回XHR;
}
你还可以用下面的例子调用它:
jsonRequest({
网址:'https://api.github.com/users/staxmanade/repos',
回调:函数(ERR,数据){
的console.log(JSON.stringify(数据[0],NULL,''));
}
});
希望这有助于。
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 link
http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%223015%22&format=json
I tried to use this function with some modification
function getDocument(url) {
var templateXHR = new XMLHttpRequest();
templateXHR.responseType = "json";
templateXHR.open("GET", url, true);
templateXHR.send();
return templateXHR;
}
but didn't work out. Any hints or help ?
If I need to use NodeJS, how can I do that ?
解决方案
This is one that I got working. It's not ideal in many respects, but shows you something to get started with.
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;
}
And you can call it with the following example:
jsonRequest({
url: 'https://api.github.com/users/staxmanade/repos',
callback: function(err, data) {
console.log(JSON.stringify(data[0], null, ' '));
}
});
Hope this helps.
这篇关于耗时JSON API通过TVJS,tvOS电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文