Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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();
    }

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?
Posted
Updated 13-Jan-11 8:57am
v2
Comments
Manfred Rudolf Bihy 13-Jan-11 15:01pm    
You showed us the request that was made via AJAX, please also post the servers response, because that is where the redirect information would be seen. Not that the AJAX object would care about that. Please see my answer for more information why this is so. Cheers!

1 solution

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
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900