jQuery 当 AJAX 请求收到“302 Moved”响应时会发生什么?

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

What happens when an AJAX request gets a "302 Moved" response?

jqueryajaxgoogle-chromehttp-status-code-302

提问by Johnny

I'm trying to AJAXify an old website form without modifying the back end. Originally, the backend would response with a "302 Moved" redirect to a "thank you" page after receiving the form.

我正在尝试 AJAXify 一个旧的网站表单而不修改后端。最初,后端会在收到表单后响应“302 Moved”重定向到“谢谢”页面。

Now, when I try to submit the form using a jQuery $.ajax call, the form data gets submitted successfully, but the "302 Moved" redirect seems to get cancelled by the browser and jQuery doesn't know what's going on.

现在,当我尝试使用 jQuery $.ajax 调用提交表单时,表单数据成功提交,但“302 Moved”重定向似乎被浏览器取消,jQuery 不知道发生了什么。

My problem is that no matter what happens, the $.ajax call returns with an error and status = 0, so I have no way to distinguish between a successful submit and an error.

我的问题是,无论发生什么,$.ajax 调用都会返回错误且状态 = 0,因此我无法区分成功提交和错误。

Is there a way to prevent the browser from trying to follow the redirect, or at least getting back the proper response codes? (I'm using Chrome.)

有没有办法阻止浏览器尝试跟随重定向,或者至少取回正确的响应代码?(我正在使用 Chrome。)

回答by Hazarapet Tunanyan

I think no,you can't do it without changing the back-end.You have to change the response header,for Ajax to know/understand what to do.If you made a redirect,you have to change the header,because Ajax call response will not do it.

我认为不,如果不更改后端,您就无法做到。您必须更改响应标头,以便 Ajax 知道/了解要做什么。如果您进行了重定向,则必须更改标头,因为 Ajax呼叫响应不会这样做。

回答by Arius

Your backend app propably has relative redirect which may somehow be not processed by jquery ajax. Location header will not include domain name in this situation, just relative path. For some (unclear for me ;)) reasons it may cause same origin policy issues. What is funny, changing redirect to absolute path should fix the issue. Tested on jQuery 1.11.1.

您的后端应用程序可能具有相对重定向,它可能以某种方式不被 jquery ajax 处理。在这种情况下,位置标头将不包含域名,仅包含相对路径。由于某些(我不清楚;))原因,它可能会导致同源政策问题。有趣的是,将重定向更改为绝对路径应该可以解决问题。在 jQuery 1.11.1 上测试。

Using code to describe:

用代码来描述:

Theoretical ajax call:

理论ajax调用:

$.ajax({
    'url': '/your-url',
    'method': 'post',
    'data': form.serialize()
}).done(function(data) {
    // something
}).fail(function(jqXHR, textStatus, errorThrown) {
    // another something
});

So in /your-url controller you may have something similar to:

所以在 /your-url 控制器中,你可能有类似的东西:

return $this->response->redirect('//same.domain/path');

or

或者

return $this->response->redirect('/path');

First one wil work. Second one not.

第一个会起作用。第二个没有。

So?

所以?

The point is that you propably need to change backend but only a little bit. You don't need to check is the request a XmlHttpRequest if you really don't want to and/or handle it in different way.

关键是你可能需要改变后端,但只需要一点点。如果您真的不想和/或以不同的方式处理它,则不需要检查请求是否为 XmlHttpRequest。

回答by Nitish Pandey

The Ajax code would look like this:

Ajax 代码如下所示:

$.ajax({
'url': '/your-url',
'method': 'post',
'data': form.serialize()
}).done(function(data) {
    // something
  }).fail(function(jqXHR, textStatus, errorThrown) {
if (jqXHR.getResponseHeader('Location') != null)
{ 
 window.Location= jqXHR.getResponseHeader('Location');
 }
 // other conditions in failure situation.
});

回答by jacob

Check the form an add {{csrf_field()}}.it is caused when the form get submitted without the token.

检查表单并添加{{csrf_field()}}。这是在没有令牌的情况下提交表单时引起的。

回答by Morten Bergset

Could this get you in the right direction?

这能让你朝着正确的方向前进吗?

$.ajax({
    type: "GET",
    url: url,
    data: data,
    complete: function(e, xhr, settings){
       if(e.status === 200){
            console.log(e.responseText);
       }else{
            console.log("error");
       }
   }
   });