Reputation: 979
Just want to alert a message when there are some unexpected errors(I changed 'controller/get_data' to 'controller/get_dat') but it does not alert anything. Could you please check what is wrong with my error handling function.
$.get('controller/get_data', function (data) {
if (data.message !== undefined) {
$( "#data_is_not_got").text(data.message);
} else {
//displaying posts
}
}, "json").error(function(jqXHR, textStatus, errorThrown){
alert("Something gone wrong. Please try again later.");
});
Upvotes: 0
Views: 347
Reputation: 11955
I believe you need to try .fail
instead of .error
.
$.get('controller/get_data', function (data) {
if (data.message !== undefined) {
$( "#data_is_not_got").text(data.message);
} else {
//displaying posts
}
}, "json").fail(function() {
alert("Something gone wrong. Please try again later.");
});
Otherwise, you could use the more basic $.ajax.
$.ajax({
url: 'controller/get_data',
type: 'GET',
success: function(data){
// Do something with data
},
error: function(data) {
alert('Something's gone wrong!');
}
});
Upvotes: 1