JQuery ajax () method, jqueryajax Method

Source: Internet
Author: User

JQuery ajax () method, jqueryajax Method

$. Ajax () method:

  


* There are three main ways to send data:
* 1. json array (Recommendation 1)
* 2. url Splicing
* 3. Form serialization serialize (recommended 2)

 

(1) json array (Recommendation 1)

$ (Function () {var myClick = function () {var userName = $ (). val (); var myDate = {userName: usrName}; var myFunction = function (result) {alert (result)}; var eeorFunction = function () {alert ("exception occurred")}; $. ajax ({url: 'B', // The type of the request address sent by Servlet B: 'get',/* the default access Servlet B Request Method (post or GET) is get. Note that other http request methods, such as put and delete, can also be used, but are only supported by some browsers. */Date: myDate, // submit the information to the Servlet data dateType: 'text', // callback function after the return type is text success: myFunction, // success, result indicates the content returned by the server: error: errorFunction, timeout: 2000

/*
$. Ajax () is a common method.
*/}) ;}$ ("# Img "). click (myClick );}); <body> <form> <input id = "userName" name = "userName" value = "xiaoming"> <br> </form> </body>

 

 

(2) url Splicing

$ (Function () {var myClick = function () {var userName = $ (). val (); var myDate = {userName: usrName}; var myFunction = function (result) {alert (result )};
/* Var eeorFunction = function () {alert ("exception occurred ")};*/

Var errorFunction = function (XMLHttpRequest, textStatus ){
// Alert ('error occurred ');
If (XMLHttpRequest. status = 404 ){
Alert ('server not found [404] ');
} Else if (XMLHttpRequest. status = 500 ){
Alert ('server busy, please try again later [500] ');
} Else {
Alert ('server error ['+ XMLHttpRequest. status +'] ');
}

} $. Ajax ({url: 'B? UserName = '+ $ ("# userName"). val (), // the target is the request address sent by Servlet B.
Type: 'get',/* the default access Servlet B Request Method (post or GET) is get. Note that other http request methods, such as put and delete, can also be used, but are only supported by some browsers. */Date: myDate, // submit the information to the Servlet data dateType: 'text', // callback function after the return type is text success: myFunction, // success, result indicates the content returned by the server: error: errorFunction, timeout: 2000}) ;}$ ("# img "). click (myClick );}); <body> <form> <input id = "userName" name = "userName" value = "xiaoming"> <br> </form> </body>

 

(3) form serialization serialize (Recommendation 2)

$(function(){

Var kk = $ ("# userName"). serialize ();
Var tt = $ ("# frm1"). serialize ();
// Alert (t// Var myClick = function// Var userName = $ (). val ();

// Var myDate = {userName: usrName }; 

Var myFunction = function (result) {alert (result )};
/* Var eeorFunction = function () {alert ("exception occurred")}; */var errorFunction = function (XMLHttpRequest, textStatus) {// alert ('error occurred '); if (XMLHttpRequest. status = 404) {alert ('server [404] Not found ');} else if (XMLHttpRequest. status = 500) {alert ('server busy, please try again later [500] ');} else {alert ('server error [' + XMLHttpRequest. status + ']') ;}}$. ajax ({url: 'B? UserName = '+ $ ("# userName "). val (), // The type of the request address sent by Servlet B: 'get', And the/* method for accessing Servlet B requests (post or GET) is get by default. Note that other http request methods, such as put and delete, can also be used, but are only supported by some browsers. */Date: myDate, // submit the information to the Servlet data dateType: 'text', // callback function after the return type is text success: myFunction, // success, result indicates the content returned by the server: error: errorFunction, timeout: 2000}) ;}$ ("# img "). click (myClick );});<Body> <form> <input id = "userName" name = "userName" value = "xiaoming"> <br>
<Input id = "sex" name = "sex" value = "male"> <br>
  </Form> </body>

 

Related Article

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.