javascript XMLHttpRequest 状态返回 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25830847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLHttpRequest status returning 404
提问by Krowi
I have some bit of code from the internet that updates my webpage when I type in a text input
.
我有一些来自互联网的代码,当我输入text input
.
My code is below
我的代码在下面
function validate(field, query) {
var xmlhttp;
if (window.XMLHttpRequest) { // for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
document.getElementById(field).innerHTML = "Validating..";
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(field).innerHTML = xmlhttp.responseText;
} else {
document.getElementById(field).innerHTML = "Error Occurred. <a href='index.php'>Reload Or Try Again</a> the page.";
}
}
xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false);
xmlhttp.send();
}
After debugging I found out that the code is run twice (afaik). The first time xmlhttp.readyState
is 1, meaning that the request is being set up. The second time it's 4 meaning it's complete. So this is working like intended.
调试后我发现代码运行了两次(afaik)。第一次xmlhttp.readyState
为 1,表示正在建立请求。第二次它是 4 意味着它是完整的。所以这是按预期工作的。
The problem is that it always returns the Error Occurred
bit in the field. The reason why is that xmlhttp.status
keeps the status number 404, meaning that it is not found. I have no clue why it returns 404. If the browser I'm using is important, I'm using the latest version of Safari. I also checked the latest version of Chrome and got the same problem.
问题是它总是返回Error Occurred
字段中的位。原因是xmlhttp.status
保持状态号404,这意味着它没有找到。我不知道它为什么返回 404。如果我使用的浏览器很重要,我使用的是最新版本的 Safari。我还检查了最新版本的 Chrome 并遇到了同样的问题。
采纳答案by Seventoes
Make sure that your code is actually requesting the correct URL. You can do this with most modern browser's developer tools, including the Network tab of Chrome's.
确保您的代码实际上请求的是正确的 URL。您可以使用大多数现代浏览器的开发人员工具执行此操作,包括 Chrome 的“网络”选项卡。