0

I currently have an ASP.NET MVC app and on one page I have the following Ajax call:

$.ajax({
        url: "/Home/GetAuthCode/",
        type: "GET",
        contentType: 'application/json',

        success: function () {
        }
    });

This calls an ActionResult method in my ASP app:

public ActionResult GetAuthCode()
    {
        //Do Stuff
        Return View();
    }

I would like it to navigate to the view once the method has 'Done Stuff' however it doesnt return to the view but returns to the ajax call once completed.

How can I call the method from the webpage and allow it to redirect to the specified view as the ajax call does not seem to do it.

2
  • does your view with name GetAuthCode exists and try setting contentType:html. Additionally you can set window.location.href = '/Home/GetAuthCode/' in your ajax success. Commented Apr 3, 2014 at 11:09
  • 1
    This is not correct. That means that GetAuthCode will be called twice and it could easily contain functionality you do not want to occur twice. Commented Apr 3, 2014 at 11:13

4 Answers 4

1

If you really need to do the redirection in javascript, then use:

window.location.href = '/Home/GetAuthCode/';
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent thats exactly what I needed, I have to wait 5 mins then i can accept your answer :)
Do not do this in the scope of the AJAX success call please.
@ChristianDuvall with this code I wont need the ajax call at all anymore
Precisely, so why use JavaScript at all? Why not just route with a link?
0

If you are trying to navigate to a new page, then don't use the AJAX call at all, just create a route and go to it.

<a href="/Home/GetAuthCode">I just did something and went to the returned view.</a>

2 Comments

And how would I call that using javascript? Could you put your example into a javascript function?
Why do you want do use javascript? It's a simple navigation. The point of calling something with AJAX is that you do not want to route away, you want to take action then make some dynamic change within the site itself.
0

You can simply redirect in success function of ajax

$.ajax({
        url: "/Home/GetAuthCode/",
        type: "Post",


        success: function (data) {
window.location.href= data.Url;

        }
    })

in controller

public ActionResult GetAuthCode()
    {
        //Do Stuff
        return Json(new { Url = "urlsing"});
    }

Comments

0
$.ajax({
         url: '@Url.Action("GetAuthCode", "Home")',
         data: { },
         type: "GET",
         contentType: 'application/json',
         success: function () 
         {
            if(result.yourValidationIsOk)
            {
                document.location.href = '@Url.Action("RedirectAction", "Home")';
            }
            else
            {
                alert("error occured");
            }
         }
      });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.