阿贾克斯不会得到过去的readyState 1,为什么呢? [英] Ajax won't get past readyState 1, why?

查看:169
本文介绍了阿贾克斯不会得到过去的readyState 1,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让此功能工作,这确实对参数的要求网​​址然后发送到的responseText 回调该函数。

它似乎只到达 readyState的1 (感谢Firebug的命令)。

这是:

 功能要求(URL,回调){
如果(window.XMLHtt prequest){// Mozilla中,Safari浏览器,...
    HTT prequest =新XMLHtt prequest();
}否则,如果(window.ActiveXObject){// IE
    HTT prequest =新的ActiveXObject(Microsoft.XMLHTTP);
} 其他{
    返回false;
}
HTT prequest.onreadystatechange =功能(){
    的console.log(HTT prequest.readyState);
    如果(HTT prequest.readyState == 4){
        回调(HTT prequest.responseText);
    }
};
的console.log(HTT prequest,URL);
HTT prequest.open(GET,URL,真正的);
HTT prequest.send(空);
}
 

解决方案

我workarounded这个问题分配onload事件,而不是onreadystatechange的:

 功能要求(URL,回调){
如果(window.XMLHtt prequest){// Mozilla中,Safari浏览器,...
    HTT prequest =新XMLHtt prequest();
}否则,如果(window.ActiveXObject){// IE
    HTT prequest =新的ActiveXObject(Microsoft.XMLHTTP);
} 其他{
        返回false;
}

VAR readyStateChange =功能(){
    的console.log(HTT prequest.readyState);

    如果(HTT prequest.readyState == 4){
                回调(HTT prequest.responseText);
    }
};


如果(isFirefox&安培;&安培; firefoxVersion→3){
    HTT prequest.onload = readyStateChange;
} 其他 {
    HTT prequest.onreadystatechange = readyStateChange;
}

的console.log(HTT prequest,URL);
HTT prequest.open(GET,URL,真正的);
HTT prequest.send(空);
}
 

I'm trying to get this function to work, which does a request for parameter url then sends the responseText to callback which is a function.

It seems that it only gets to readyState 1 (thanks to the Firebug commands).

Here it is:

function Request(url, callback){
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else{
    return false;
}
httpRequest.onreadystatechange = function(){
    console.log(httpRequest.readyState);
    if (httpRequest.readyState == 4) {
        callback(httpRequest.responseText);
    }
};
console.log(httpRequest, url);
httpRequest.open('GET', url, true);
httpRequest.send(null);
}

解决方案

I workarounded this problem assigning onload event instead of onreadystatechange:

function Request(url, callback){
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else{
        return false;
}

var readyStateChange = function(){
    console.log(httpRequest.readyState);

    if (httpRequest.readyState == 4) {
                callback(httpRequest.responseText);
    }
};


if (isFirefox && firefoxVersion > 3) {
    httpRequest.onload = readyStateChange;
} else {
    httpRequest.onreadystatechange = readyStateChange;
}

console.log(httpRequest, url);
httpRequest.open('GET', url, true);
httpRequest.send(null);
}

这篇关于阿贾克斯不会得到过去的readyState 1,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆