开发者

In XMLHttpRequest, where is error flag variable?

开发者 https://www.devze.com 2022-12-23 15:35 出处:网络
In the XMLHttpRequest Spec it says that: The DONE state has an associated error flag that indicates some type of

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() fla开发者_C百科g" 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?


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消