jQuery 在 AJAX 调用中,不遵循 302

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6955308/
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-26 21:36:18  来源:igfitidea点击:

In an AJAX call, 302 is not followed

jqueryajax

提问by Johan

I'm using jQuery 1.6.2 to make a POST AJAX request to a page on the same domain. That page does a 302 redirect to another page.

我正在使用 jQuery 1.6.2 向同一域上的页面发出 POST AJAX 请求。该页面执行 302 重定向到另一个页面。

Now, on my local machine this work fine but on our production server, the redirect is never followed and in the Chrome inspector the request is said to be 'canceled'.

现在,在我的本地机器上,这工作正常,但在我们的生产服务器上,重定向从未被遵循,并且在 Chrome 检查器中,请求被称为“取消”。

If I visit the same page without involving javascript, everything works fine, and as I said the AJAX works on my local machine but not on the production server.

如果我在不涉及 javascript 的情况下访问同一页面,则一切正常,正如我所说,AJAX 在我的本地机器上工作,但不在生产服务器上工作。

Does anyone know what might cause this?

有谁知道这可能导致什么?

There are some differences between the servers (OS X, Apache2, PHP5.3.6, HTTP on local machine, Ubuntu, Lighttpd, PHP5.3.3, HTTPS on production) but none that to me should make any difference.

服务器之间存在一些差异(OS X、Apache2、PHP5.3.6、本地机器上的 HTTP、Ubuntu、Lighttpd、PHP5.3.3、生产上的 HTTPS),但对我来说没有任何区别。

采纳答案by Johan

Turns out a bug in the redirect code caused the redirect to go to http:// while the page that was requested was https://. That makes the browser refuse to follow the redirect.

结果是重定向代码中的一个错误导致重定向转到 http://,而请求的页面是 https://。这使得浏览器拒绝遵循重定向。

回答by Chamika Sandamal

function doAjaxCall() {
   $.ajaxSetup({complete: onRequestCompleted});
   $.get(yourUrl,yourData,yourCallback);
}

function onRequestCompleted(xhr,textStatus) {
   if (xhr.status == 302) {
      location.href = xhr.getResponseHeader("Location");
   }
}

following questions related to your answer. you can find the answer from below links.

以下问题与您的回答有关。您可以从以下链接中找到答案。

Catching 302 FOUND in JavaScript

在 JavaScript 中捕获 302 FOUND

How to manage a redirect request after a jQuery Ajax call

如何在 jQuery Ajax 调用后管理重定向请求

回答by augustomen

I consider this to be a server-side issue, and not client-side. The browser is correct not to follow redirections to httpwhen it makes ajax request by https, as that would be a security flaw.

我认为这是服务器端问题,而不是客户端问题。浏览器http在发出 ajax 请求时不遵循重定向是正确的https,因为这将是一个安全漏洞。

I realized I was using relativepaths, such as HttpResponseRedirect('/path/to/'). On some layer, that url was prepended with the http://prefix and that was what the browser received: http://example.com/path/to/

我意识到我使用的是相对路径,例如HttpResponseRedirect('/path/to/'). 在某些层,该 url 带有http://前缀,这就是浏览器收到的内容:http://example.com/path/to/

You must ensure that the Locationgets sent in the response header with a full path, including the https://.

您必须确保Location使用完整路径在响应标头中发送,包括https://.

回答by DanH

Based on this answer: https://stackoverflow.com/a/8752354/698289I found the following code to be very useful:

基于这个答案:https: //stackoverflow.com/a/8752354/698289我发现以下代码非常有用:

$('body').ajaxComplete(function (e, xhr, settings) {
    if (xhr.status == 200) {
        var redirect = null;
        try {
            redirect = $.parseJSON(xhr.responseText).redirect;
            if (redirect) {
                window.location.href = redirect;
            }
        } catch (e) {
            return;
        }
    }
});

Then you just provide JSON such as the following:

然后您只需提供 JSON,如下所示:

{redirect: '/redirect/to/this/path'}

And the ajaxCompletewill make sure to redirect the browser.

并且ajaxComplete将确保重定向浏览器。

Be mindful that $.ajax('complete')triggers AFTER $.ajax('success')or $.ajax('error')

请注意$.ajax('complete')触发 AFTER$.ajax('success')$.ajax('error')