我正在对 mvc Controller 进行 jquery ajax 调用。我想从 Controller 返回 2 个或更多变量。我如何为此将数据打包到 Controller 中?如何使用 jquery 提取?
最佳答案
在您的 Controller 操作中,使用内置的 Json 方法:
return Json(new {name1 = "value1", name2 = "value2"});
你的 jQuery 调用:
$.ajax({
type: "POST",
url: "/your-url",
dataType: "json",
data: {data: to_send},
success: function(msg) {
alert(msg.name1);
alert(msg.name2);
}
});
//you can of course use another ajax function jQuery provides.
关于javascript - 从ajax返回的数据对象中读取多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/736783/