文章詳情頁
$.ajax中contentType: “application/json” 的用法詳解
瀏覽:334日期:2022-06-11 11:07:21
具體內(nèi)容如下所示:
$.ajax({
type: httpMethod,
cache:false,
async:false,
contentType: "application/json; charset=utf-8",
dataType: "json",//返回值類型
url: path+url,
data:jsonData,
success: function(data){
var resultData = "返回碼="+data.status+",響應(yīng)結(jié)果="+data.message+",耗時(shí)="+data.tcost;
layer.msg(resultData,{icon: 1});
},
error : function(xhr, ts, et) {
layer.msg("服務(wù)調(diào)用失敗!", {icon: 2});
}
});
區(qū)分:
contentType: 發(fā)送信息至服務(wù)器時(shí)內(nèi)容編碼類型,簡單說告訴服務(wù)器請求類型的數(shù)據(jù)
默認(rèn)值: "application/x-www-form-urlencoded"
dataType:告訴服務(wù)器,我要想什么類型的數(shù)據(jù),除了常見的json、XML,還可以指定 html、jsonp、script或者text
不使用contentType: “application/json”則data可以是對象
$.ajax({
url: actionurl,
type: "POST",
datType: "JSON",
data: { id: nodeId },
async: false,
success: function () {}
});
使用contentType: “application/json”則data只能是json字符串
$.ajax({
url: actionurl,
type: "POST",
datType: "JSON",
contentType: "application/json"
data: "{"id": " + nodeId +"}",
async: false,
success: function () {}
});
總結(jié)
以上所述是小編給大家介紹的$.ajax中contentType: “application/json” 的用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對網(wǎng)站的支持!
標(biāo)簽:
Ajax
相關(guān)文章:
1. AJAX檢測用戶名是否存在的方法2. PHP下ajax跨域的解決方案之jsonp實(shí)例分析3. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法4. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算5. 通過Ajax請求動(dòng)態(tài)填充頁面數(shù)據(jù)的實(shí)例6. 利用promise及參數(shù)解構(gòu)封裝ajax請求的方法7. AJAX實(shí)現(xiàn)注冊驗(yàn)證用戶名8. 爬取今日頭條Ajax請求9. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法10. ztree+ajax實(shí)現(xiàn)文件樹下載功能
排行榜

網(wǎng)公網(wǎng)安備