The ContentType in $.ajax

Source: Internet
Author: User
Tags json

In the cnodejs.org forum there is a problem, let me also very strange, said is $.ajax set the data type Applicaiton/json, the server side (Express) can not get data, curiosity, went to turn over the data, found a thing that he did not notice. So I have this article.

Body

$.ajax ContentType and DataType, ContentType mainly set the format you sent to the server, DataType set the format you received the server data.

In HTTP requests, get and post are the most common. In jquery Ajax, ContentType is the default value: Application/x-www-form-urlencoded, which is characterized by the Name/value becoming a group of & joins between each group, and The name and value are used = connected. For example: Wwwh.baidu.com/q?key=fdsa&lang=zh This is get, while the POST request is using the request body, the parameter is not in the URL, the parameter representation in the request body is also: the form of Key=fdsa&lang=zh.

The key value of the organization in the general case is no problem, it is generally said, without nested type JSON, that is, simple json, like this:

{
    a:1,
    b:2,
    c:3
}
But in some complicated situations there is a problem. For example, in Ajax you want to pass a complex JSON pair like, also say is an object nested array, the array includes the object, brother, you preach:
{
  data: {
    A: [{
      x:2
    }]
  }
}

This complex object, application/x-www-form-urlencoded This form is no way to organize the complex JSON into key-value pairs form (of course, there is also a scheme to refer to), you can send the request, but the server received the data is empty, because the Ajax There is no way to know how to deal with this data.

How can this be?

The Smart programmer discovers that HTTP can also customize the data type, so it defines a type called Application/json. This type is text, the complex JSON data of our Ajax, serialized with Json.stringify, then sent, on the server side and then with Json.parse to restore on the line, so that can handle complex objects.

$.ajax ({
    dataType: ' json ',
    contentType: ' Application/json ',
    data:JSON.stringify ({a: [{b:1, a:1}]})
})
This allows you to send a complex JSON object. This is how restclient are treated like they are now.

Reference articles

jquery Ajax documentation tells you that you can use the default application/x-www-form-urlencoded, Multipart/form-data, or text/plain of these three kinds, others can, but need to tell Ajax How to serialize it. The difference between Datetype and ContentType, this article tells you the difference between Datetype and ContentType. jquery Ajax sends JSON, this article tells you how to use ContentType. Application-x-www-form-urlencoded, this article tells you how to application-x-www-form-urlencoded the difference. Post data, this article is about the post data form.

Reprint Address: https://www.cnblogs.com/htoooth/p/7242217.html

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.