Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop jQuery.ajax() from logging failures to the console?

I am using the following jQuery JSONP request to check the status of a resource by URL. When the resource is not available or the server is down, my ajaxFail() function updates the display.

function fetchServerStatus(service, host) {     var port = serverStatus[service].port;     $.ajax({         url: "http://" + host + ":" + port + "/admin/server/status",         dataType: "jsonp",         timeout: 1000,         error: ajaxFail,         fail: ajaxFail,         success: ajaxDone,         done: ajaxDone,         complete: ajaxAlways,         always: ajaxAlways     }); } 

The problem I'm having is that despite declaring handlers for fail and error, which do not log anything to the console, jQuery always logs the failed request in the console. If the resource is unavailable for a long time, this builds up a huge console log, eventually crashing the browser process (Chrome in my case).

Is there any way to stop it from doing this?

like image 845
Will Hains Avatar asked Aug 14 '12 09:08

Will Hains


People also ask

How do you handle Ajax failure?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

What causes Ajax fail?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.

What is jQuery Ajax timeout?

The jQuery ajax timeout option is used to specifies that the number of milliseconds a request should wait for automatically being terminated. The jQuery ajax timeout option is a built-in option that is passed to the ajax() function in the jQuery.


1 Answers

The logging entry in chrome's console is a behaviour of chrome when any HTTP request is handled, not a problem with jQuery or ajax(XMLHttpRequest), even an <img> or <link> tag could cause this issue.

Your only choice is to fix your server-side code not to trigger an error for ajax (eg. 404 not found or wrong content-type), so based on the content logged to console, maybe a better solution could be found, please provide accurate logging message in your console.

like image 195
otakustay Avatar answered Sep 22 '22 04:09

otakustay