0

If you have an instance of XMLHttpRequest that has timed out or been aborted, but you didn't do anything during the onTimeout events for example, how can we see what state the instance of XMLHttpRequest is in?

For example, if a request returns a 404 - it's possible to see this in the status property long after the request is complete.

But timeouts doesn't seem to leave anything behind. the readyState on timeout is 'done', and there is no response - but no way to know why there is no response (timeout, connectivity issue, abort, etc etc).

Context:

I'm using Backbone. Which uses a XMLHttpRequest from jQuery. The errorText/exception recieved by aborts/timeouts are not surfaced by Backbone to the applications logic, only the XHR instance itself.

Hence the need to find this information from the XHR directly without attaching an onTimeout event handler etc etc.

Looks like the solution might be to adapt the way it attaches the timeout event, or bubble up it's errors.

Diptendu
  • 2,120
  • 1
  • 15
  • 28
simbolo
  • 7,279
  • 6
  • 56
  • 96
  • possible duplicate of [How to detect timeout on an AJAX (XmlHttpRequest) call in the browser?](http://stackoverflow.com/questions/1018705/how-to-detect-timeout-on-an-ajax-xmlhttprequest-call-in-the-browser) – Hacketo Jun 29 '15 at 13:14
  • Well why don't you simply do something in the `onTimeout` handler to know? – Bergi Jun 29 '15 at 13:16
  • As mentioned in the question, I want to examine this *after* the timeout events have been called. The solution to that question is to listen for the event. In my case, other third party libs (backbone) have swallowed the onTimeout event and only pass back to my code the XHR object itself. – simbolo Jun 29 '15 at 13:17
  • @simbolo you are talking about `XMLHttpRequest`. I don't see any third party mentioned in your post. – Hacketo Jun 29 '15 at 13:18
  • And they pass it back after everything has happened? Maybe write a feature request for that third party library… – Bergi Jun 29 '15 at 13:19
  • @Bergi I agree, I was looking to see if they only passed the xhr instance and skipped the exception if it were possible to get this from the xhr itself, much like you can with the status for example. I will have to update Backbone which will have it's own issues. – simbolo Jun 29 '15 at 13:20
  • @Hacketo it's a jQuery XHR. So seems to have the same properties. Thought I must be missing something for this kind of information not to be stored within the xhr, when other events are. – simbolo Jun 29 '15 at 13:21

1 Answers1

0

On further inspection, although there doesn't seem to be any documentation for it, there is a textStatus property on a jqXHR which is set and available to access after the event has been triggered. I'll use this.

It doesn't help if your not using jQuery and the onTimeout even has already passed. It feels like an extra readyState option could be added to reflect timeed out state.

simbolo
  • 7,279
  • 6
  • 56
  • 96