文章詳情頁
$.ajax中contentType: “application/json” 的用法詳解
瀏覽:242日期: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+",耗時="+data.tcost; layer.msg(resultData,{icon: 1}); }, error : function(xhr, ts, et) { layer.msg("服務(wù)調(diào)用失敗!", {icon: 2}); } });
區(qū)分:
contentType: 發(fā)送信息至服務(wù)器時內(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” 的用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對網(wǎng)站的支持!
標(biāo)簽:
Ajax
相關(guān)文章:
1. Ajax報錯400的參考解決辦法2. Ajax實(shí)現(xiàn)二級聯(lián)動菜單3. ajax使用formdata上傳文件流4. 告別AJAX實(shí)現(xiàn)無刷新提交表單5. Java使用Ajax異步上傳文件6. 基于ajax后臺返回的數(shù)據(jù)為空前臺顯示出現(xiàn)undefined的解決方法7. layui Ajax請求給下拉框賦值的實(shí)例8. springboot如何讀取application.yml文件9. SpringBoot基于Shiro處理ajax請求代碼實(shí)例10. 聊一聊數(shù)據(jù)請求中Ajax、Fetch及Axios的區(qū)別
排行榜
