onreadystatechange永远不会被调用

时间:2012-11-02 15:12:13

标签: javascript ajax xmlhttprequest

我创建了XHR,我也正确地从服务器获得响应。 但由于某种原因,回调没有发生。请帮忙。

function uploadDr(){
    var url = "UploadExcelServlet";     
    if (req != null) {
        try {
            req.open("GET", url, true);
        } catch (e) {
            alert(e);
        }
        req.onreadystatechange = callBk;
        req.setRequestHeader("Content-Type", "multipart/form-data");
        req.send();
    } else {
        alert("Null Request");
    }
}


function callBk(){
    if (req.readyState == 4){
        if (req.status == 200){
            alert(req.responseXml);
        } else {
            alert(req.statusText);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对于GET请求,需要设置“Content-Type”标头,因为数据总是在URL中传递。

也许你需要设置请求数据发送如下:

req.send(null);

然后在回调函数中测试readyState

function callBk(){
    console.log(req.readyState); // don't stop the script by using alert!
    // other stuff
}

确保readyState不等于0XMLHttpRequest.UNSENT)。

相关问题