Ajax 函數的使用及解釋
碰到了一個data參數傳遞了,後面字符串傳遞的方式好像有問題?後來發現是contentType:"application/json",這個參數的問題,它規定了傳遞給後臺方法的參數必須是json格式即{鍵:'值',鍵:'值'}
$.ajax({
type: "get",
url: "test.jsp",
datatype: "json",
contentType: "application/json",
data:"id=1234",
success: function(result){
alert(result);
}
});
實例
function save(){
if(checkValue()){//判斷真假是否執行
var resturl = "<%=request.getContextPath() %>/rest/zhswleave/saveLeave";//調用後臺方法地址
$.ajax({
url:resturl,
data:{
leaveId:$("input[name=leaveId]").val(),//取數賦值給*
leavePersonId:$("input[name=leavePersonId]").val(),
leavePerson: $("input[name=leavePerson]").val(),
leaveDeptId:$("input[name=leaveDeptId]").val(),
leaveDeptName: $("input[name=leaveDeptName]").val(),
leaveReason:$("textarea[name=leaveReason]").val(),
startDate: $("input[name=startDate]").val(),
endDate: $("input[name=endDate]").val(),
leaveTypeId:$("select[name=leaveType]").val(),
leaveType: $('#leaveType option:selected').text(),
common:$("textarea[name=common]").val(),
leaveState:$("input[name=leaveState]").val(),
leaveNodeNum:$("input[name=leaveNodeNum]").val()
},
type:"post",//方式
async:false,
dataType:'json',//形式
success:function(e){//e爲後臺方法處理後輸出的結果**
if(e.success==true){
msgbox('提示','保存成功!' ,'',null,0,'true');
//alert(e.result);
// initData();
$("input[name=leaveId]").val(e.result);//給隱形字段賦值***
alert("leaveId:"+$("input[name=leaveId]").val());
}
if(e.success==false){
// msgbox('提示','操作失敗!','',null,0,'error');
}
},
error:function(e){
msgbox('提示','操作失敗!','',null,0,'error');
}
});