POST请求contentType设置为application/json,但请求却把data的json转成了字符串?
请大神指教是什么原因?
代码如下
$.ajax({
method: 'POST',
url: "demo_test.txt",
data: {
aa: 1,
bb: 2
},
contentType: "application/json",
success: function (result) {}
});
请求抓包
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
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
使用的数据格式
简单来说,就是把你发请求的数据当做xxx类型处理。
对应的,dataType,就是把服务端响应回来的数据当做xxx类型处理。
这只是修改请求头中的contentType,和你接受服务器响应是什么内容没有关系。
你可以加上这个拿到json格式的数据。