I'm sending some json data with ajax by post
我正在通过post发送一些带有ajax的json数据
function SendF() {
$.ajax({
url: '@Url.Action("Summary")',
type: 'POST',
data: JSON.stringify(flags),
contentType: "application/json;charset=utf-8",
success: function() {
},
error: function() {
alert("Oops! We've experienced a connection problem!");
}
});
}
to my controller
到我的控制器
[HttpPost]
public ActionResult Summary(List<string> flagsChecked)
{
[...]
return View(flags);
}
and tried returning a view with data I've processed, but I guess it's not gonna happen since ajax is all about asynchronous http requests. How do I change my code to be synchronous?
并尝试使用我已处理的数据返回一个视图,但我想这不会发生,因为ajax是关于异步http请求的。如何将代码更改为同步?
2 个解决方案
#1
2
The whole idea behind using ajax is to give the user the partial page update experience. If you are making an ajax call and once that is done and you are doing a redirect to another page, it does not give the partial page update experience to user. It looks very similar to the normal form submit(full page submit).
使用ajax背后的整个想法是为用户提供部分页面更新体验。如果您正在进行ajax调用,并且一旦完成并且您正在重定向到另一个页面,则它不会向用户提供部分页面更新体验。它看起来非常类似于普通表单提交(整页提交)。
If you absolutely have to send the data to server via ajax , but want to do the redirect after the ajax call is successfully finished, you can do that using javascript in the success
or done
callback event on the $.ajax
method.
如果你绝对必须通过ajax将数据发送到服务器,但想在ajax调用成功完成后进行重定向,你可以在$ .ajax方法的成功或完成回调事件中使用javascript来实现。
All you have to do is, set the location.href
property to the new url.
您所要做的就是将location.href属性设置为新的url。
var flags = ["aa", "bb", "cc"];
$.ajax({
url: '@Url.Action("Summary")',
type: 'POST',
data: JSON.stringify(flags),
contentType: "application/json;charset=utf-8"
}).done(function(res) {
window.location.href = res.newUrl;
}).fail(function(xhr, a, error) {
console.log(error);
});
This assumes that your server action method returns a JSON response with the newUrl
property which contains the url you want to redirect to .
这假设您的服务器操作方法返回一个带有newUrl属性的JSON响应,该属性包含您要重定向到的URL。
[HttpPost]
public ActionResult Summary(List<string> flagsChecked)
{
return Json(new { newUrl = Url.Action("Index","Home") });
}
#2
1
One way to do this would be to send the request to the controller via ajax and then render a partial view on your page. The easiest way would be to use the built in ajax helpers in ASP MVC. Here is a link to another post that gives a pretty good overview:
一种方法是通过ajax将请求发送到控制器,然后在页面上呈现局部视图。最简单的方法是在ASP MVC中使用内置的ajax助手。这是一个链接到另一篇文章,提供了一个非常好的概述:
How to render partial view in MVC5 via ajax call to a controller and return HTML
如何通过调用控制器并返回HTML在MVC5中呈现局部视图
#1
2
The whole idea behind using ajax is to give the user the partial page update experience. If you are making an ajax call and once that is done and you are doing a redirect to another page, it does not give the partial page update experience to user. It looks very similar to the normal form submit(full page submit).
使用ajax背后的整个想法是为用户提供部分页面更新体验。如果您正在进行ajax调用,并且一旦完成并且您正在重定向到另一个页面,则它不会向用户提供部分页面更新体验。它看起来非常类似于普通表单提交(整页提交)。
If you absolutely have to send the data to server via ajax , but want to do the redirect after the ajax call is successfully finished, you can do that using javascript in the success
or done
callback event on the $.ajax
method.
如果你绝对必须通过ajax将数据发送到服务器,但想在ajax调用成功完成后进行重定向,你可以在$ .ajax方法的成功或完成回调事件中使用javascript来实现。
All you have to do is, set the location.href
property to the new url.
您所要做的就是将location.href属性设置为新的url。
var flags = ["aa", "bb", "cc"];
$.ajax({
url: '@Url.Action("Summary")',
type: 'POST',
data: JSON.stringify(flags),
contentType: "application/json;charset=utf-8"
}).done(function(res) {
window.location.href = res.newUrl;
}).fail(function(xhr, a, error) {
console.log(error);
});
This assumes that your server action method returns a JSON response with the newUrl
property which contains the url you want to redirect to .
这假设您的服务器操作方法返回一个带有newUrl属性的JSON响应,该属性包含您要重定向到的URL。
[HttpPost]
public ActionResult Summary(List<string> flagsChecked)
{
return Json(new { newUrl = Url.Action("Index","Home") });
}
#2
1
One way to do this would be to send the request to the controller via ajax and then render a partial view on your page. The easiest way would be to use the built in ajax helpers in ASP MVC. Here is a link to another post that gives a pretty good overview:
一种方法是通过ajax将请求发送到控制器,然后在页面上呈现局部视图。最简单的方法是在ASP MVC中使用内置的ajax助手。这是一个链接到另一篇文章,提供了一个非常好的概述:
How to render partial view in MVC5 via ajax call to a controller and return HTML
如何通过调用控制器并返回HTML在MVC5中呈现局部视图