Use $.ajax({url,data,type,dataType,success}) in the method of js;

The use and explanation of Ajax functions

I encountered a data parameter passed, and there seems to be a problem with the way the string is passed behind? Later, it was found that it was contentType: “application/json”, the problem with this parameter, it stipulated that the parameter passed to the background method must be in json format, namely {key: ‘value’, key: ‘value’}

$.ajax({ 
    type: "get", 
    url: "test.jsp", 
    datatype: "json", 
    contentType: "application/json",

data:"id=1234", 
    success: function(result){ 
       alert(result); 
    } 
  });

Example

function save() {
     if (checkValue()){ //Determine whether to execute true or false 
    var resturl = "<%=request.getContextPath() %>/rest/zhswleave/saveLeave" ; //Call background method address 
    $.ajax({
            url:resturl,
        data:{
          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" , //method 
        async : false ,
         dataType : 'json' , //form 
        success: function ( e ) { //e is the output result after processing by the background method** 
            if (e.success== true ){
                msgbox( 'Alert' , 'Save successfully!' , '' , null , 0 , 'true' );
                 //alert(e.result); 
                // initData(); 
                $( "input[name=leaveId]" ) .val(e.result); //Assign value to invisible field*** 
                alert( "leaveId:" +$( "input[name=leaveId]" ).val());
            }
            if (e.success== false ){
                 // msgbox('prompt','operation failed!','',null,0,'error');
            }
    },
       error:function(e){
           msgbox( 'Prompt' , 'Operation failed!' , '' , null , 0 , 'error' );
       }

    });

Leave a Comment

Your email address will not be published. Required fields are marked *