Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  178] [ 1]  / answers: 1 / hits: 33523  / 13 Years ago, thu, july 21, 2011, 12:00:00

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


Any ideas? I'm testing this on Chrome.


More From » ajax

 Answers
15

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 pagen);
}
};
req.send(formData);


In your code error callback is never called because it is only triggered by network-level errors, it ignores HTTP return codes.


[#91073] Wednesday, July 20, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
xiomara

Total Points: 378
Total Questions: 90
Total Answers: 104

Location: Guernsey
Member since Thu, Oct 7, 2021
3 Years ago
;