JQuery Ajax examples: $. ajax, $. post, $. get, jquery. ajax

Source: Internet
Author: User

JQuery Ajax examples: $. ajax, $. post, $. get, jquery. ajax
Jquery is well encapsulated in asynchronous submission. It is very troublesome to directly use AJAX to handle compatibility issues between browsers. Jquery greatly simplifies these operations, you don't have to worry about the browser. You can use it directly!
$. Post and $. get are some simple methods. To process complicated logic, you still need to use jQuery. ajax ()
 
I. general format of $. ajax
$. Ajax ({
Type: 'post ',
Url: url,
Data: data,
Success: success,
DataType: dataType
});
 
2. $. ajax parameter description
Parameter description
The url is required. Specifies the URL to which the request is sent.
Data is optional. Ing or string value. Specifies the data sent to the server together with the request.
Success (data, textStatus, jqXHR) is optional. The callback function executed when the request is successful.
DataType
Optional. Specifies the expected server response data type.
Intelligent judgment (xml, json, script, or html) is performed by default ).
 
Iii. Notes for $. ajax:
1. There are three main methods of data: html splicing, json array, form serialized by serialize (); specified by dataType, no smart judgment is specified.
2. $. ajax only submits the form in text mode. If the asynchronous submission contains <file> upload is not passed, you need to use $. ajaxSubmit of jquery. form. js.


Iv. Examples of my actual applications of $. ajax
Js Code collection code
// 1. $. ajax asynchronous request with json data
Var aj = $. ajax ({
Url: 'leyangjuntest. php', // 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 serialize asynchronous requests whose table content is a string
Function noTips (){
Var formParam = $ ("# form1"). serialize (); // serialize the table content as a string
$. Ajax ({
Type: 'post ',
Url: 'notice _ noTipsNotice ',
Data: formParam,
Cache: false,
DataType: 'json ',
Success: function (data ){
}
});
}


// 3. $. asynchronous request for url Splicing
Var yz = $. ajax ({
Type: 'post ',
Url: 'leyangjuntest. php? Password2 = '+ password2,
Data :{},
Cache: false,
DataType: 'json ',
Success: function (data ){
If (data. msg = "false") // if the server returns false, change the value of validatePassword2 to pwd2Error. This is asynchronous and the return time must be considered.
{
TextPassword2.html ("<font color = 'red'> incorrect business password! </Font> ");
$ ("# ValidatePassword2"). val ("pwd2Error ");
CheckPassword2 = false;
Return;
}
},
Error: function (){}
});


// 4. $. ajax Concatenates the asynchronous request of data
$. Ajax ({
Url: '<% = request. getContextPath () %>/kc/kc_checkMerNameUnique.action ',
Type: 'post ',
Data: 'mername = '+ values,
Async: false, // The default value is true asynchronous.
Error: function (){
Alert ('error ');
},
Success: function (data ){
$ ("#" Nvidivs#.html (data );
}
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.