0

I want to make a upload progress bar but it does not work this function xhr.upload.addEventListener("progress", function (e){...})... response error error undefined

$.ajax({ xhr: function () { var xhr = new window.XMLHttpRequest(); xhr.upload.addEventListener("progress", function (e) { if (e.lengthComputable) { console.log(e.loaded + " / " + e.total) } }, false); return xhr; }, type: "POST", url: server + '/api/UserFiles/UploadFile', contentType: false, processData: false, data: data, success: function (result) { alert("Uploaded Success"); }, error: function (err) { console.log(err); } }); 

on the consol the respones is: Error error undefined

2
  • 3
    You're not passing anything called err in the error callback, just (xhr, status, p3, p4). Commented Oct 11, 2017 at 15:16
  • Did you edit the code to match my comment, or is the original code the code you wrote? If so, you should leave it in the question. Commented Oct 11, 2017 at 15:22

1 Answer 1

1

You didn't correctly implement the error handler: in your example you attempt to log a variable called err which doesn't exist.

(Edit: Ok, even with the edited code, keep reading.)

According to documentation, it should instead be:

error: function (xhr, textStatus, thrownError) { console.log(xhr.status); console.error(thrownError); } 

As a side comment, don't use alert(): it's not nearly as powerful as the console API which can do amazing things like print data tables, run timers, show stack traces, and so on.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.