Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XMLHttpRequest, where is error flag variable?

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?

like image 611
tiangolo Avatar asked Mar 22 '10 22:03

tiangolo


3 Answers

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

like image 89
Buhake Sindi Avatar answered Nov 12 '22 09:11

Buhake Sindi


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.

like image 31
typo.pl Avatar answered Nov 12 '22 10:11

typo.pl


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)

like image 3
tiangolo Avatar answered Nov 12 '22 09:11

tiangolo