$.post, $.get is a simple method, if you want to deal with complex logic, still need to use the Jquery.ajax ()
I. GENERAL format of $.ajax
$.ajax ({
Type: ' POST ',
Url:url,
Data:data,
Success:success,
Datatype:datatype
});
Second, the parameter description of $.ajax
Parameter description
URL required. Specifies which URL to send the request to.
Data is optional. The map or string value. Specifies the data that is sent to the server along with the request.
Success (data, Textstatus, JQXHR) is optional. The callback function to execute when the request succeeds.
DataType is optional. Specifies the data type of the expected server response.
The default is to perform smart judgments (XML, JSON, script, or HTML).
Three, $.ajax need to pay attention to some places:
There are three main ways of 1.data, HTML splicing, JSON array, form form is serialized by Serialize (), and no intelligent judgment is specified by datatype.
2.$.ajax only submits form in text mode, if the asynchronous commit contains <file> uploads are not passed over, you need to use Jquery.form.js $.ajaxsubmit
Iv. $.ajax Examples of my practical application
JS Code
- 1.$.ajax asynchronous requests 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 ("modified successfully! ");
- Window.location.reload ();
- }else{
- View (DATA.MSG);
- }
- },
- Error: function () {
- //view ("Exception! ");
- Alert ("Exception! ");
- }
- });
- 2.$.ajax asynchronous requests that serialize the contents of a table to a string
- function Notips () {
- var Formparam = $ ("#form1"). Serialize (); Serialize table contents as strings
- $.ajax ({
- Type:' post ',
- URL:' notice_notipsnotice ',
- Data:formparam,
- Cache:false,
- DataType:' json ',
- Success:function (data) {
- }
- });
- }
- 3.$.ajax asynchronous requests for stitching URLs
- var Yz=$.ajax ({
- Type:' post ',
- URL:' http://www.nuoya118.com= ' +password2,
- data:{},
- Cache:false,
- DataType:' json ',
- Success:function (data) {
- if (data.msg = ="false") //The server returns false to change the value of ValidatePassword2 to Pwd2error, which is asynchronous and takes into account the return time
- {
- Textpassword2.html ("<font color= ' red ' > Business password is incorrect! </font> ");
- $ ("#validatePassword2"). Val ("pwd2error");
- CheckPassword2 = false;
- return;
- }
- },
- Error:function () {}
- });
- 4.$.ajax asynchronous request to splice data
- $.ajax ({
- URL:' <%=request.getcontextpath ()%>/kc/kc_checkmernameunique.action ',
- Type:' post ',
- Data:' mername= ' +values,
- Async: false, //default is True async
- Error:function () {
- Alert (' error ');
- },
- Success:function (data) {
- $ ("#" +divs). HTML (data);
- }
- });
1.$.ajax asynchronous request with JSON data var AJ = $.ajax ({url: ' productmanager_reverseupdate ',//Jump to action data:{s Elrollback: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 ("Exception! "); Alert ("Exception! "); }});//2.$.ajax the asynchronous Request function Notips () {var Formparam = $ ("#form1") of the serialized table contents as a string. Serialize ();//The serialized table contents are string $.ajax ( {type: ' post ', url: ' Notice_notipsnotice ', Data:formparam, Cache:false, DA Tatype: ' JSON ', success:function (data) {}}); }//3.$.ajax the asynchronous request for the concatenation URL var yz=$.ajax ({ Type: ' Post ', url: ' validatepwd2_checkpwd2?password2= ' +password2, data:{}, Cache:false, Dataty PE: ' JSON ', success:function (data) {if (data.msg = = "false")///The server returns false to change the value of ValidatePassword2 to Pwd2error , this is asynchronous, need to consider return time {textpassword2.html ("<font color= ' red ' > Business password is incorrect! </font> "); $ ("#validatePassword2"). Val ("Pwd2error"); CheckPassword2 = false; Return }}, Error:function () {}}); 4.$.ajax Stitching Data Asynchronous request $.ajax ({URL: ' <%=request.getcontextpath ()%>/kc/kc_checkmernameunique.action ', type: ' Post ', data: ' Mername= ' +values, Async:false,//default True Async Error:function () {alert (' Error '); }, Success:function (data) {$ ("#" +divs). HTML (data); }});
JQuery Ajax instances ($.ajax, $.post, $.get)