Ajax 无法通过 ReadyState 1,为什么?
我试图让这个函数工作,它会请求参数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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通过分配 onload 事件而不是 onreadystatechange 解决了这个问题:
I workarounded this problem assigning onload event instead of onreadystatechange:
通过直接在浏览器中访问该 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)
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.
我也面临同样的问题。 通过阅读下面的网址,我的问题得到了解决。
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.
我在 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.