Jquery Ajax submits json data, jqueryjson

Source: Internet
Author: User

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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.