39

I know how to receive server data or error. There are many ways. For example:

$.ajax({
    type: "get",
    url: "/widgets/",
    success: function (data, text) {
        console.log('this is a data');
    },
    error: function (request, status, error) {
        console.log('this is an error');
    }
});

Please see this image:

result in chrome developer console

I have my error function and I do what I need there. How can I prevent error in the first line. I have tested 4xx and 5xx errors but no difference. I know that end user does not see this error when developer console is hidden.

I need this to design an API. I know that I can send 200 responses from server with an additional argument that show that this is an error or a success result.

But if I can send 4xx or 5xx responses then there is no need to extra argument. This way has some additional gains for me. For example I can apply success result to my client side models without worry about additional args.

Thank you

wprl
  • 24,489
  • 11
  • 55
  • 70
iman
  • 21,202
  • 8
  • 32
  • 31
  • jquery-1.9.1.js:8526 `// This may raise an exception which is actually` `// handled in jQuery.ajax (so no try/catch here)` `xhr.send( ( s.hasContent && s.data ) || null );` – iman May 10 '13 at 12:16
  • 6
    Wrapping the `xhr.send` in a `try/catch` does **not** prevent the error from showing in the developer console. Note that these are *not* JS errors/exceptions, but are related to the *request* that the browser sends out, so trying to catch a JS error won't work. – gkalpak May 10 '13 at 14:08
  • Try this answer: http://stackoverflow.com/questions/7436195/disabling-some-jquery-global-ajax-event-handlers-for-a-request – JVE999 Aug 10 '14 at 23:14

1 Answers1

18

AFAIK you cannot make the red thingies go away, when returning error status-codes (4xx, 5xx). They are used by the browser to indicate that something might have not gone as expected. Still these are mere decorations, since 4xx and 5xx status codes are perfectly valid, so I don't see a reason for you wanting to eliminate them.

As you said yourself, your other alternative would be sending "200 responses from server with an additional argument that show that this is an error or a success result".

I certainly recommend using the 4xx and 5xx codes though (that's what they are designed for) and stop worrying about browser's heads-up warnings in developer console (aka red thingies).

See also this and this questions that refer to similar errors when fetching other kinds of resources (images, files etc). The errors you see are actually of the same nature.

Community
  • 1
  • 1
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • 2
    There is a case were it would be nice to avoid the reporting of these errors: Testing (specially headless testing of web apps). For example, you write a test (say with mocha-phantonJS) for a failing ajax request. It is annoying having all these errors appearing in the tests summary. – emepyc Mar 04 '14 at 14:20
  • 2
    I have a check to see if a page exists, and the red error makes it look like something went wrong, so it would be nice to suppress it. – JVE999 Aug 10 '14 at 23:12
  • 1
    @JVE999: My answer holds. Don't be "red-thingy-phobic" ! 4xx and 5xx are valid codes and you can't do anything about how a browser chooses to log them (nor should you worry about it). If you are too concerned about a user opening the console and mis-judging your app due to red-thingies, maybe you could log some message to explain it and let them know it's expected behavior. – gkalpak Aug 11 '14 at 05:14