jQuery Ajax USES instances

  • 2020-05-30 19:21:00
  • OfStack

Jquery encapsulates very well in terms of asynchronous delivery, it's very cumbersome to use AJAX directly, and Jquery greatly simplifies our operations, regardless of browser differences.

$.post, $.get are simple methods. If you want to deal with complex logic, you still need to use jQuery.ajax ().
1. $.ajax 1 format


$.ajax({
   type: 'POST',
   url: url ,
  data: data ,
  success: success ,
  dataType: dataType
});

2. $.ajax parameter description

Parameters to describe
url required. Specify to which URL to send the request.
data optional. Mapping or string values. Specifies the data to be sent to the server along with the request.
success(data, textStatus, jqXHR) is optional. The callback function that executes when the request succeeds.
dataType optional. Specifies the data type of the expected server response.
Intelligent judgment (xml, json, script, or html) is performed by default.
3. $. Some points to note about ajax:

1. There are three main ways of data: html concatenated, json array, form form serialized by serialize(); Specified by dataType, intelligent judgment is not specified.

2.$.ajax only submits form in text if the asynchronous submission is included < file > The upload is not passable, so you need to use $.ajaxSubmit of jquery.form.js

4. $.ajax my practical example


//1.$.ajax with json An asynchronous request for data  
var aj = $.ajax( {  
  url:'productManager_reverseUpdate',//  Jump to  action  
  data:{  
       selRollBack : selRollBack,  
       selOperatorsCode : selOperatorsCode,  
       PROVINCECODE : PROVINCECODE,  
       pass2 : pass2  
  },  
  type:'post',  
  cache:false,  
  dataType:'json',  
  success:function(data) {  
    if(data.msg =="true" ){  
      // view(" Modified successfully! ");  
      alert(" Modified successfully! ");  
      window.location.reload();  
    }else{  
      view(data.msg);  
    }  
   },  
   error : function() {  
     // view(" Abnormal! ");  
     alert(" Abnormal! ");  
   }  
}); 
 
 
//2.$.ajax Serialize an asynchronous request for the contents of a table as a string  
function noTips(){  
  var formParam = $("#form1").serialize();// Serialize the table contents as a string   
  $.ajax({  
    type:'post',    
    url:'Notice_noTipsNotice',  
    data:formParam,  
    cache:false,  
    dataType:'json',  
    success:function(data){  
    }  
  });  
}  
 
 
//3.$.ajax Joining together url Asynchronous request  
var yz=$.ajax({  
   type:'post',  
   url:'validatePwd2_checkPwd2?password2='+password2,  
   data:{},  
   cache:false,  
   dataType:'json',  
   success:function(data){  
     if( data.msg =="false" ) // Server return false , it will validatePassword2 To change the value of pwd2Error , this is asynchronous, need to consider the return time   
     {  
        textPassword2.html("<font color='red'> Incorrect business password! </font>");  
        $("#validatePassword2").val("pwd2Error");  
        checkPassword2 = false;  
        return;  
      }  
   },  
   error:function(){}  
});  
 
 
//4.$.ajax Joining together data Asynchronous request  
$.ajax({   
  url:'<%=request.getContextPath()%>/kc/kc_checkMerNameUnique.action',   
  type:'post',   
  data:'merName='+values,   
  async : false, // The default is true  asynchronous    
  error:function(){   
    alert('error');   
  },   
  success:function(data){   
    $("#"+divs).html(data);   
  } 
}); 


Related articles: