通过Ajax post Json类型的数据到Controller
来源:互联网 发布:ubuntu上安装mysql 编辑:程序博客网 时间:2024/05/22 12:20
function getVehiclesPreLocation(dataArray) {
var arr = new Array();
for (var i = 0; i < dataArray.length; i++) {
arr.push(JSON.parse(dataArray[i]).No);
}
var dataSend = JSON.stringify({ "data": '' + JSON.stringify(arr) + '' });//此处的”data”即为action的入参名
$.ajax({
type: "POST",
url: "/控制器类名/Action方法名",
contentType: "application/json",//必须有
dataType: "json", //表示返回值类型,不必须
data: dataSend,//相当于 //data: "{'str1':'foovalue', 'str2':'barvalue'}",
success: function (jsonResult) {
//获取数据ok
alert(jsonResult);
}
});
};
[HttpPost]
public ActionResult GetMyData(string data)
{
if (string.IsNullOrEmpty(data)) return null;
var noList = JsonConvert.DeserializeObject<List<string>>(data);
string jsonString = _srv.GetData(noList);
return Json(jsonString);
}
-----------------------------------------------------------------------------------
View
function postSimpleData() { $.ajax({ type: "POST", url: "/Service/SimpleData", contentType: "application/json", //必须有 dataType: "json", //表示返回值类型,不必须 data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' }), //相当于 //data: "{'str1':'foovalue', 'str2':'barvalue'}", success: function (jsonResult) { alert(jsonResult); } }); } function postListString() { $.ajax({ type: "POST", url: "/Service/ListString", contentType: "application/json", dataType: "json", data: JSON.stringify({ "BuIds": ["1", "2", "3"] }), success: function (jsonResult) { alert(jsonResult); } }); } function postEmployees() { $.ajax({ type: "POST", url: "/Service/Employees", contentType: "application/json", dataType: "json", data: JSON.stringify({ "Employees": [ { "firstName": "Bill", "lastName": "Gates" }, { "firstName": "George", "lastName": "Bush" }, { "firstName": "Thomas", "lastName": "Carter" } ] }), success: function (jsonResult) { alert(jsonResult); } }); }
Controller
[HttpPost] public ActionResult SimpleData(string foo, string bar) { return Json("SimpleData", JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult ListString(List<string> buIds) { return Json("ListString", JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult Employees(List<Employee> Employees) { return Json("Employees", JsonRequestBehavior.AllowGet); }
public class Employee { public string FirstName { get; set; } public string LastName { get; set; } }
结果
值得注意的有2点:
1)Ajax 选项中
contentType: "application/json"
这一条必须写,表明request的数据类型是json。
而
dataType: "json"
这一条表示返回值的类型,不必须,且依据返回值类型而定。
2)选项中
data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' })
很多时候我们将数据写作:
{ 'foo': 'foovalue', 'bar': 'barvalue' }
这样会导致错误,因为js会默认将这个json对象放到表单数据中,故而导致controller接收不到。
有两种办法处理:第一种方式是用JSON.stringify()函数,其中JSON被Ecmascript5定义为全局对象。有关该函数的用法,见此处。
第二种方式是直接用双引号包裹起来,比如data: "{'str1':'foovalue', 'str2':'barvalue'}"。
post接收传到本地参数方法
uri = "http://test.apps-ltd.com/webapi/get_game_enter_info?unionid=oP-NDxDsUkAKu5HKJq8BEFyJhCnq&openid=" + openidJ + "";
$.ajax({
url: uri,
type: 'POST',
dataType: "json",
async: false,
data: {"unionid":"oP-NDxDsUkAKu5HKJq8BEFyJhCnq","openid":"openidJ"},
error: function () {
console.log('http1连接失败');
},
- 通过Ajax post Json类型的数据到Controller
- 通过Ajax post Json类型的数据到Controller
- 通过Ajax post Json类型的数据到Controller
- 通过Ajax post Json类型的数据到Controller
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- ajax发送json数据到springmvc的controller
- 通过Ajax进行Post提交Json数据
- java 读取 ajax post 到后台的Json数据
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
- MVC3/4项目开发中遇到的ajax提交Json数据到后台Controller处理(接收参数:多重JSON)
- 使用jquery ajax post json数据到jersey resource
- Ajax方式传json到controller的几种形式
- AJAX如何以POST的方式提交JSON数据到Form
- Ajax POSt数据到PHP的方法
- 通过Ajax技术,将数组类型数据转换为JSON格式的对象
- 用原生的AJax代码发送JSON数据到Action使其自动类型转化
- 后台Controller无法接受到post请求json串
- asp.net 使用ajax请求地址接口数据 ajax返回类型是json js get post请求数据
- 数组中只出现一次的数字
- Xamarin.Forms 用户界面——动画——简单动画
- cc.TableView之 TableViewCell带有点击事件时 滑不动的问题
- unity blend 笔记
- 深入理解Java虚拟机 第二章 Java运行时数据区域
- 通过Ajax post Json类型的数据到Controller
- 看完给跪了:技术大牛总结的Github与华为软件开发云完整对比
- PHPExcel的使用方法
- 关于JavaScript中的select()与focus()
- 安装、卸载、启动app
- VUE博文随写
- git详细版
- /etc/sysctl.conf介绍
- SQLmap简介以及防火墙绕过方法