Home » jQuery » JQuery Ajax instance ($.ajax, $.post, $.get)

JQuery Ajax instance ($.ajax, $.post, $.get)

Jquery in the asynchronous submission package is very good, direct use of AJAX is very troublesome, Jquery greatly simplifies our operation, without considering the browser's surprise,.



$.post and $.get are some simple methods, and if you want to handle complex logic, you still need to use jQuery.ajax ()



1, $.ajax's general format,


$.ajax ({


, type:,'POST',


, url:, url,


, data:, data,


, success:, success,


dataType: dataType


});



two, the parameter description

of $.ajax
Parameter description






to send the request to



that is sent to the server along with the request










url must. Specify which URL.
data optional. Mapping or string values. Specify the data.
success (data, textStatus, jqXHR) optional. Callback function executed when request succeeds..
dataType

optional. Specifies the data type of the expected server response.


defaults to smart judgment (XML, JSON, script, or HTML).




three, $.ajax, something to note:


There are three main ways to

1.data: HTML, JSON, dataType, form, serialize, and.


2.$.ajax only submits form in text mode, and if asynchronous submission contains , upload is passed, and you need to use jquery.form.js's $.ajaxSubmit


four, $.ajax, my actual application example,


 //1.$.ajax asynchronous request with JSON 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 (modify success!);
Window.location.reload ();
}else{
View (data.msg);
}
},
Error: function () {
/ / view ("abnormal!");
Alert ("exception!");
}
});
//2.$.ajax serialize the table contents asynchronously requests for Strings
Function, noTips () {
Var formParam = $("#form1") (.Serialize); / / String serialization table of contents
$.ajax ({)
Type:' post',
Url:' Notice_noTipsNotice',
Data:formParam,
Cache:false,
DataType:' json',
Success:function (data) {
}
});
}
//3.$.ajax splicing asynchronous requests from URL
Var yz=$.ajax ({)
Type:' post',
Url:' validatePwd2_checkPwd2; password2=' +password2,
Data:{},
Cache:false,
DataType:' json',
Success:function (data) {
If (data.msg = = "false") / / server returns false, the value of validatePassword2 will be changed to pwd2Error, which is asynchronous, need to consider the return time
{
TextPassword2.html (" business password is incorrect" ");
$("#validatePassword2").Val ("pwd2Error");
CheckPassword2 = false;
Return;
}
},
Error:function () {}
});
//4.$.ajax splicing asynchronous requests from data
$.ajax ({)
Url:' <%=request.getContextPath ()%>/kc/kc_checkMerNameUnique.action',
Type:' post',
Data:' merName=' +values,
Async: false, true / / default is asynchronous
Error:function () {
Alert (' error');
},
Success:function (data) {
$("#" +divs).Html (data);
}
};




Latest