这段代码可以正常工作:
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绝对重要。开头的描述为open
:
初始化请求。此方法将在JavaScript代码中使用;要从本机代码初始化请求,请改用openRequest()。
直到open
被调用之前,请求尚未完全初始化(这里的分配不是初始化),并且不能保证其他方法可以正常工作。
从WhatWG规范中的一些示例来看,onreadystatechange
应该可以使用,但是我无法想象setRequestHeader
会。实际上,在调用setRequestHeader
之前open
应该抛出InvalidStateError
,这似乎是:
如果状态不是OPENED,则抛出“ InvalidStateError”异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句