简体   繁体   English

XMLHttpRequest状态码始终为0

[英]XMLHttpRequest status code is always 0

I am trying to operate on all ajax requests and specifically handle 409s, but my status code is always 0 even though FireFox debugger will clearly show me a 409. 我试图对所有ajax请求进行操作,并专门处理409s,但是即使FireFox调试器清楚地向我显示409,我的状态代码也始终为 0。

JS Code JS代码

(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
    if(this.status === 409)
        console.log("Conflict 409")
    open.apply(this, arguments);
  };
})(XMLHttpRequest.prototype.open);

I can extremely easily generate 409s with my server code. 我可以使用服务器代码轻松生成409s。 I see FireFox and Chrome showing 409s. 我看到FireFox和Chrome显示409s。 But this.status is always 0, even on 400s. 但是,即使在400s上, this.status始终为0。 What am I doing wrong? 我究竟做错了什么?

You're checking status before the request has finished. 您在请求完成之前正在检查状态。

You can use xhook library to intercept XHR responses. 您可以使用xhook库来拦截XHR响应。

Example: 例:

xhook.after((request, response) => {
  if (response.status === 409) {
    console.log("Conflict 409")
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM