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
参考:jQuery.ajax() 文档
一般是用
application/x-www-form-urlencoded
,也就是默认值,上传文件通常是用multipart/form-data
,现在很多使用 JSON 接口的也用后面这种。text/plain
我平时见得不多。补充
jQuery 的 ajax 要发送 application/json 请求需要
contentType: "application/json;charset=UTF-8"
processData: false
data: stringify(aObject)
比如
Data format used
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.
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.
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
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.