Jquery Ajax submits json data, jqueryjson
There is a CreateOrder Action method under the MVC controller (TestController ).
[HttpPost]public ActionResult CreateOrder(List<Person> model){ return View();}
The Person class is as follows:
public class Person{ public string Name { get; set; } public string IDCard { get; set; }}
This is similar to buying a train ticket. when buying a ticket, you must provide the information of all the passengers (name and ID number)
The front-end view code is as follows:
@ {Layout = null ;}<! DOCTYPE html> View Code
When you click the button, an Ajax request is initiated to submit json data. The json data contains the information of two passengers (this is only a demonstration of data submission and does not discuss the validity of the ID card number)
When you click the button, you can see the Ajax request initiated in the developer tool, but the form data is not json data
During debugging, we can also monitor that the parameter model is null.
From the developer tool, we can see that the request header is Content-Type: application/x-www-form-urlencoded; charset = UTF-8, but actually it should be Content-Type: application/json; charset = UTF-8
Modify the View:
@ {Layout = null ;}<! DOCTYPE html> View Code
After modifying the view, refresh the page and click the button to initiate a request again.
Successful.
Note: After contentType is set in the jquery ajax request, you must set the parameter data to a json string and use JSON. stringify (). Otherwise, the following message is displayed during submission: