nsIXMLHttpRequest 仅执行到readyState 1
我正在使用以下脚本通过 Firefox 扩展 UI 加载一些数据。
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
var www = "http://localhost/view?url=http://google.com";
req.open('GET', www, true);
if (req.readyState){
alert(req.readyState);
}
它只提醒 1。
启用严格的 javascript 后,该脚本在错误控制台中不会显示任何错误。
I am using the following script to load some data through the firefox extension UI.
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
var www = "http://localhost/view?url=http://google.com";
req.open('GET', www, true);
if (req.readyState){
alert(req.readyState);
}
It alerts just 1.
The script shows no error in the error console with the strict javascript being enabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码存在两个问题:
req.send(null)
。该请求仅在您调用send()
< 时执行/a>.您正在进行异步请求,因为您指定了 第三个参数 < code>open() 为
true
。这意味着当您请求readyState
时,该请求仍将执行。您应该像这样使用readystatechange监听器:我建议查看有关使用XMLHttpRequest的文档,其中包含上面的示例和其他几个示例。
There are two problems with your code:
req.send(null)
. The request will only execute when you callsend()
.You're doing an asynchronous request, because you specified the third parameter for
open()
to betrue
. This means that by the time you request thereadyState
, the request will still be executing. You should instead use a readystatechange listener as so:I suggest taking a look at the documentation on Using XMLHttpRequest, which contains the example above and several others.