Back to Devexpress

Why redirect to another page or View does not work on AJAX requests

aspnetmvc-404539-troubleshooting-server-side-issues-redirect-to-another-page-or-view-does-not-work.md

latest729 B
Original Source

Why redirect to another page or View does not work on AJAX requests

  • Aug 30, 2023

This error occurs when you redirect to another page on AJAX requests ( $.ajax()) on the server. For example, when you call the Redirect method in an action method that handles AJAX requests in a controller.

In this case, return to the initial View and then use JavaScript to redirect to another page.

Controller:

csharp
return Json(new { newUrl = "https://www.devexpress.com" });

View:

csharp
$.post('@Url.Action("Save")',  
    function (data) {  
        if(data.newUrl)  
            window.location.href = data.newUrl;  
    }  
);