search

Home  >  Q&A  >  body text

What does jquery ajax contentType mean?

The contentType of the POST request is set to application/json, but the request converts the json of data into a string?
What is the reason for asking God to enlighten me?
code show as below

$.ajax({
        method: 'POST',
        url: "demo_test.txt",
        data: {
            aa: 1,
            bb: 2
        },
        contentType: "application/json",
        success: function (result) {}
    });

Request packet capture

POST http://localhost:8888/demo_test.txt HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 9
Origin: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36
Content-Type: application/json
Accept: */*
X-Requested-With: XMLHttpRequest
Referer: http://172.17.35.112:8099/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie: selectFluence=4; VFS_USERNAME=admin; VFS_PASSWORD=123456; VFS_APPURL=; VFS_ISSAVE=true; VFS_ISDMZ=true; webserver_is_save=0; _alert=1495876699555

aa=1&bb=2
phpcn_u1582phpcn_u15822709 days ago1013

reply all(6)I'll reply

  • 漂亮男人

    漂亮男人2017-07-05 11:04:37

    参考:jQuery.ajax() 文档

    contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

    Type: Boolean or String

    When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

    一般是用 application/x-www-form-urlencoded,也就是默认值,上传文件通常是用 multipart/form-data,现在很多使用 JSON 接口的也用后面这种。text/plain 我平时见得不多。

    补充

    jQuery 的 ajax 要发送 application/json 请求需要

    1. contentType: "application/json;charset=UTF-8"

    2. processData: false

    3. data: stringify(aObject)

    比如

    $.ajax("https://blablabla.com/", {
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        type: "post",
        processData: false,
        data: JSON.stringify({
            user: {
                name: "hello",
                pass: "world"
            },
            stamp: new Date()
        })
    });

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 11:04:37

    Data format used

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 11:04:37

    To put it simply, the data you request is treated as xxx type.
    Correspondingly, dataType is to treat the data returned by the server as xxx type.

    reply
    0
  • 黄舟

    黄舟2017-07-05 11:04:37

    The data transferred in http are all strings, but the server will parse the strings in different ways according to the contentType when receiving the data. Objects can only exist in memory, not just http, all data transmitted over the network is based on strings.

    reply
    0
  • 天蓬老师

    天蓬老师2017-07-05 11:04:37

    First of all, I don’t think there is a problem with your packet capture. If you are indeed using a POST request,
    From the packet capture, it seems that this is a GET request, because POST does not serialize the request parameters

    Let’s talk about what contentType means?

    The contentType of ajax is the HTTP request header set. The purpose of this header is to tell the server what format of data my request parameters are. You have to process them according to the corresponding format, that's it.
    The default is "application/x-www-form-urlencoded; charset=UTF-8", which is the format of ordinary form submission. Of course, you can also override it, such as "application/json", so that the server can get it directly to a json request parameter. Instead of key values ​​one by one

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-05 11:04:37

    This just changes the contentType in the request header and has nothing to do with the content of the server response you receive.

    You can add this to get the data in json format.

    dataType:"json"

    reply
    0
  • Cancelreply