文章詳情頁
$.ajax中contentType: “application/json” 的用法詳解
瀏覽:319日期: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” 的用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對網(wǎng)站的支持!
標(biāo)簽:
Ajax
相關(guān)文章:
1. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法2. layui的checbox在Ajax局部刷新下的設(shè)置方法3. 解決SpringMVC、tomcat、Intellij idea、ajax中文亂碼問題4. JSP Tag Library-AjaxTags 1.0, released5. PHP與 xajax使用方法說明6. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. Webform 內(nèi)置對象 Session對象、Application全局對象,ViewState詳細(xì)介紹8. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法9. ajax使用formdata上傳文件流10. Ajax提交post請求案例分析
排行榜

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