Ajax 无法通过 ReadyState 1,为什么?

发布于 2024-07-17 00:44:53 字数 698 浏览 4 评论 0原文

我试图让这个函数工作,它会请求参数url,然后将responseText发送到callback,这是一个函数。

看起来它只到达了readyState 1(感谢Firebug命令)。

这里是:

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'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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一曲琵琶半遮面シ 2024-07-24 00:44:53

我通过分配 onload 事件而不是 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);
}

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);
}
命硬 2024-07-24 00:44:53

通过直接在浏览器中访问该 URL 来检查该 URL 是否确实响应。

用不同的浏览器测试是否得到相同的结果。

使用某种形式的 HTTP 监视器来监视客户端到服务器的对话(我最喜欢的是 Fiddler

Check that the URL in question does actually respond by visiting it directly in the browser.

Test with a different browser do you get the same result.

Use some form of HTTP monitor to watch the client to server conversation (my favorite is Fiddler)

同尘 2024-07-24 00:44:53

Ajax 请求可能不返回数据(因此,某种服务器端错误)。 尝试在 firebug 控制台中启用“show XMLHttpRequests”选项来检查这一点。

Possibly the Ajax request doesn't return data (so, a server side error of some kind). Try enabling the option 'show XMLHttpRequests' in the firebug console, to check for this.

小姐丶请自重 2024-07-24 00:44:53

我也面临同样的问题。 通过阅读下面的网址,我的问题得到了解决。

http://bytes.com/topic/javascript/answers/ 548442-ajax-readystate-1-wall

基本上,当我将函数分配为 httpRequest.onreadystatechange 的事件侦听器时,我无法向其传递任何变量。 所以我必须将变量嵌入到服务器后端的 HTTP POST 字符串中,然后从 HTTP 响应中获取它。

它适用于 FF 3。无需使用 jQuery。

I also faced the same issue. By reading the url below, I got mine solved.

http://bytes.com/topic/javascript/answers/548442-ajax-readystate-1-wall

basically, when I assign my function as the event listener for httpRequest.onreadystatechange, I cannot pass any variable to it. SO that I have to embed the variable inside the HTTP POST string to the server backend then get it back from the HTTP response.

It works fine for FF 3. No need to use jQuery.

橘味果▽酱 2024-07-24 00:44:53

我在 FireFox 上遇到了同样的问题,但在 Chrome 上没有。

问题是我的响应将 mime-type 设置为
“应用程序/八位字节流”。

将其更改为“text/html”使其也可以在 FireFox 上运行。

I had the same problem on FireFox but not on Chrome.

The problem was my response had the mime-type set to
"application/octet-stream".

Changing it to "text/html" made it work on FireFox too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文