Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmlhttprequest(xhr) object difference between xhr.onerror and xhr.upload.onerror

This might be a very obvious question but please explain what is the difference between xhr and xhr.upload?

My use case is that I am uploading a file to a server but suppose the server goes down. In this case should I handle xhr.onerror or xhr.upload.onerror

Basically, as mentioned in the question, when to use xhr and when to use xhr.upload where xhr = new XMLHttpRequest();

Another newbie question on the same, is it related to how GET vs POST/PUT are handled?

like image 728
gashu Avatar asked Sep 03 '25 03:09

gashu


1 Answers

xhr.onerror fires when there is a failure on the network level. If the error only exists on the application level, e.g., an HTTP error code is sent, then onload still fires. You need to test the returned status code explicitly in your onreadystatuschange handler.

where as xhr.upload.onerror fires something happend when you are trying to upload, this is basically application level function just like xhr.upload.Onprogress.

This upload always depends upon the ajax post method.

like image 167
Parveen Sharief Avatar answered Sep 05 '25 22:09

Parveen Sharief



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!