XMLHttpRequest 方法的顺序很重要吗?

XMLHttpRequest methods order is important?

这段代码工作正常:

function callFromFlex(url, method, payload) {
   console.log("call from Flex: " + method + " " + url + " " + payload);
   var xhttp = new XMLHttpRequest();
   xhttp.open(method, url, true);
   xhttp.setRequestHeader("Content-Type", "application/json");
   xhttp.onreadystatechange = function() {
    console.log(xhttp.readyState);
       if (xhttp.readyState == 4) { 
        console.log("trying to call flash...");
           // Callback to Flash here
           ...  
       }
   };

   xhttp.send(payload);
}

但这不会 - 永远不会调用 onreadystatechange:

function callFromFlex(url, method, payload) {
    console.log("call from Flex: " + method + " " + url + " " + payload);
    var xhttp = new XMLHttpRequest();

    xhttp.setRequestHeader("Content-Type", "application/json");
    xhttp.onreadystatechange = function() {
        console.log(xhttp.readyState);
        if (xhttp.readyState == 4) {    
            console.log("trying to call flash...");
            // Callback to Flash here;
            ... 
        }
    };
    xhttp.open(method, url, true);
    xhttp.send(payload);
}

我刚刚将 xhttp.open(method, url, true) 移动到另一个位置,但从未调用过 xhttp.onreadystatechange。检查过 Firefox 45.0.2 和 IE 11,我相信它与 Flash 播放器无关。订单应该不会影响这一切吧?

方法顺序对于 XMLHttpRequest 来说绝对重要。 description of open 开头为:

Initializes a request. This method is to be used from JavaScript code; to initialize a request from native code, use openRequest() instead.

在调用 open 之前,请求未完全初始化(此处分配 不是 初始化)并且不能保证其他方法正常工作。

来自 the examples in the WhatWG spec, onreadystatechange ought to work, but I can't imagine setRequestHeader will. In fact, calling setRequestHeader before open should throw an InvalidStateError, it seems 中的一些:

If the state is not OPENED, throw an "InvalidStateError" exception.