Reputation: 6365
When using jQuery
and Ajax
, I pass the entire URL to ajax's url:
In case that URL
weren't there (somehow), how could I redirect to a page not found
page from inside ajax
itself? The url
go thru and .htaccess
file.
$(document).ready(function() {
$('.link').click(function () {
var parentTag = 'check=checked';
if(parentTag) {
// ajax call
$.ajax({
type:"GET",
url:"/mylink/buyers/1/0/0", //If this failed, how do I redirect to something else from here?
data:parentTag,
dataType: 'json',
beforeSend:function(callBack){
console.log('checked');
},
success: function(callBack){
console.log('Success');
},
error: function(callBack){
console.log('Failed');
},
});
}
return false;
});
});
Upvotes: 0
Views: 1845
Reputation: 2453
Just add a statusCode handler for a 404, or any other HTTP status you want!
You could also add a generic error callback, but the advantage of using the status code method is distinguishing between actual 404s (page not found errors) from the server and other kinds of connectivity problems.
$.ajax({
/* ... */
statusCode: {
404: function() {
// server says: page not found; let's redirect to 404 page
window.location.href = "http://example.com/my/404/page";
}
},
error: function() {
// something else went wrong
alert('An unknown error occurred!');
}
});
Upvotes: 2
Reputation: 423
Just put window.location
inside error callback:
$(document).ready(function() {
$('.link').click(function () {
var parentTag = 'check=checked';
if(parentTag) {
// ajax call
$.ajax({
type:"GET",
url:"/mylink/buyers/1/0/0", //If this failed, how do I redirect to something else from here?
data:parentTag,
dataType: 'json',
beforeSend:function(callBack){
console.log('checked');
},
success: function(callBack){
console.log('Success');
},
error: function(callBack){
console.log('Failed');
window.location = "http://www.example.com";
},
});
}
return false;
});
});
Upvotes: 2