再谈Jquery Ajax方法传递到action(补充)

2020-05-23 06:07:41易采站长站整理



var option = { 
               url: ‘/test/ReadPerson’, 
               type: ‘POST’, 
               data: JSON.stringify(person), 
               dataType: ‘html’, 
               success: function (result) { alert(result); } 
           }; 
$.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.
默认的ContentType的属性值是”application/x-www-form-urlencoded”
引自http://www.w3.org/TR/html401/interact/forms.html#adef-enctype
看请求头的截图:


clipboard[4]_thumb


因此,传递到controller的是一个json字符串,MVC根据命名匹配也是可以获得到参数的值。


将将option 的代码改成如下



var option = { 
                url: ‘/test/ReadPerson’, 
                type: ‘POST’, 
                data: person, 
                dataType: ‘html’, 
                 contentType: ‘application/json’, 
                 success: function (result) { alert(result); }