具體內(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)站的支持!
您可能感興趣的文章:- 跨域解決之JSONP和CORS的詳細(xì)介紹
- JSON生成Form表單的方法示例
- 簡單說說angular.json文件的使用
- JavaScript根據(jù)json生成html表格的示例代碼
- MongoDB使用mongoexport和mongoimport命令,批量導(dǎo)出和導(dǎo)入JSON數(shù)據(jù)到同一張表的實(shí)例
- nodejs讀取本地中文json文件出現(xiàn)亂碼解決方法
- Go語言的JSON處理詳解
- JavaScript JSON使用原理及注意事項(xiàng)