How to detect JQuery $.get failure? Seeking simple code example -
i'm jquery n00b. i'm trying code simple using $.get(). official documentation says
if request jquery.get() returns error code, fail silently unless script has called global .ajaxerror() method or. of jquery 1.5, .error() method of jqxhr object returned jquery.get() available error handling.
so, if goes well, callback function success called. however, if request fails, http code :404, 502, etc , formulate meaningful error message user.
however, since asynchronous call can imagine might have several outstanding. how .ajaxerror() know request corresponds to? maybe better use .error() method of jqxhr object returned jquery.get()?
can please prvode extremely simple code example? perhaps success routine calls alert("page found") , failure routine checks 404 , alert("page not found")
update: following page extremely helpful ... http://api.jquery.com/jquery.get/
you're right can use jquery 1.5's new jqxhr assign error handlers $.get()
requests. how can it:
var request = $.get('/path/to/resource.ext'); request.success(function(result) { console.log(result); }); request.error(function(jqxhr, textstatus, errorthrown) { if (textstatus == 'timeout') console.log('the server not responding'); if (textstatus == 'error') console.log(errorthrown); // etc });
you can chain handlers directly onto call:
$.get('/path/to/resource.ext') .success(function(result) { }) .error(function(jqxhr, textstatus, errorthrown) { });
i prefer former keep code less tangled, both equivalent.
Comments
Post a Comment