2

In Jquery Ajax How Can I show alert error message If the ajax url is not found/failed to load due to server down.

I tried

error:function(){
}

and

statusCodes:{
}

Those are working only If url is loaded successfully. If the url is failed to load how can I show error message ?

I am Using JSONP

3 Answers 3

1

you can use the following to check all the statuses returned by jQuery ajax

 $.ajax(
   statusCode: {
      200: function (response) {
         alert('status 200');
      },
      201: function (response) {
         alert('status  201');
      },
      400: function (response) {
         alert('status  400');
      },
      404: function (response) {
         alert('status  404 ');
      }
   }, success: function () {
      alert('success');
   },
});
Sign up to request clarification or add additional context in comments.

Comments

0

I tried this in an ajax call I was just editing and I got the alert.

                $.ajax({
                    url: "/Stores/Ajax_GetStoreDetailsw",
                    type: 'POST',
                    data: { StoreId: selectedStoreId },
                    success: function (data) {
                        $(".StoreWindows").html(data);
                    },
                    error: function () {
                        alert(9);
                        //alert the error here.
                    }
                });

I changed the url to one that is incorrect and the alert appeared.

Change your error handler to this also so you can begin to see what error codes are produced by which errors.

                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                        //alert the error here.
                    }

7 Comments

that url is not found. so can you try with that ?
works whether the url is not found, or the controller is not found. it's not relevant to the error.
@RajeevChowdaryGurram you can't request other domain, until other domain has enabled CORS support (Cross origin resource sharing)
actually, if you combine this answer with the one from Ravi, you should find this works. If not then you may want to post more code and environmental settings and setups.
@Ravi, he's not asking an XDomain question, he's asking on how to detect that the other url doesn't exist. So the example he gave was for example purposes only i believe
|
0

An object of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404: Jquery ajax try like this.

$.ajax({
  statusCode: {
    200: function() {
      alert("OK ");
    },
    404: function() {
      alert("page not found");
    }
  }
});

though you can get status code in error block also. some thing like this.

error: function (xhrReq, textstatus, errorThrown) {
                        alert(xhrReq.status);
                        alert(errorThrown);

                    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.