In an ASP.NET MVC app I use jQuery for posting data on button-click:
<button onclick="addProducts()">Add products</button>
....
$.post('<%= Url.Action("AddToCart", "Cart") %>',
{
...
returnUrl: window.location.href
});
In the “AddToCart” action of “Cart” controller I use redirection to another View after posting:
public RedirectToRouteResult AddToCart(..., string returnUrl)
{
...
return RedirectToAction("Index", new { returnUrl });
}
All is okay, except this redirection. I stay on the same page after posting. I suspect it’s due to AJAX type of “POST” request.
How to solve the problem with jQuery POST request blocking the redirection?
I created a
$.form(url[, data[, method = 'POST']])function which creates a hidden form, populates it with the specified data and attaches it to the<body>. Here are some examples:With jQuery’s
.submit()method you can create and submit a form with a simple expression:Here’s the function definition: