IE中的XMLHTTPRequest ReadyState问题

时间:2014-07-13 07:11:19

标签: javascript ajax xmlhttprequest

从我的UI线程,我正在尝试启动一个工作线程来执行XMLHTTPRequest。它在Chrome中完美运行,但在IE 10中它不起作用。 在IE中,它总是给我ReadyState = 1,Status = 0并停止。在chrome中,代码完美地运行并且符合预期。有人可以告诉我这里有什么可能与一个浏览器有关吗?

UI线程代码:

  if (typeof (Worker) !== "undefined") {
            var w = new Worker("worker.js");
            var now = new Date();
            w.postMessage({
                newid: ws.length,
                starttime: now
            });

下面是我的worker.js代码

this.onmessage = function ($event) {
var myid = 0;
var mystarttime = "";
var url = "http://rest-service.guides.spring.io/greeting";

myid = $event.data.newid;
mystarttime = $event.data.starttime;

var gotresponse = function () {
    var rs = new Response();
    if ((xhr.readyState == 4 || xhr.readyState == 3) && xhr.status == 200) {
        var result = JSON.parse(xhr.responseText);
        rs.id = myid;
        rs.starttime = mystarttime;
        rs.data = result.content;
        rs.endtime = new Date();
    }
    else {
        rs.err = 'Error.   ReadyState: ' + xhr.readyState + ' Status:' + xhr.status;
    }
    self.postMessage(rs);
    self.close();
};

var timedout = function () {
    var rs = new Response();
    rs.err = 'Error TIMEOUT occurred : ' + xhr.statusText + '  ReadyState: ' + xhr.readyState + ' Status:' + xhr.status + ' E: ' + e + ' Msg:' + e.message;
    rs.starttime = mystarttime;
    rs.data = result.content;
    rs.endtime = new Date();


    self.postMessage(rs);
    self.close();
};

function Response(name, gender) {
    this.id = 0;
    this.data = "";
    this.starttime = new Date();
    this.endtime = new Date();
    this.err = "";
}

var xhr = new XMLHttpRequest();
getSpecData();

function getSpecData() {
    try {
        xhr.onload = function () { console.log(this.responseText); };
        xhr.open('GET', url, false);
        xhr.timeout = 5000;
        xhr.setRequestHeader('Content-Type', 'application/json');
        //xhr.setRequestHeader('Authorization', 'Basic ' + btoa('sk:pwd'));
        xhr.ontimeout = timedout;
        xhr.onreadystatechange = gotresponse;
        //xhr.withCredentials = true;
        xhr.send();
    } catch (e) {
        var rs = new Response();
        rs.err = 'Error occured : ' + xhr.statusText + '  ReadyState: ' + xhr.readyState + ' Status:' + xhr.status + ' E: ' + e + ' Msg:' + e.message;
        self.postMessage(rs);
        self.close();
    }
}

};

1 个答案:

答案 0 :(得分:0)

我的意思是,在readystatechange事件发生后添加超时:

var gotresponse = function () {

    setTimeout(function(){
    var rs = new Response();
    if ((xhr.readyState == 4 || xhr.readyState == 3) && xhr.status == 200) {
        var result = JSON.parse(xhr.responseText);
        rs.id = myid;
        rs.starttime = mystarttime;
        rs.data = result.content;
        rs.endtime = new Date();
    }
    else {
        rs.err = 'Error.   ReadyState: ' + xhr.readyState + ' Status:' + xhr.status;
    }
    self.postMessage(rs);
    self.close();
    },500);

};

试一试..