Home > Article > Web Front-end > jQuery$.post() method, with ajax example code attached
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 });
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 28897b20adb25fbae118a3f80f538dec, 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='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); } });
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!