EducateYourself
EducateYourself

Reputation: 979

alert() does not work for ajax error function

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

Answers (2)

D'Arcy Rail-Ip
D'Arcy Rail-Ip

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

Ohgodwhy
Ohgodwhy

Reputation: 50767

.error is not what you expect it to be. Furthermore, it's deprecated. What you want is .fail()

.fail(function(){
     alert("Something gone wrong. Please try again later.");
});

Upvotes: 2

Related Questions