adding in error control to to .when.then and xdr request
so i have some code that goes and gets a json file and from that it passes
it to a bootstrap page that kicks my app into life
so I have a function that goes and gets the file and returns a callback
function makeCorsRequest(method, url, callbackFunction)
{
if ($.browser.msie && window.XDomainRequest) {
var xdr = new XDomainRequest();
xdr.open(method, url);
xdr.onload = function() {
var JSON = $.parseJSON(xdr.responseText);
if (JSON == null || typeof (JSON) == 'undefined')
{
JSON = $.parseJSON(data.firstChild.textContent);
}
window[callbackFunction](JSON);
};
xdr.onprogress = function() {};
xdr.send();
} else {
var promise = $.ajax({
url: url,
method: method,
dataType: 'json'
});
$.when(promise).then(function(result) {
window[callbackFunction](result);
});
}
}
and then some code that calls it
$(document).ready(function() {
makeCorsRequest('GET', '/data/data.json', 'processData');
});
this works and when the json is returned the processData is run, but i
need some error handling to deal with when the json cant be loaded and
processData isnt run..
what could be a good way to do this?
No comments:
Post a Comment