admin管理员组

文章数量:1279007

In the XMLHttpRequest Spec it says that:

The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

Also says something similar about a "send() flag" in an "OPENED" state.

It's said in the specification but not in the IDL and when I create a new XMLHttpRequest I can't find those "flags".

Where are those boolean variables?

In the XMLHttpRequest Spec it says that:

The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

Also says something similar about a "send() flag" in an "OPENED" state.

It's said in the specification but not in the IDL and when I create a new XMLHttpRequest I can't find those "flags".

Where are those boolean variables?

Share Improve this question asked Mar 22, 2010 at 22:40 tiangolotiangolo 1,6061 gold badge16 silver badges29 bronze badges 3
  • 1 Any reason you are working at this low of a level? certainly jquery or similar will give you better results. – Byron Whitlock Commented Mar 22, 2010 at 22:43
  • 1 @Byron Whitlock, jQuery ajax is just a wrapper of the Ajax calls, and I agree, it gives you better error results. – Buhake Sindi Commented Mar 22, 2010 at 23:03
  • You both are right, at the end I will use jQuery and I don't even need to use those "flags" but I like to know what is happening underlying although I will finally be using jQuery for all. – tiangolo Commented Mar 24, 2010 at 14:15
Add a ment  | 

3 Answers 3

Reset to default 5

The XMLHttpRequest.readyState property is what you're looking for.

From the Spec you've given, you will see that all those "boolean" flags are actually numeric values.

  • UNSENT (numeric 0)
  • OPENED (numeric 1)
  • HEADERS_RECEIVED (numeric 2)
  • LOADING (numeric 3)
  • DONE (numeric 4)

These values are the result of XMLHttpRequest.onreadystatechange event handler. Basically, in order to get those values, do something of this effect.

//In Javascript
var request = new XMLHttpRequest();
if (request) {
  request.onreadystatechange = function() { 
    if (request.readyState == 4) { //Numeric 4 means DONE

        }
   };

request.open("GET", URL + variables, true); //(true means asynchronous call, false otherwise)
request.send(""); //The function that executes sends your request to server using the XMLHttpRequest.
}

Bear in mind, always write the onreadystatechange event BEFORE calling the XMLHttpRequest.send() method (if you decide to do asynchronous calls). Also, asynchronous calls will call XMLHttpRequest.onreadystatechange event listener so it's always vital you have that implemented.

More info on Wikipedia

I've heard that the XHR editor said that the error flag referenced in the spec is an internal implementation variable that consumers cannot access.

Same deal with the "send()" flag.

I wrote to the webapps e-mail list about those flags, this is what they responded:

Everything that authors can use is expressed in the Web IDL fragment. Everything outside of that represents some kind of data implementations need to keep around one way or another to properly implement the specification.

(That was my doubt)

本文标签: javascriptIn XMLHttpRequestwhere is error flag variableStack Overflow