国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - jquery ajax contentType是啥意思?

瀏覽:114日期:2023-02-21 17:52:55

問題描述

POST請(qǐng)求contentType設(shè)置為application/json,但請(qǐng)求卻把data的json轉(zhuǎn)成了字符串?請(qǐng)大神指教是什么原因?代碼如下

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

請(qǐng)求抓包

POST http://localhost:8888/demo_test.txt HTTP/1.1Host: localhostConnection: keep-aliveContent-Length: 9Origin: localhostUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36Content-Type: application/jsonAccept: */*X-Requested-With: XMLHttpRequestReferer: http://172.17.35.112:8099/Accept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8Cookie: selectFluence=4; VFS_USERNAME=admin; VFS_PASSWORD=123456; VFS_APPURL=; VFS_ISSAVE=true; VFS_ISDMZ=true; webserver_is_save=0; _alert=1495876699555aa=1&bb=2

問題解答

回答1:

參考: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,也就是默認(rèn)值,上傳文件通常是用 multipart/form-data,現(xiàn)在很多使用 JSON 接口的也用后面這種。text/plain 我平時(shí)見得不多。

補(bǔ)充

jQuery 的 ajax 要發(fā)送 application/json 請(qǐng)求需要

contentType: 'application/json;charset=UTF-8'

processData: false

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() })});

javascript - jquery ajax contentType是啥意思?

回答2:

使用的數(shù)據(jù)格式

回答3:

簡單來說,就是把你發(fā)請(qǐng)求的數(shù)據(jù)當(dāng)做xxx類型處理。對(duì)應(yīng)的,dataType,就是把服務(wù)端響應(yīng)回來的數(shù)據(jù)當(dāng)做xxx類型處理。

回答4:

http中傳的數(shù)據(jù)都是都是字符串,只是服務(wù)器在接受到數(shù)據(jù)時(shí)會(huì)根據(jù)contentType來用不同的方式解析字符串。對(duì)象只能存在于內(nèi)存中,不僅僅是http,所有在網(wǎng)絡(luò)中傳輸?shù)臄?shù)據(jù)都是基于字符串的。

回答5:

首先我不覺得你的抓包有問題,如果你確實(shí)是用的是POST請(qǐng)求的話,從抓包看起來這是個(gè)GET請(qǐng)求,因?yàn)镻OST不會(huì)對(duì)請(qǐng)求參數(shù)做序列化處理

下面說下contentType是啥意思?

ajax的contentType是設(shè)置的http的請(qǐng)求頭,這個(gè)頭的目的是告訴服務(wù)器端,我的請(qǐng)求參數(shù)是什么格式的數(shù)據(jù),你要按照對(duì)應(yīng)的格式去處理,就這樣。默認(rèn)的是 'application/x-www-form-urlencoded; charset=UTF-8',也就是普通的表單提交的格式,當(dāng)然你也可以覆蓋,比如'application/json',這樣服務(wù)端可以直接拿到一個(gè)json請(qǐng)求參數(shù)。而不是一個(gè)一個(gè)的key value

回答6:

這只是修改請(qǐng)求頭中的contentType,和你接受服務(wù)器響應(yīng)是什么內(nèi)容沒有關(guān)系。

你可以加上這個(gè)拿到j(luò)son格式的數(shù)據(jù)。

dataType:'json'

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 欧美精品做人一级爱免费 | 乱淫67194| 成人国产精品视频 | 碰碰人人 | 亚洲精品xxxxx | 欧美国产日韩在线播放 | 日韩美女一级毛片 | 中国一级毛片特级毛片 | 狠狠色丁香久久婷婷综 | 伊大人香蕉久久网 | 色资源二区在线视频 | 日韩在线小视频 | 大尺度福利视频在线观看网址 | 国产l精品国产亚洲区久久 国产tv在线 | 亚洲国产精品免费 | 国产日产欧美a级毛片 | 三级网站在线免费观看 | 免费毛片儿| 好吊操这里只有精品 | 香港aa三级久久三级 | 三级黄色毛片网站 | 91久久久久久久 | 精品久久在线 | 国产成人影院一区二区 | 91年精品国产福利线观看久久 | 中国一级性生活片 | 特级a做爰全过程片 | 亚洲国产精品久久久久秋霞不卡 | 综合网站 | 成人看片黄a免费看视频 | 免费一级做a爰片久久毛片 免费一级做a爰片性色毛片 | 俄罗斯极品美女毛片免费播放 | 日本欧美精品 | 久久久成人啪啪免费网站 | 特黄特黄一级高清免费大片 | 99久久免费观看 | 欧美视频在线观看一区二区 | 欧美大片一区二区三区 | 国产日韩欧美精品一区二区三区 | 露脸 在线 国产 眼镜 | 国产高清一区 |