Home  >  Article  >  Web Front-end  >  jQuery Ajax usage example_jquery

jQuery Ajax usage example_jquery

WBOY
WBOYOriginal
2016-05-16 16:03:461167browse

Jquery encapsulates asynchronous submission very well. It is very troublesome to use AJAX directly. Jquery greatly simplifies our operations without considering the differences in browsers.

$.post and $.get are simple methods. If you want to handle complex logic, you still need to use jQuery.ajax()
1. General format of $.ajax

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

2. Parameter description of $.ajax

Parameter Description
url Required. Specifies the URL to which the request should be sent.
data is optional. Map or string value. Specifies the data to be sent to the server with the request.
success(data, textStatus, jqXHR) Optional. The callback function executed when the request is successful.
dataType optional. Specifies the data type of the expected server response.
Intelligent judgment is performed by default (xml, json, script or html).
3. Some things to note about $.ajax:

1. There are three main methods of data, html splicing, json array, form serialized by serialize(); specified by dataType, no intelligent judgment is specified.

2.$.ajax only submits the form in text mode. If the asynchronous submission contains 28897b20adb25fbae118a3f80f538dec, the upload cannot be passed. You need to use $.ajaxSubmit of jquery.form.js

4. My practical application example of $.ajax

//1.$.ajax带json数据的异步请求 
var aj = $.ajax( {  
  url:'productManager_reverseUpdate',// 跳转到 action  
  data:{  
       selRollBack : selRollBack,  
       selOperatorsCode : selOperatorsCode,  
       PROVINCECODE : PROVINCECODE,  
       pass2 : pass2  
  },  
  type:'post',  
  cache:false,  
  dataType:'json',  
  success:function(data) {  
    if(data.msg =="true" ){  
      // view("修改成功!");  
      alert("修改成功!");  
      window.location.reload();  
    }else{  
      view(data.msg);  
    }  
   },  
   error : function() {  
     // view("异常!");  
     alert("异常!");  
   }  
}); 
 
 
//2.$.ajax序列化表格内容为字符串的异步请求 
function noTips(){  
  var formParam = $("#form1").serialize();//序列化表格内容为字符串  
  $.ajax({  
    type:'post',    
    url:'Notice_noTipsNotice',  
    data:formParam,  
    cache:false,  
    dataType:'json',  
    success:function(data){  
    }  
  });  
}  
 
 
//3.$.ajax拼接url的异步请求 
var yz=$.ajax({  
   type:'post',  
   url:'validatePwd2_checkPwd2?password2='+password2,  
   data:{},  
   cache:false,  
   dataType:'json',  
   success:function(data){  
     if( data.msg =="false" ) //服务器返回false,就将validatePassword2的值改为pwd2Error,这是异步,需要考虑返回时间  
     {  
        textPassword2.html("<font color='red'>业务密码不正确!</font>");  
        $("#validatePassword2").val("pwd2Error");  
        checkPassword2 = false;  
        return;  
      }  
   },  
   error:function(){}  
});  
 
 
//4.$.ajax拼接data的异步请求 
$.ajax({   
  url:'<%=request.getContextPath()%>/kc/kc_checkMerNameUnique.action',   
  type:'post',   
  data:'merName='+values,   
  async : false, //默认为true 异步   
  error:function(){   
    alert('error');   
  },   
  success:function(data){   
    $("#"+divs).html(data);   
  } 
}); 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn