【问题标题】:xmlhttprequest(xhr) object difference between xhr.onerror and xhr.upload.onerrorxhr.onerror 和 xhr.upload.onerror 之间的 xmlhttprequest(xhr) 对象区别
【发布时间】:2012-09-17 18:23:31
【问题描述】:

这可能是一个非常明显的问题,但请解释xhrxhr.upload 之间的区别是什么?

我的用例是我正在将文件上传到服务器,但假设服务器出现故障。在这种情况下我应该处理xhr.onerror 还是xhr.upload.onerror

基本上,正如问题中提到的,何时使用xhr,何时使用xhr.upload where xhr = new XMLHttpRequest();

另一个新手问题,它与如何处理 GET 和 POST/PUT 有关吗?

【问题讨论】:

    标签: javascript html file-upload xmlhttprequest


    【解决方案1】:

    xhr.onerror 在网络级别出现故障时触发。如果错误仅存在于应用程序级别,例如,发送了 HTTP 错误代码,则 onload 仍然会触发。 您需要在 onreadystatuschange 处理程序中显式测试返回的状态代码。

    当您尝试上传时,xhr.upload.onerror 触发了一些事情,这基本上是应用程序级别的功能,就像xhr.upload.Onprogress

    此上传始终依赖于 ajax post 方法。

    【讨论】:

    • 谢谢,这澄清了很多。但是,我仍然无法理解 xhr 和 xhr.upload 之间的区别,即 xhr.addEventListener("error", errorhandler) 和 xhr.upload.addEventListener("error", errorhandler) {same for progress event}。您能否提供一个示例何时触发 xhr.onerror 以及何时触发 xhr.upload.onerror(例如,如果服务器回复 500/内部错误)。如果我可以使用 .onreadystatechange 事件,那么 xhr.upload 有什么用?
    猜你喜欢
    • 2019-02-15
    • 2012-01-12
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2018-03-26
    • 2011-07-13
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多