Home > Article > Web Front-end > Let's talk about Jquery Ajax method passing to action (supplement)_jquery
I wrote an article beforeJquery Ajax method to pass value to action. This article is a supplement to the article
Assume that the method in the controller is as follows:
Where PersonModel is defined as follows:
Copy code
{
age:{2}
gender :{3}
city:{4}
", id, name, age, gender, city);
return s; >
For the case of passing a single parameter, assume that the js code is as follows:
Copy code
The code is as follows:
The data passed is a string of Form data. According to the principle of naming matching, the data can also be obtained.
Change the option code to the following
Copy code
The code is as follows:
var option = {
url: '/test /ReadPerson',type: 'POST',
Therefore, what is passed to the controller is a json string, and MVC can also get the value of the parameter based on name matching.
Change the option code to the following
Change the contentType to json format, and you will get an error message.
Although person is a json object, the ajax and data in jquery will be automatically converted into the query string format key1=value1&key2=value2. Obviously this form is not in json format, so an error will occur.
To avoid conversion to query string format, just set processData to fasle. processData defaults to true.
What needs to be noted here is: when contentType is specified, the data will no longer be submitted in the form of Form Data, but will be submitted in the form of Request Data. It can be seen from the Request Header in the picture. It should be noted that the data submitted by Form Data can be obtained by FormCollection. Submissions via Request Data cannot be obtained through FormCollection.
If processData is set to the default value true.
If processData is set to false.
The above two methods will fail if they are passed according to the application/json type, because json is a text-based format, and what is passed in the above two methods is not json text. So something goes wrong.
Therefore, change the option to:
then passes the json text, so based on the name matching, the value can be obtained.
For simpler data types, sometimes the value can be passed through named matching without specifying contentType. But for slightly more complex data types, sometimes it is more convenient to specify contentType: 'application/json'.
If the action method in a controller accepts a List type parameter, such as:
public ActionResult ReadPersons(List
Then construct such an array of json objects in js first. As follows
var jsonp = { model: persons };
If you change data to JSON.stringify(jsonp), as follows:
The code is as follows:var option = {
Then the Form Data passed is a string. The controller cannot recognize this thing, so the value cannot be obtained. If you only set contentType: 'application/json', and the data passed is not in json format, as follows:
Then the Form Data passed is a string. The controller cannot recognize this thing, so the value cannot be obtained. If you only set contentType: 'application/json', and the data passed is not in json format, as follows:
Because jquery’s ajax method will convert data into a query string, it becomes as follows. This string of text certainly does not conform to the json format, so the following error will occur.
If you set contentType: 'application/json', and set data: JSON.stringify(persons), as follows:
Finally, a more complex parameter type is demonstrated here to deepen understanding.
First take a look at the method signature in Controller, TestClassB and a List of TestClassA. A little more complicated.
The code is as follows:
Finally, the json string sent is as follows:
{"TB":{"b1":"b1","ITCC":{"c1":[1,2,3,4]}} ,"TA":[{"a1":"a1","a2":["a","b","x","y"]},{"a1":"a2","a2" :["a2","b2","x2"]}]}
After the Controller receives this json string, it can automatically match the parameters. The specific parameters obtained are as follows:
Summary:
1. If the contentType is not specified, it will be sent by application/x-www-form-urlencoded by default. Even if the data in json format is sent at this time, by default, jquery's ajax will convert it into the form of a query string (can be modified by modifying the ajax parameters) and send it out in the form of FormData.
2. When contentType is not specified, if the method signature in the controller is relatively simple, then even data in the form of FormData can be obtained by the naming matching rules of MVC.
3. When contentType is specified as 'application/json', the data sent must be a string that conforms to the json specification. Generally, using JSON.stringify(jsondata) has better readability and can obtain a json string. Of course, it's not necessary. Concatenated strings can also be sent as long as they comply with the json specification.
4. If the contentType is 'application/json' and the data sent is not a string that conforms to the json specification, an error will occur.
5. Under normal circumstances, try to specify the contentType as 'application/json' and send a json string as the sending data. This will make it more readable and can also match complex function signatures well. .
This article comes from the “One Blog” blog