Home > Web Front-end > JS Tutorial > body text

jQuery$.post() method, with ajax example code attached

yulia
Release: 2018-09-13 15:14:39
Original
1520 people have browsed it

This article mainly introduces the $.post() method in jquery. Jquery is very good at encapsulating asynchronous submission. It is very troublesome to use AJAX directly. Jquery greatly simplifies our operations without considering the differences in browsers. Friends who need it can refer to it, I hope it can help you.
$.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
});
Copy after login

2. Parameter description of $.ajax

Parameter description
url - required. Specifies the URL to which the request should be sent.
data - 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 pay attention to in $.ajax

1. There are three main methods of data, html splicing, json array, form form serialize() sequence oriented; specified by dataType, no intelligent judgment is specified.
2.$.ajax only submits the form in text mode. If the asynchronous submission contains , the upload cannot be passed. You need to use $.ajaxSubmit of jquery.form.js

4. Practical application examples 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=&#39;red&#39;>业务密码不正确!</font>");    
               $("#validatePassword2").val("pwd2Error");    
               checkPassword2 = false;    
               return;    
           }    
      },    
      error:function(){}    
});   
  
  
//4.$.ajax拼接data的异步请求  
$.ajax({     
    url:&#39;<%=request.getContextPath()%>/kc/kc_checkMerNameUnique.action&#39;,     
    type:&#39;post&#39;,     
    data:&#39;merName=&#39;+values,     
    async : false, //默认为true 异步     
    error:function(){     
       alert(&#39;error&#39;);     
    },     
    success:function(data){     
       $("#"+divs).html(data);     
    }  
});
Copy after login

The above is the detailed content of jQuery$.post() method, with ajax example code attached. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!