再谈Jquery Ajax方法传递到action

以前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充。html

假设 controller中的方法是以下: jquery

  
  
           
  
  
  1. public ActionResult ReadPerson(PersonModel model)  
  2.         {  
  3.             string s = model.ToString();  
  4.             return Content(s);  
  5.         } 
  6.  
  7. public ActionResult ReadPersons(List<PersonModel> model)  
  8.         {  
  9.             string result = "";  
  10.             if (model == nullreturn Content(result);  
  11.             foreach (var s in model)  
  12.             {  
  13.                 result += s.ToString();  
  14.                 result += "-------------";  
  15.             } 
  16.             return Content(result);  
  17.         } 

 其中PersonModel定义以下: ajax

  
  
           
  
  
  1. public class PersonModel  
  2.     {  
  3.         public int id  
  4.         {  
  5.             set;  
  6.             get;  
  7.         }  
  8.         public string name  
  9.         {  
  10.             set;  
  11.             get;  
  12.         } 
  13.  
  14.         public int age  
  15.         {  
  16.             set;  
  17.             get;  
  18.         } 
  19.  
  20.         public bool gender  
  21.         {  
  22.             set;  
  23.             get;  
  24.         }  
  25.         public string city  
  26.         {  
  27.             set;  
  28.             get;  
  29.         } 
  30.  
  31.         public override string ToString()  
  32.         {  
  33.             string s = string.Format(@"id:{0}  
  34. name:{1}  
  35. age:{2}  
  36. gender:{3}  
  37. city:{4}  
  38. ", id, name, age, gender, city);  
  39.             return s;  
  40.         }  
  41.     } 

那么controller方法分别接受单个model和一个model的List。采用经过ajax传递参数。chrome

对于传递单个参数的状况,假设js代码以下: json

  
  
           
  
  
  1. var person = {  
  2.                id: "001",  
  3.                name: "zhangsan",  
  4.                age: "20",  
  5.                gender: true,  
  6.                city: "shanghai"  
  7.            }; 
  8.  
  9. var option = {  
  10.                url: '/test/ReadPerson',  
  11.                type: 'POST',  
  12. data: person,  
  13.                dataType: 'html',  
  14.                success: function (result) { alert(result); }  
  15.            };  
  16. $.ajax(option); 

 从chrome中截图能够看到以下:clipboard_thumb数组

传递的数据是一串Form数据,根据命名匹配的原则,也是能够取得数据的。p_w_picpath_thumbapp


将option 的代码改为以下 ide

  
  
           
  
  
  1. var option = {  
  2.                url: '/test/ReadPerson',  
  3.                type: 'POST',  
  4.                data: JSON.stringify(person),  
  5.                dataType: 'html',  
  6.                success: function (result) { alert(result); }  
  7.            };  
  8. $.ajax(option); 

其中JSON.stringify方法签名为 stringify ( value [ , replacer [ , space ] ] ),根据ECMA-262标准stringify 函数返回的是JSON格式的字符串。它能够有3个参数。摘抄以下:函数

The stringify function returns a String in JSON format representing an ECMAScript value. It can take three parameters. The first parameter is required. The value parameter is an ECMAScript value, which is usually an object or array, although it can also be a String, Boolean, Number or null. The optional replacer parameter is either a function that alters the way objects and arrays are stringified, or an array of Strings and Numbers that acts as a white list for selecting the object properties that will be stringified. The optional space parameter is a String or Number that allows the result to have white space injected into it to improve human readability.jsonp

默认的ContentType的属性值是"application/x-www-form-urlencoded"

引自http://www.w3.org/TR/html401/interact/forms.html#adef-enctype

看请求头的截图:

clipboard[4]_thumb

所以,传递到controller的是一个json字符串,MVC根据命名匹配也是能够得到到参数的值。


将将option 的代码改为以下

  
  
           
  
  
  1. var option = {  
  2.                 url: '/test/ReadPerson',  
  3.                 type: 'POST',  
  4.                 data: person,  
  5.                 dataType: 'html',  
  6.                  contentType: 'application/json',  
  7.                  success: function (result) { alert(result); }  
  8.                 };  

把contentType改为json格式,那么获得的是出错的信息。

虽然person是json对象,可是jquery中的ajax,data会自动的被转换成查询字符串格式key1=value1&key2=value2这种形式,很显然这种形式不是json格式,所以会出错。

要避免转换成查询字符串格式,只须要设置processData为fasle便可。processData默认是true。

这里须要注意的是:当指定了contentType的时候,数据将再也不按照Form Data的形式提交了,而是变成Request Data的形式提交。能够从图上的Request Header中看出。须要注意的是,Form Data提交的数据能够由FormCollection得到到。Request Data方式提交的则不能经过FormCollection得到。

若是把processData设置为默认值true。

p_w_picpath_thumb[3]

若是把processData设置为false。

p_w_picpath_thumb[2]

以上两种方式,按照application/json的类型传给都会失败,由于json是基于文本的格式,上面两种方式传递的都不是json文本。所以会出错。


所以,把option改为:

  
  
           
  
  
  1. var option = {  
  2.     url: '/test/ReadPerson',  
  3.     type: 'POST',  
  4.     data:JSON.stringify(person),  
  5.     dataType: 'html',  
  6.     contentType: 'application/json',  
  7.     success: function (result) { alert(result); }  
  8. };  

则传递的就是json文本,所以根据命名匹配,就能得到值了。

clipboard[8]_thumb


对于较为简单是数据类型,有时候不指定contentType也能经过命名匹配传值。可是对于稍微复杂点的数据类型,有时指定contentType: 'application/json',处理起来更加方便。

若是一个controller里的action方法是接受一个List类型的参数,好比:

public ActionResult ReadPersons(List<PersonModel> model)

那么js中先构造这样的一个json对象的数组。以下

  
  
           
  
  
  1. var persons = [{ 
  2.                 id: "001"
  3.                 name: "zhangsan"
  4.                 age: "20"
  5.                 gender: true
  6.                 city: "shanghai" 
  7.             }, 
  8.             { 
  9.                 id: "002"
  10.                 name: "lisi"
  11.                 age: "21"
  12.                 gender: false
  13.                 city: "beijing" 
  14.             } 
  15.             ]; 

单纯一个数组传递是做为data传递是,Form Data也是没法识别出的。所以把这个数组再次组成一个json形式。以下:其中json的键值用model是为了能和controller中的参数名相同,能够匹配。 

  
  
           
  
  
  1. var jsonp = { model: persons }; 
  2.            var option = { 
  3.                url: '/test/ReadPersons'
  4.                type: 'POST'
  5.                data: jsonp, 
  6.                dataType: 'html'
  7.                success: function (result) { alert(result); } 
  8.            }; 

因为未指定contentType,所以是默认的application/x-www-form-urlencoded。此时是按照Form Data的方式传递的,

clipboard

能够从截图中看到。可是这种格式的数据,controller中只能得到指定model用2个元素,没法得到元素中属性的值。

clipboard[1]

若是把data改为JSON.stringify(jsonp),以下:     

  
  
           
  
  
  1. var option = { 
  2.               url: '/test/ReadPersons'
  3.               type: 'POST'
  4.               data: JSON.stringify(jsonp), 
  5.               dataType: 'html'
  6.               success: function (result) { alert(result); } 
  7.           };  

clipboard[2]

那么传递过去的Form Data是一串字符串,controller跟没法识别出这个东西,所以获不到值。若是仅仅设置contentType: 'application/json',而传递的又不是json格式的数据,以下:

  
  
           
  
  
  1. var option = { 
  2.     url: '/test/ReadPersons'
  3.     type: 'POST'
  4.     data: jsonp, 
  5.     dataType: 'html'
  6.     contentType: 'application/json'
  7.     success: function (result) { alert(result); } 
  8. }; 

由于jquery的ajax方法会把data转换成查询字符串,所以就变成以下的样子。这串文本固然不符合json格式,所以会出现下面的错误。

clipboard[3]

clipboard[4]

若是设置contentType: 'application/json',而且设置data: JSON.stringify(persons),以下:

  
  
           
  
  
  1. var option = { 
  2.                 url: '/test/ReadPersons'
  3.                 type: 'POST'
  4.                 data: JSON.stringify(persons), 
  5.                 dataType: 'html'
  6.                 contentType: 'application/json'
  7.                 success: function (result) { alert(result); } 
  8.             }; 

那么能够得到到真正完整的json数据了

clipboard[5]


最后,此处再演示一个更复杂的参数类型,以便加深理解。

首先看一下Controller中的方法签名,TestClassB 和一个TestClassA的List。稍显复杂。 

  
  
           
  
  
  1. public ActionResult Fortest(TestClassB TB,List<TestClassA> TA) 
  2.         { 
  3.             string result = ""
  4.             return Content(result); 
  5.         } 

再看TestClassA和TestClassB,更显复杂。可是结构要清晰的话,也不是很难。

  
  
           
  
  
  1. public class TestClassA 
  2.     { 
  3.        public string a1 { setget; } 
  4.        public List<string> a2 { setget; } 
  5.     } 
  6.     public class TestClassB 
  7.     { 
  8.         public string b1 { setget; } 
  9.         public InnerTestClassC ITCC { setget; } 
  10.         public class InnerTestClassC 
  11.         { 
  12.             public List<int> c1 { setget; } 
  13.         } 
  14.     } 

看js代码:逐步的构造出一个json格式。

  
  
           
  
  
  1. $("#btn").click(function () { 
  2.             var jsondata = { TB: {}, TA: [] }; 
  3.             jsondata.TB.b1 = "b1"
  4.             jsondata.TB.ITCC = {}; 
  5.             jsondata.TB.ITCC.c1 = new Array(1, 2, 3, 4); 
  6.             var ta1 = {}; 
  7.             ta1.a1 = "a1"
  8.             ta1.a2 = new Array("a""b""x""y"); 
  9.            var ta2 = {}; 
  10.             ta2.a1 = "a2"
  11.             ta2.a2 = new Array("a2""b2""x2"); 
  12.             jsondata.TA.push(ta1); 
  13.             jsondata.TA.push(ta2); 
  14.             var option = { 
  15.                 url: '/test/Fortest'
  16.                 type: 'POST'
  17.                 data: JSON.stringify(jsondata), 
  18.                 dataType: 'html'
  19.                 contentType: 'application/json'
  20.                 success: function (result) { alert(result); } 
  21.             }; 
  22.             $.ajax(option); 
  23.         }); 

最终,发送出去的json字符串以下:

{"TB":{"b1":"b1","ITCC":{"c1":[1,2,3,4]}},"TA":[{"a1":"a1","a2":["a","b","x","y"]},{"a1":"a2","a2":["a2","b2","x2"]}]}

Controller接收到这个json串后,就能自动的匹配参数了。具体获得的参数以下截图:

clipboard[6]

clipboard[7]


总结:

1.不指定contentType的话,默认都是application/x-www-form-urlencoded方式发送。此时即使发送的是json格式的数据,默认状况下,jquery的ajax也会把他转为查询字符串的形式(能够经过修改ajax参数修改),以FormData的形式发送出去。

2.不指定contentType的时候,若是controller中的方法签名比较简单,那么即使是FormData形式的数据也能由MVC的命名匹配规则获取到数据。

3.指定contentType为'application/json'时候,发送的数据必须是符合json规范的字符串。一般,使用 JSON.stringify(jsondata)有较好的可读性,能够得到一个json字符串。固然,不是必须的。使用拼接的字符串,只要是符合json规范的,也是能够发送的。

4.若是contentType为'application/json'时,发送的data不是符合json规范的字符串,则会出错。

5.一般状况下,尽可能指定contentType为'application/json',而且发送json字符串做为发送数据,这样可读性更好,而且对于复杂的函数签名,也能起到很好的匹配。

相关文章
相关标签/搜索