jQuery 在ajax请求中检测重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9177252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:28:14  来源:igfitidea点击:

Detecting a redirect in ajax request?

jqueryajaxhttp

提问by kdt

I want to use jQuery to GET a URL and explicitly check if it responded with a 302 redirect, but notfollow the redirect.

我想使用 jQuery 获取 URL 并明确检查它是否以 302 重定向响应,但遵循重定向。

jQuery's $.ajaxappears to always follow redirects. How can I prevent this, and see the redirect without following it?

jQuery$.ajax似乎总是遵循重定向。如何防止这种情况发生,并在不遵循重定向的情况下查看重定向?

There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.

有各种标题为“jquery ajax 重定向”的问题,但它们似乎都涉及完成一些其他目标,而不仅仅是直接检查服务器提供的状态。

采纳答案by Nick Garvey

The AJAX request never has the opportunity to NOT follow the redirect (i.e., it must follow the redirect). More information can be found in this answer https://stackoverflow.com/a/2573589/965648

AJAX 请求永远没有机会不跟随重定向(即,它必须跟随重定向)。更多信息可以在这个答案中找到https://stackoverflow.com/a/2573589/965648

回答by Riki_tiki_tavi

Welcome to the future!

欢迎来到未来!

Right now we have a "responseURL" property from xhr object. YAY!

现在我们有一个来自 xhr 对象的“responseURL”属性。好极了!

See How to get response url in XMLHttpRequest?

请参阅如何在 XMLHttpRequest 中获取响应 url?

However, jQuery (at least 1.7.1) doesn't give an access to XMLHttpRequest object directly. You can use something like this:

但是,jQuery(至少 1.7.1)不能直接访问 XMLHttpRequest 对象。你可以使用这样的东西:

var xhr;
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
  xhr = _orgAjax();
  return xhr;
};

jQuery.ajax('http://test.com', {
  success: function(responseText) {
    console.log('responseURL:', xhr.responseURL, 'responseText:', responseText);
  }
});

It's not a clean solution and i suppose jQuery team will make something for responseURL in the future releases.

这不是一个干净的解决方案,我想 jQuery 团队会在未来的版本中为 responseURL 做一些事情。

TIP: just compare original URL with responseUrl. If it's equal then no redirect was given. If it's "undefined" then responseUrl is probably not supported. However as Nick Garvey said, AJAX request never has the opportunity to NOT follow the redirect but you may resolve a number of tasks by using responseUrlproperty.

提示:只需将原始 URL 与 responseUrl 进行比较。如果相等,则不提供重定向。如果它是“未定义的”,则可能不支持 responseUrl。然而,正如 Nick Garvey 所说,AJAX 请求永远没有机会不跟随重定向,但您可以使用responseUrl属性解决许多任务。

回答by Eli Collins

While the other folks who answered this question are (sadly) correct that this information is hidden from us by the browser, I thought I'd post a workaround I came up with:

虽然回答这个问题的其他人(遗憾地)正确地认为浏览器对我们隐藏了这些信息,但我想我会发布一个我想出的解决方法:

I configured my server app to set a custom response header (X-Response-Url) containing the url that was requested. Whenever my ajax code receives a response, it checks if xhr.getResponseHeader("x-response-url")is defined, in which case it compares it to the url that it originally requested via $.ajax(). If the strings differ, I know there was a redirect, and additionally, what url we actually arrived at.

我将我的服务器应用程序配置为设置X-Response-Url包含请求的 url的自定义响应标头 ( )。每当我的 ajax 代码收到响应时,它都会检查是否xhr.getResponseHeader("x-response-url")已定义,在这种情况下,它会将其与最初通过$.ajax(). 如果字符串不同,我知道有一个重定向,此外,我们实际到达的 url 是什么。

This does have the drawback of requiring some server-side help, and also may break down if the url gets munged (due to quoting/encoding issues etc) during the round trip... but for 99% of cases, this seems to get the job done.

这确实有需要一些服务器端帮助的缺点,并且如果 url 在往返过程中被修改(由于引用/编码问题等),也可能会崩溃……但对于 99% 的情况,这似乎得到完成的工作。



On the server side, my specific case was a python application using the Pyramid web framework, and I used the following snippet:

在服务器端,我的具体案例是使用 Pyramid Web 框架的 python 应用程序,我使用了以下代码段:

import pyramid.events

@pyramid.events.subscriber(pyramid.events.NewResponse)
def set_response_header(event):
    request = event.request
    if request.is_xhr:
        event.response.headers['X-Response-URL'] = request.url

回答by fvrab

You can now use fetch API/ It returns redirected: *boolean*

您现在可以使用fetch API/ 它返回redirected: *boolean*