0

这是 html/javascrpt 文件中的代码:

var console = console || 
    {log:function(msg){window.runtime.trace(msg);}};
function waitToFinish(){
    console.log("this is wait to finish loading the url");
    console.log(this.readyState);
    console.log(this.status);
    if(this.readyState != 4){
        return;
    }
    console.log("readystate is 4");
    console.log("response text length is:"+this.responseText.length);
    if (this.status === 200 || this.status == 304) { // status is allways 0
        console.log("success");
        console.log("response text length is:"+this.responseText.length);
    }
}
function openURL(url){
    console.log("opening link:"+url);
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", url 
            ,true);
    xmlhttp.onreadystatechange
        =waitToFinish;
    xmlhttp.send(null);
    return false; 
<input type=button onclick="openURL('http://www.yahoo.com');" value="click me" />
}

这是空气中的输出:

opening link:http://www.yahoo.com
this is wait to finish loading the url
4
0
readystate is 4
response text length is:0

这是在 firefox 中激活 forcecors 插件的输出:

opening link:http://www.yahoo.com
GET http://www.yahoo.com/ 200 OK        4.3s    
this is wait to finish loading the url
1
0
this is wait to finish loading the url
2
200
this is wait to finish loading the url
... a bunch of times readystate change is called
this is wait to finish loading the url
4
200
readystate is 4
response text length is:317163
success
response text length is:317163

在空中 onreadystate 被调用一次,就是这样,状态立即为 4 但状态保持为 0 并且 responseText 为空。

http://help.adobe.com/en_US/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7eb3.html

显示 xmlhttprequest 仅检查 readystate 的示例,但如果我这样做,则 responseText 每次都是空的。同步请求有效,但异步无效

4

1 回答 1

0

在 Windows AIR 上使用 IE,所以当我的 IE 设置为脱机工作时,我的请求发生了奇怪的事情。有些没问题,因为它们在缓存中,有些会失败。

将 IE 设置为在线工作,一切都再次“正常”工作。

于 2013-01-05T11:22:41.860 回答