javascript - 从ajax返回的数据对象中读取多个变量

标签 javascript jquery asp.net-mvc ajax

我正在对 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/

相关文章:

jQuery.support 可以通过 IE10+ 但不能通过 IE9-?

c# - 为什么 'Remember Me' 在 VS2013 的 ASP.NET MVC 5 样板代码中不起作用?

javascript - this.method = function(){} VS obj.prototype.method = function (){}

javascript - Firebase 类型错误的云函数 - 无法读取属性

javascript - 用户脚本计时器错误

asp.net-mvc - ASP.NET MVC - 访问 ActionFilter 中的 Controller 属性

.net - SSL 和 ASP.NET MVC

javascript - 使用 jQuery 更新文本匹配的 HTML 容器文本

jquery - 如何使用jQuery来处理click、dblclick分离中的计时器

javascript - 轨道 slider : Captions Not Displaying