AJAX 问题:发生错误时服务器未返回正确的状态代码
在为我的网站上的 AJAX 脚本设计错误处理机制时,我发现发生错误时服务器返回的唯一状态代码是 0(或“未定义”)。即使我故意通过请求不存在的文件来创建 404 错误,服务器返回的唯一错误代码也是 0。我相信这个问题是由我的网络主机的服务器 (www.000webhost.com) 引起的它将 404 错误重定向到 http://error404.000webhost.com/?但是,我需要找到一种方法从服务器的响应中获取正确的错误代码,以便向用户提供有关错误的反馈...所以我的问题是:如何使服务器返回正确的状态代码,或者如果这不是问题:我的代码有什么问题?
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.status); //this alert box shows 200 normally, but during a error only shows 0
if (xmlhttp.status==200) { // 200 = OK, process result
//stuff for processing the result (when there is no error)
}
else { // error handling (creates a jgrowl notification showing the status code)
$('#jGrowl-container').jGrowl('AJAX Error Code: ' + xmlhttp.status', {sticky: true, theme: 'error'});
}
}
}
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("&s="+ScoutID + "&pw="+pword + "&rt="+RequestType + RequestText);
}
While designing a error handling mechanism for AJAX script on my website, I found that the only status code that was returned, by the server, in the event of a error was 0 (or "undefined"). Even when I intentionally created a 404 error by requesting a non-existent file, the only error code that was returned by the server was 0. I believe that this problem is caused by my web-host's server (www.000webhost.com) when it redirects 404 errors to http://error404.000webhost.com/? however, I need to find a way to get a proper error code from the server's response in order to deliver feedback to the user on what went wrong... So my question is: how do I make the server return the proper status code, or if that is not the problem: what is wrong with my code?
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.status); //this alert box shows 200 normally, but during a error only shows 0
if (xmlhttp.status==200) { // 200 = OK, process result
//stuff for processing the result (when there is no error)
}
else { // error handling (creates a jgrowl notification showing the status code)
$('#jGrowl-container').jGrowl('AJAX Error Code: ' + xmlhttp.status', {sticky: true, theme: 'error'});
}
}
}
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("&s="+ScoutID + "&pw="+pword + "&rt="+RequestType + RequestText);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有获得正确的 http 状态代码响应可能有多种原因。
请咨询您的托管提供商,他们是否正在处理或已为此定制网络服务器。您可以在他们的控制面板中检查或询问他们的服务台。
如果出现 404,您的服务器正在重定向,这可能不会让浏览器将其跟踪为 404,而是跟踪 302 或 303(重定向)。很少有托管公司会这样做。
我还注意到您已经在使用 jQuery,那么为什么不将它也用于您的 ajax 调用呢?效果很好。您可以在 http://api.jquery.com/category/ajax/ 查看
There could be different reasons why you are NOT getting proper http-status codes in response.
Check with your hosting provider whether they are handling or have customized the web server for this. You can check it in their control panel or ask their helpdesk.
Your server is redirecting in case of 404 which may not let the browser track it as 404 but instead a 302 or 303 (redirection). Few hosting companies do that.
Also I noticed that you are using jQuery already so why not use it for your ajax calls as well. It works well. You can check it out at http://api.jquery.com/category/ajax/