XMLHtt prequest始终在呼唤"负载"事件侦听器,即使在响应具有错误状态 [英] XMLHttpRequest is always calling "load" event listener, even when response has error status
本文介绍了XMLHtt prequest始终在呼唤"负载"事件侦听器,即使在响应具有错误状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用FORMDATA到阿贾克斯的文件上传。上传作品,但问题是,错误回调是永远不会被调用。甚至当我的HTTP响应是一个500内部服务器错误(测试此我调整服务器与500响应),加载回调函数。
I'm using FormData to ajax a file upload. The upload works, but the problem is that the "error" callback is never invoked. Even when my HTTP response is a 500 internal server error (to test this I tweak server to respond with 500), the "load" callback is invoked.
function upload_image() {
var form = document.getElementById('upload_image_form');
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function(e) {
alert("Success callback");
}, false);
xhr.addEventListener("error", function(e) {
alert("Error callback");
}, false);
xhr.open("POST", "/upload_image");
xhr.send(formData);
}
任何想法?我在Chrome测试这个。
Any ideas? I'm testing this on Chrome.
推荐答案
此设置应该更适合你的需求:
This setup should work better for your needs:
var req = new XMLHttpRequest();
req.open('POST', '/upload_image');
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200)
alert(req.responseText);
else
alert("Error loading page\n");
}
};
req.send(formData);
这篇关于XMLHtt prequest始终在呼唤"负载"事件侦听器,即使在响应具有错误状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文