【发布时间】:2014-09-24 11:03:38
【问题描述】:
我想像 $httpProvider.interceptors 在 agularjs 中那样拦截来自 REST 请求的响应: https://docs.angularjs.org/api/ng/service/$http
我正在使用 jQuery 制作一个非常小的界面,并且不想为此使用 angular。 你有和Idea吗?
实际上,我要解决的真正问题与此相同: Dealing with a 301 and location headers for a REST response in cross-domain
但我想用 jquery 以同样的方式解决它。
我尝试了这个但没有成功:(只捕获 0 状态而不是 301...)
How can I intercept ajax responses in jQuery before the event handler?
为了回答 V31,我这样做了:
$.ajaxSetup({
error: function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 301) {
alert("Element not found.");
} else {
console.log(jqXHR.status);
console.log("Error: " + textStatus + ": " + errorThrown);
}
}
});
这是我的控制台:
上面写着:
XMLHttpRequest cannot load *******. The request was redirected to '**************', which is disallowed for cross-origin requests that require preflight.
0
Error: error:
【问题讨论】:
-
我们可以看看你已经尝试了什么吗?
-
@KevinB : 你能不能多回答一下?
-
这个问题无法解决,只能通过移除重定向。对不起。您遇到的问题是 CORS 问题。使用 CORS 时无法处理重定向。
-
@KevinB:请看这篇文章:stackoverflow.com/questions/24760792/… 在这里,我设法使用 AngularJs 拦截器在重定向之前捕获了 301!
-
我的意思是你不能阻止错误出现在控制台中。您当然可以捕捉到错误,V31 的回答向您展示了如何做。然后您可以在 xhr 中找到标题。
标签: javascript jquery angularjs rest