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": ["", "", ""] }),
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'}"。

通过Ajax post Json类型的数据到Controller的更多相关文章

  1. Ajax--PHP+JQuery+Ajax解析json、XML数据、加载页面

    一.JQuery+Ajax用get.post方式提交和请求数据 知识要点: $('#userName').blur(function () { var txt = $(this).val(); $.a ...

  2. Ajax获取 Json文件提取数据

    摘自 Ajax获取 Json文件提取数据 1. json文件内容(item.json) [ { "name":"张国立", "sex":&q ...

  3. springmvc接收JSON类型的数据

    1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...

  4. springMVC参数绑定JSON类型的数据

    需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...

  5. 如何使用ajax将json传入后台数据

    首先采用jquery内部封装好的方法是比较简单的,我们只需做的就是修改里面的一些配置: 对$.ajax()的解析: $.ajax({ type: "POST", //提交方式 co ...

  6. 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法

    现在在做的项目用到了SpringMVC框架,需要从前端angular接收请求的JSON数据,为了测试方便,所以直接先用AJAX进行测试,不过刚开始用平时用的ajax方法,提交请求会出现415或者400 ...

  7. jquery ajax提交json格式的数据,后台接收并显示各个属性

    我的表单如下: <form onsubmit="return false"> <ul> <li><span>用户名</span ...

  8. ajax请求json中的数据

    在这里不多说,直接可以运行代码看效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. ajax发送js类型的数据

    一.如图所示 var ids = [1,2,3,4,5,6,7] $.ajax({ url: requestUrl, type: 'delete', data: JSON.stringify(ids) ...

随机推荐

  1. sort函数用法

    原文链接:http://blog.csdn.net/csust_acm/article/details/7326418 sort函数的用法 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己 ...

  2. qsort函数详解

    C语言标准库函数 qsort 详解 文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作. 原文链接:http://www.slyar.c ...

  3. python装饰器入门

    按别人的教程弄的. 要清楚基于类和基于函数的实现的不同之处. #!/usr/bin/env python # -*- coding: utf-8 -*- ''' class entryExit(obj ...

  4. Solr入门之(4)配置文件solr.xml

    <?xml version="1.0" encoding="UTF-8" ?> <!-- This is an example of a si ...

  5. html5 svg 圆形进度条

    html5 svg 圆形进度条 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  6. OpenMesh 之向量操作

    OpenMesh 提供了许多可供使用的向量操作函数,使用特别方便. 计算距离: 从官方文档可以看到OpenMesh提供了5个函数,分别为 Scalar length() const        // ...

  7. Unity3D打Box游戏

    先学习一些基本的脚本实现: 1.动态创建物体.默认位置是(0,0)位置 GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube ...

  8. lr中定义字符串变量

    需要注意的是数组必须定义为固定的长度,如:char chary[20]: 5C2o"Go!\gm  nHH0 数组的最大长度为32064(32K),否则会出现“too many variab ...

  9. 主窗体里面打开子窗体&&打印饼图《Delphi 6数据库开发典型实例》--图表的绘制

    \Delphi 6数据库开发典型实例\图表的绘制 1.在主窗体里面打开子窗体:ShowForm(Tfrm_Print); procedure Tfrm_Main.ShowForm(AFormClass ...

  10. PHP Java 设置cookie方法

      Java Cookie cookie = new Cookie(COOKIE_NAME, encrypt_cookieV); cookie.setMaxAge(60 * 60); cookie.s ...