【发布时间】:2021-11-09 18:01:38
【问题描述】:
基本上,我必须创建一个 javascript APP 对象,它将对服务器的一系列异步请求进行排队,处理对 JSON 的响应,并从中记录错误。
JSON 处理错误很容易被“try-catch”捕获,但是像 404、500 等服务器错误仍然显示在浏览器的控制台中,而我需要在“APP.history”中静默记录。
我尝试通过下面的代码来实现它,但没有一个 404 错误会引发一个错误。我做错了什么?
xhr = new XMLHttpRequest();
xhr.open("GET", url, true)
xhr.onerror = function(){console.log("error")}
xhr.upload.onerror = function(){console.log("error")}
顺便问一下,jQuery AJAX怎么能做到呢?
【问题讨论】:
-
您可以查看
xhr.status的值。见stackoverflow.com/questions/10931270/… -
看看这里>stackoverflow.com/questions/1442425/…,希望它能回答你的问题
标签: javascript jquery ajax