gurehbgui
gurehbgui

Reputation: 14684

How to catch a 404 (Not Found) or parsererror when using jQuery to call a Rest service?

I have a code like this:

$.ajax({
    url: "http://localhost/Proxy/service.php",
}).done(function() {
    console.log("rdy");
});

Now I want to know how to prevent jQuery to get a 404 (Not Found) or parsererror? When I e.g. can not reach localhost I will get a 404. But what can I do to catch this error and not show it in the console?

EDIT: localhost/Proxy/service.php is my REST service

Upvotes: 2

Views: 129

Answers (2)

Toothbrush
Toothbrush

Reputation: 2141

Use .fail:

$.ajax({
    url: "http://localhost/Proxy/service.php",
}).done(function() {
    console.log("rdy");
}).fail(function(jqXHR, textStatus, errorThrown) {
    console.log("Error!\r\n" + textStatus);
});

Most browsers allow you to disable a particular type of errors, like HTTP errors. See the screenshot below of the Firefox Developer Tools:

Firefox Developer Tools

Upvotes: 2

Mattigins
Mattigins

Reputation: 1016

$.ajax({
    url: "http://localhost/Proxy/service.php",
    statusCode: {
    404: function() {
        //Catch 404
    }
  }
}).done(function() {
    console.log("rdy");
});

Nothing you can do about the console log unfortunately.

Upvotes: 0

Related Questions