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.
Related
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.
In the jQuery-docs they state that there are 4 possible error codes:
parserror
timeout
abort
error
I've noticed that parserror is given when the the reponse content-type is application/json but jQuery failed to parse it
and timeout when the server doesn't respond within 30 seconds.
This is helpful because based on these codes I can display some useful error message in the browser.
The problem is that I'm not sure when these two other codes are returned and from what I read on Google it seems that abort should be returned when the network connection is down but when I tested this I got error instead.
jQuery's ajax error handlers have three arguments returned
.fail(function(jqXHR, textStatus, errorThrown) {...
The first is the XHR object, containing the request
The second is the error, as a string, which is the one you're refering to
The third is the actual network error, if one is returned, such as 404 Not Found etc
For the second argument, there are five possible return values
null
timeout
error
abort
parsererror
null is retured when the error can't be specified by one of the other options
timeout is returned if the request times out. The duration before a timeout occurs can be set with the timeout option
error is returned if a network error occured, and the third argument will also be populated with the code, as in .fail(function(jqXHR, "error", "Not Found" )
abort is returned if the request is aborted. An XMLHttpRequest can be aborted by using the abort() method
parsererror is returned if $.ajax is given a dataType option, or if the response has a content type set, that makes jQuery try to parse the response as HTML, JSON, XML etc. and the response can't be parsed as such, and the parser fails.
When this happens, the request is successful, it's only the response that can't be parsed.
I'm fairly new to AngularJS. I'm trying to get information on the actual error that occurred during a GET request. I've got the request set up like this:
$http.get(myUrl).then(mySuccessFunction).catch(function(reason) {
//
});
The reason object that is passed to the catch has 5 properties:
config - contains information on the request I sent (the URL, method, etc.)
headers
data - always seems to be null, regardless of the error that occurred
status - always seems to be 0, regardless of the error that occurred
statusText - always seems to be "", regardless of the error that occurred
I've also looked at using the $http.get(url).success().error() technique, but the error() function is passed generally the same (seemingly useless) information, only in parameter form, rather than as properties of a single object.
From the browser console, I can see that the request is triggering a status 500, or ERR_CONNECTION_REFUSED, etc. However, I'm not seeing how to get that information from the reason object...
How do I get information on the actual error that occurred during the request?
UPDATE:
I found that the issue, for me, was actually a CORS issue. See my answer below.
I found that, in my case, the problem was a CORS issue, which was hiding the real issue.
Before posting the question, I was testing 2 error cases:
Server Unavailable (ERR_CONNECTION_REFUSED)
Server encountered an internal error (status 500)
Both error conditions were giving me exactly the same "error" data (null/0/"" for data/status/statusText) via the angular catch/error function. For (1) null/0/"" is the correct response. For (2), when my dev server was (intentionally) triggering status 500 response, it wasn't handling CORS correctly for that error response.
Apparently, for CORS issues, null/0/"" is also the correct "error" data for an Angular-generated GET.
After posting this question, I tried other error conditions (404, etc.), and started getting more useful data passed to the catch/error function.
$http.get('/someUrl').success(function(data, status, headers, config) {
}).
error(function(data, status, headers, config) {
});
the status variable should provide the error code and headers should provide better details on the request
here's the work around to get StatusText (as pointed out that some mobile browsers strip out DATA from successfull responses: https://stackoverflow.com/a/28470163/1586498
I have also discovered that .success and .error on $http in Angular does not pass the StatusText, you have to use .then(response)
I have some javascript that is making an ajax request using ajax. The preflight OPTIONS call for this request fails. I'd like to display some debug information to the user about the call and it's status code - how can I get this information from the jqXhr object in the error callback?
$.ajax
url: url
headers: headers
...
error: (jqXhr, status) =>
# how can I get info about OPTIONS call here?
At the moment, this simply isn't possible.
CORS is severely lacking in error-information when a pre-flight request fails for whatever reason, both in the spec, and in browser implementations. Relevant excerpts from HTML5 Rocks:
If there is an error in the CORS request, the browser will fire the client's onerror event handler. It will also print the following error to the console log:
XMLHttpRequest cannot load http://api.alice.com. Origin http://api.bob.com is not allowed by Access-Control-Allow-Origin.
The browser doesn't give you a lot of details on why the error occurred, it only tells you that something went wrong.
... it goes on to say (under known issues):
No error information provided to onerror handler - When the onerror handler is fired, the status code is 0, and there is no statusText. This may be by design, but it can be confusing when trying to debug why CORS requests are failing.
Indeed, all you'll find is that the jqXHR statusText property will be blank, the status code will be 0, and getAllResponseHeaders() will return an empty string.
Just to be clear: this is a problem getting data out of the underlying XMLHttpRequest object. It's not that jQuery is masking information or otherwise not making it available; it simply isn't possible (at the moment) to get error-data out of the XmlHttpRequest object in this circumstance.
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.