consuming API JSon calls through TVJS-tvOS
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.
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 });}
Did you call your function in the '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);}
Might be worth looking at https://mathiasbynens.be/notes/xhr-responsetype-json