Ajax GET url on error jqxhr
Adding to @KhawerZeshan answer. It's better to set beforeAjax globally:
$.ajaxSetup({
beforeSend: function(jqXHR, settings) {
jqXHR.url = settings.url;
}
});
This way if you use it with async await:
try {
await $.get('http://example.com');
} catch(e) {
console.log(e.url);
}
Save your url
in a variable. And you can use it in the error function. Obviously the url will be same as it was supplied in the url parameter of the ajax request
var url = 'somewhere/foo';
$.ajax({
type: 'get',
url: url,
context: this,
success: this.mySuccess,
error: this.myError,
cache: false,
error: function(jqXHR, exception) {
//use url variable here
}
});
Another option can be this
$.ajax({
type: 'get',
url: 'https://google.com',
context: this,
success: this.mySuccess,
error: this.myError,
cache: false,
beforeSend: function(jqXHR, settings) {
jqXHR.url = settings.url;
},
error: function(jqXHR, exception) {
alert(jqXHR.url);
}
});
FIDDLE
I believe the simplest way would be:
this.url
The this
should be bounded to the ajax settings object that has the url attribute.