如何防止ajax请求使用jQuery跟踪重定向

问题描述:

我使用jQuery ajax函数来访问Web服务,但是服务器不是返回带有描述问题的状态代码的响应,而是将请求重定向到具有200头的页面,描述问题。我无法对此进行任何更改,因此我需要在客户端以某种方式解决它。

I use the jQuery ajax functions to access a web service, but the server, instead of returning a response with a status code describing a problem, the request is redirected to a page with a 200 header, describing the problem. I can't make any changes to this, so I need to solve it on the client somehow.

示例:
请求转到某个URL,即找不到,所以我收到302重定向到另一个位置。发送一个新请求,我收到200 OK,从而防止错误回调触发。

Example: A request goes to some URL which is not found, so I receive a 302 Redirect to another location. A new request is sent, and I receive a 200 OK, thus preventing the error callback to fire.

有什么方法可以防止ajax请求跟踪重定向和而是调用回调,最好是错误方法。或者,是否可以检测客户端中是否发生了重定向?

Is there some way I can prevent the ajax request to follow redirects and instead invoke a callback, preferably the error method. Alternatively, is it possible to detect if a redirect has happened in the client?

我发现您的问题很有趣,但问题是在整体看来我更多的是一种误解。至少我会试着解释一下我对这个问题的理解。

I find your question interesting, but the problem in whole seems me more a misunderstanding. At least I'll try to explain my understanding of the problem.

静默(透明)重定向是 XMLHttpRequest $ c的一部分$ c>规范(参见此处特别是单词......透明地遵循重定向......)。标准仅提及用户代理(Web浏览器)可以阻止或通知某些类型的自动重定向,但它不是 XMLHttpRequest 的一部分>。它是HTTP客户端配置(操作系统配置)或Web浏览器配置的一部分。所以 jQuery.ajax 不能有任何阻止重定向的选项。

The silent (transparent) redirection is the part of XMLHttpRequest specification (see here especially the words "... transparently follow the redirect ..."). The standard mention only that the user agent (the web browser) can prevent or notify of certain kinds of automatic redirections, but it's not a part of XMLHttpRequest. It's the part of HTTP client configuration (OS configuration) or the web browser configuration. So jQuery.ajax can't have any option where you can prevent redirection.

你可以看到HTTP重定向是HTTP协议的一部分,而不是 XMLHttpRequest 的一部分。所以它在另一个抽象层或网络堆栈上。例如,可以从HTTP代理或本地浏览器缓存中检索来自 XMLHttpRequest 的数据,它是HTTP协议的一部分。主要是服务器提供数据而非客户端可以影响缓存。

You can see that HTTP redirection is the part of HTTP protocol and not a part of XMLHttpRequest. So it's on the another level of abstraction or the network stack. For example the data from the XMLHttpRequest can be retrieved from the HTTP proxy or from the local browser cache, and it's the part of HTTP protocol. Mostly the server which provide the data and not the client can influence on caching.

您可以将问题的要求与要求进行比较防止在通信过程中更改Web服务器的IP地址或更改IP路由。在某些情况下,所有的事情都会很有趣,但是通信堆栈的另一个层次的部分内容并且不能由 jQuery.ajax管理 XMLHttpRequest

You can compare the requirement from your question with the requirement to prevent changing of IP address of the web server or the changing of the IP route during the communication. All the things can be interesting in some scenarios, but there are parts of another level of the communication stack and can't be managed by jQuery.ajax or XMLHttpRequest.

XMLHttpRequest 标准说客户端配置可以具有阻止重定向的选项。对于微软世界,我更清楚,你可以看看 WinHttpSetOption 函数,可以用 WINHTTP_DISABLE_REDIRECTS $ c设置 WINHTTP_OPTION_DISABLE_FEATURE 选项$ c>价值。另一种方法是使用 WINHTTP_OPTION_REDIRECT_POLICY 选项和 WINHTTP_OPTION_REDIRECT_POLICY_NEVER 值。可以在Windows中使用的另一个功能是 WinHttpSetStatusCallback 函数可以设置回调函数,收到一些通知,如 WINHTTP_CALLBACK_FLAG_REDIRECT

The XMLHttpRequest standard say that the client configuration can have options which prevent redirection. In case of "Microsoft world", which I better know, you can look at WinHttpSetOption function which can be used to set WINHTTP_OPTION_DISABLE_FEATURE option with the WINHTTP_DISABLE_REDIRECTS value. Another way are the usage of WINHTTP_OPTION_REDIRECT_POLICY option with the WINHTTP_OPTION_REDIRECT_POLICY_NEVER value. One more feature which one can use in Windows is the WinHttpSetStatusCallback function which can set callback function received some notifications like WINHTTP_CALLBACK_FLAG_REDIRECT.

所以一般来说,它可以实现您的要求,但解决方案可能不是独立于操作系统或Web浏览器,也不是 jQuery.ajax XMLHttpRequest

So it's do possible to implement your requirements in general, but the solution will be probably not independent from the operation system or the web browser and be not on the level of jQuery.ajax or XMLHttpRequest.