为什么浏览器不进行重定向,尽管Fiddler显示了相反的结果 [英] Why browser DOESN'T make the redirect though Fiddler shows the opposite

查看:161
本文介绍了为什么浏览器不进行重定向,尽管Fiddler显示了相反的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XmlHttpRequest调用``Select''操作.

The ''Select'' action is called by XmlHttpRequest.

$.ajax(
             {
                 async: false,
                 url: ''Project/Select/1''
             });



``选择''操作使重定向



The ''Select'' action makes redirect

[HttpPost]
    public ActionResult Select(core_User user)
    {
        int id = 0;
        if (int.TryParse(this.RouteData.Values["id"].ToString(), out id))
        {
            Project.Load(id);
            return Redirect("~/general-settings"));
        }
        return new EmptyResult();
    }


在Fiddler中,我看到重定向请求已发送,但是,浏览器不会进行重定向**.它停留在上一页.

这是REDIRECT请求的RAW.


In Fiddler I see that redirect request is sent, **BUT, the browser DOESN''T make the redirect**. It stays on the previous page.

Here is the RAW of the REDIRECT request.

GET http://localhost:26838/general-settings HTTP/1.1
  Host: localhost:26838
  Connection: keep-alive
  Referer: http://localhost:26838/project-manager
  X-Requested-With: XMLHttpRequest
  Accept: application/json, text/javascript, */*
  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko)
  Chrome/8.0.552.237 Safari/534.10
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: en-US,en;q=0.8
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  Cookie: ASP.NET_SessionId=t2242xxnk4bf0qk0gkvyrji2




问题出在哪里?




Where is the problem?

推荐答案

.ajax( { 异步:错误, 网址:``项目/选择/1'' });
.ajax( { async: false, url: ''Project/Select/1'' });



``选择''操作使重定向



The ''Select'' action makes redirect

[HttpPost]
    public ActionResult Select(core_User user)
    {
        int id = 0;
        if (int.TryParse(this.RouteData.Values["id"].ToString(), out id))
        {
            Project.Load(id);
            return Redirect("~/general-settings"));
        }
        return new EmptyResult();
    }


在Fiddler中,我看到重定向请求已发送,但是,浏览器不会进行重定向**.它停留在上一页.

这是REDIRECT请求的RAW.


In Fiddler I see that redirect request is sent, **BUT, the browser DOESN''T make the redirect**. It stays on the previous page.

Here is the RAW of the REDIRECT request.

GET http://localhost:26838/general-settings HTTP/1.1
  Host: localhost:26838
  Connection: keep-alive
  Referer: http://localhost:26838/project-manager
  X-Requested-With: XMLHttpRequest
  Accept: application/json, text/javascript, */*
  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko)
  Chrome/8.0.552.237 Safari/534.10
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: en-US,en;q=0.8
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  Cookie: ASP.NET_SessionId=t2242xxnk4bf0qk0gkvyrji2




问题出在哪里?




Where is the problem?


这可以通过您通过AJAX发出的请求来解释. XMLHttpRequest对象不会像浏览器那样做出反应.它仅用于从服务器检索数据,然后您可以在javascript代码中处理该数据. Fiddler表明服务器响应包含重定向,但是XMLHttpRequest不在乎它,如果您真的很难,您将理解为什么应该这样做.
正如我之前提到的,发送AJAX请求是为了从服务器检索数据.因此,如果重定向将使浏览器转到另一个页面,则处理AJAX响应所需的所有代码都将消失.
为了解决您的问题,您必须将一些信息发送回客户端,以便您在接收到AJAX调用的服务器响应时可以对其进行分析.如果指示重定向(通过您梦dream以求的逻辑),则可以通过javascript导致重定向.

希望能将其清除一点.

如果您仍有疑问,请发表评论.

添加的信息:

Fiddler显示了浏览器和服务器之间的所有通信.这还包括AJAX对象的通信.就像我上面已经说过的那样,AJAX控件并不关心响应标头中发送的重定向信息,因为它不是浏览器,而只是用于异步获取数据.
添加结束


最好的问候,
曼弗雷德(Manfred)
This can be explained by your request being made via AJAX. The XMLHttpRequest object doesn''t react like a browser. It is only used to retrieve data from the server and then you can process that data in your javascript code. Fiddler shows that that the server response contains a redirect, but XMLHttpRequest doesn''t care about it and if you think real hard you''ll understand why that should be so.
Sending an AJAX request is for retrieveing data from the server as mentions by me before. So if a redirect would make the browser go to a different page all the code that is needed to handle the AJAX response would be gone.
To solve your problem you''ll have to send some information back to the client that you can analyze when receiving the servers response of your AJAX call. If a redirect is indicated (by whatever logic you dreamed up) you can cause a redirect via javascript.

Hope that cleared it up a little.

If you still have doubts leave a comment.

Added Info:

Fiddler shows you all the communication between the browser and the server. That also includes the communication of the AJAX object. Like I already said above the AJAX control doesn''t care about the redirect information sent in the response header, because it is NOT a browser but rather just for fetching data asynchronously.
End of addition


Best Regards,
Manfred


这篇关于为什么浏览器不进行重定向,尽管Fiddler显示了相反的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆