dar*_*job 4 javascript flash xmlhttprequest call onreadystatechange
这段代码工作正常:
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);
}
Run Code Online (Sandbox Code Playgroud)
但这不会 - 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);
}
Run Code Online (Sandbox Code Playgroud)
我刚刚将 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”异常。
| 归档时间: |
|
| 查看次数: |
763 次 |
| 最近记录: |