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>