$.getJSON()
is a kind of abstraction of a regular AJAX call where you would have to tell that you want a JSON encoded response.
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
You can handle errors in two ways: generically (by configuring your AJAX calls before actually calling them) or specifically (with method chain).
'generic' would be something like:
$.ajaxSetup({
"error":function() { alert("error"); }
});
And the 'specific' way:
$.getJSON("example.json", function() {
alert("success");
})
.done(function() { alert("second success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });
Someone give Luciano these points :) I just tested his answer -had a similar question- and worked perfectly...
I even add my 50 cents:
.error(function(jqXHR, textStatus, errorThrown) {
console.log("error " + textStatus);
console.log("incoming Text " + jqXHR.responseText);
})
Here's my addition.
From http://www.learnjavascript.co.uk/jq/reference/ajax/getjson.html and the official source
"The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead."
I did that and here is Luciano's updated code snippet:
$.getJSON("example.json", function() {
alert("success");
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function() { alert('getJSON request failed! '); })
.always(function() { alert('getJSON request ended!'); });
And with error description plus showing all json data as a string:
$.getJSON("example.json", function(data) {
alert(JSON.stringify(data));
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) { alert('getJSON request failed! ' + textStatus); })
.always(function() { alert('getJSON request ended!'); });
If you don't like alerts, substitute them with console.log
$.getJSON("example.json", function(data) {
console.log(JSON.stringify(data));
})
.done(function() { console.log('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) { console.log('getJSON request failed! ' + textStatus); })
.always(function() { console.log('getJSON request ended!'); });
I know it's been a while since someone answerd here and the poster probably already got his answer either from here or from somewhere else. I do however think that this post will help anyone looking for a way to keep track of errors and timeouts while doing getJSON requests. Therefore below my answer to the question
The getJSON structure is as follows (found on http://api.jqueri.com):
$(selector).getJSON(url,data,success(data,status,xhr))
most people implement that using
$.getJSON(url, datatosend, function(data){
//do something with the data
});
where they use the url var to provide a link to the JSON data, the datatosend as a place to add the "?callback=?"
and other variables that have to be send to get the correct JSON data returned, and the success funcion as a function for processing the data.
You can however add the status and xhr variables in your success function. The status variable contains one of the following strings : "success", "notmodified", "error", "timeout", or "parsererror", and the xhr variable contains the returned XMLHttpRequest object (found on w3schools)
$.getJSON(url, datatosend, function(data, status, xhr){
if (status == "success"){
//do something with the data
}else if (status == "timeout"){
alert("Something is wrong with the connection");
}else if (status == "error" || status == "parsererror" ){
alert("An error occured");
}else{
alert("datatosend did not change");
}
});
This way it is easy to keep track of timeouts and errors without having to implement a custom timeout tracker that is started once a request is done.
Hope this helps someone still looking for an answer to this question.
$.getJSON("example.json", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
It is fixed in jQuery 2.x; In jQuery 1.x you will never get an error callback
Why not
getJSON('get.php',{cmd:"1", typeID:$('#typesSelect')},function(data) {
// ...
});
function getJSON(url,params,callback) {
return $.getJSON(url,params,callback)
.fail(function(jqXMLHttpRequest,textStatus,errorThrown) {
console.dir(jqXMLHttpRequest);
alert('Ajax data request failed: "'+textStatus+':'+errorThrown+'" - see javascript console for details.');
})
}
??
For details on the used .fail()
method (jQuery 1.5+), see http://api.jquery.com/jQuery.ajax/#jqXHR
Since the jqXHR
is returned by the function, a chaining like
$.when(getJSON(...)).then(function() { ... });
is possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With