I use the following to catch an unauthorized access and force a login:
$.ajaxSetup({
statusCode: {
401: function(){
//do stuff
}
}
});
Works great... when accessing resources on my servers (domain), but now I'm attempting to use 3rd party API resources via ajax calls and their 401's are caught by this as well. So, how might I either:
Thanks much!
version added: 1.0.Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time.
Definition and Usage. The ajaxSetup() method sets default values for future AJAX requests.
Category: Global Ajax Event HandlersThese methods register handlers to be called when certain events, such as initialization or completion, take place for any Ajax request on the page. The global events are fired on each Ajax request if the global property in jQuery. ajaxSetup() is true , which it is by default.
ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.
Through experimentation I discovered the answer. $.ajaxSetup() basically sets a default for any $.ajax() requests. However, the global setup can be overridden for a specific request like so:
$.ajaxSetup({
statusCode: {
401: function(){
//this will catch any and all access denied errors
}
}
});
$.ajax({
type: 'GET',
dataType: 'json',
url: someresouceurl,
statusCode: {
401: function(){
//except this one!
}
},
success: function(data) {
}
});
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