具體內(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ù)器請(qǐng)求類型的數(shù)據(jù)
默認(rèn)值: "application/x-www-form-urlencoded"
dataType:告訴服務(wù)器,我要想什么類型的數(shù)據(jù),除了常見的json、XML,還可以指定 html、jsonp、script或者text
不使用contentType: “application/json”則data可以是對(duì)象
$.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” 的用法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選