Stop jQuery from console logging on failed XHR GET request - javascript

I am trying to load a page when it is done with certain processes. When the process is still running, it returns a 503 error code, when the page is loaded, it returns a 200 code.
I am doing this via the $.get function. However, when jQuery get's a 503 error code (and probably also with other error codes), it logs this in the console:
XHR failed loading: GET "URL".
Example:
How do I remove this console.log()?

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 or tag could cause this issue.
So you cannot stop the chrome browser from logging 503 errors in console. Instead you should correct the server side implementation. The server should return 200 even if the process is running, with a response data indicating that the process is running. (You may run your process in background thread on the server asynchronously and return the response on the main thread.)

Related

Handling get response while server has been crashed

I have a question.
I send a Get request by Ajax every second and expect to get some response from the server.
In the case and my application crashes and I continue to send Get request every second, I don't get any response until receiving the server's timeout error (an error code 502.) .
If somebody knows how I can handle this moment between sending the get requests every second and until receiving error code 502.
Thanks
If the server is down, whatever you are using for the ajax call should have errored out... in which case you should handle it with a call back or a catch block. There, you should handle what you will do on the frontend based on the response.
As far as not sending out more requests, there really is no way to know for sure. For all your code knows, the server is particularly slow at that moment.

Fetch() sends error to console | JavaScript [duplicate]

I'm writing a script that uses an XMLHttpRequest to search for a file defined by a relative path, by attempting to resolve that relative path against other same domain absolute paths that the script is aware of, then attempting to load the file from that resolved url. If I encounter a 404, I just try to resolve the files relative path against another absolute path, and try again. For this particular script, its perfectly fine to encounter a 404- however, my console is littered with 'Failed to load resource: the server responded with a status of 404 (Not Found) messages, and I want to suppress them.
There is no error to catch as far as I can see- error cases are handled by the xmlHttpRequest.onreadystatechange handler, and there is no window.onerror.
Is there any way to suppress these messages?
Thanks
This feature was introduced last year. You can enable it here: DevTools->Settings->General->Console->Hide network messages.
See also Filtering the Console output and Additional settings in the devtools documentation.
Use console.clear() in the catch block or error handler function.
It will clear those request error on the console immediately after it is logged.
PLEASE NOTE
From MDN
Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.
Read more about it on MDN
try {
var req = new XMLHttpRequest();
req.open('GET', 'https://invalidurl', false);
req.send();
} catch(e) {
console.clear();
}
Should log this
GET https://invalidurl/ net::ERR_NAME_NOT_RESOLVED
but it will be cleared.
If you use a web worker to perform XMLHttpRequest requests, the errors will be logged into the worker script context so they will not appear on the console unless this context will be specifically chosen for display.

How to suppress javascript errors when xmlhttp.status != 200

In my Google Chrome console I keep receiving errors when the xmlhttp status does not result in 200. For example, in certain cases I intentionally set the header of my PHP files to 500 so in javascript I can display the error and avoid anything that requires that PHP file. Since I have my own error handler for this, is there anyway to suppress the default error?
Error Example:
POST http://localhost/mama/cgi-bin/pages/Module-Install.php 500 (Internal server error)
It seems that there is no way to supress these warnings in google chrome when handling the error yourself. For any of those who are looking for an alternate solution, embed the error code in a json object, and parse the json object client side for any errors.
This error was sended by Server side, not browser, not js. You can not just "hide" it from console. You can handle it in onerror event:
request.onerror = function (e) {
// do something to fix the results of server error...
};
In other words, this is a server error and it CAN break your code execution, but if you make couple steps to resume the job after a possible exception from xmlHttp class, the code will be fine.
UPD:
Just if you are pure perfectionist, and you cares about flawless environment:
In a server side you must implement the server answer without a firing any statuses except 200. It's not a super hard job.
For ASP: Suppressing HTTP 500 response codes;
For PHP: http://php.net/manual/en/function.http-response-code.php .

jQuery.getJSON().error() isn't being executed on 404 error

This is a brief outline of my code:
$.getJSON(json_url, function(data) {
// application logic
}).error(function() {
console.log("error");
});
The problem is that when the server returns a 404 error, it does not appear to be handled as there is no console.log() saying error, but there is a GET request failure with a code of 404 (Not Found) showing up in the console.
I am using jQuery 1.9.0.
Is there some simple error I am making?
Due to the nature of JSONP requests, the error callback is not called for those.
From the docs:
When data is retrieved from remote servers (which is only possible using the script or jsonp data types), the error callbacks and global events will never be fired.

In what way Backbone.js error function is triggered

In Backbone.js in what terms the error state is triggered. I'm facing a challange and error is triggered even it receives 200. I've read it an article that success can be forced by sending a 204 status code from php.
What are the circumstances that causes error to trigger?
If your response is returning anything other than JSON, even with a 200 status, you'll get an error. For instance, in the app I'm building, I'm calling into an old REST endpoint that returns XML. The response is valid, but error gets invoked every time. If you can get your response to return as JSON, the error condition should go away.

Categories

Resources