In JQuery, AJAX has three implementation methods: $. ajax (), $. post, $. get ().
First, let's look at $. get ():
Copy codeThe Code is as follows: $. get ("test. jsp ",
{Name: "cssrain", time: "2008/01/21"}, // data to be transmitted
Function (data ){
Alert ("returned data:" + data );
}
)
Then check $. post ():
The format is the same as that of $. get.Copy codeThe Code is as follows: $. post ("test. jsp ",
{Name: "cssrain", time: "2008/01/21"}, // data to be transmitted
Function (data ){
Alert ("returned data:" + data );
}
)
The difference between the above two methods should be that the request method is different (A get and a post ).
Finally, let's look at $. ajax ():Copy codeThe Code is as follows: $. ajax ({
Url: 'Accept. jsp ',
Type: 'post', // data transmission method
DataType: 'html', // accept the data format (there are many commonly used formats such as html, xml, js, and json)
Data: 'text = '+ $ ("# name"). val () +' & date = '+ new Date (), // data to be passed
Error: function () {// failed
Alert ('error loading document ');
},
Success: function (msg) {// success
Alert ("Data Saved:" + msg );
}
});
Instance
The front-end jsp code is as follows :...
Number of votes:Copy codeThe Code is as follows: <span id = "I <% = id %>"> <% = vote_number %> </span> <br/>
<A onclick = myvote (<% = id %>); href = 'javascript:; '"> vote </a>
...
The js Code is as follows:Copy codeThe Code is as follows: function myvote (id ){
$. Post ("vote. jsp", {id: id },
Function (data ){
Eval ("var data =" + data );
If (data. issucc = "0 "){
Alert (data. mess)
} Else {
// Alert ("Update page ");
$ ("# I" using data.myid).html (data. votenum );
}
});
}
The returned data is in json format.
The json data returned by the background is as follows:
{Issucc:, mess: "", votenum:, myid :}
Issucc: successful or not
Mess: information, mainly error information, such as No Logon or exceeding the limit
Votenum: Total number of votes received after voting
Myid: The voting id, used to update the number of votes on the page
One registered login instance
Js
The type returned by login. jsp is in text format. If it is correct, it is "OK". If it is incorrect, it is
"Error ".Copy codeThe Code is as follows: var userName;
Var password;
Var result;
$ (Document). ready (function (){
$ ("# Load"). hide ();
$ ("# Success"). hide ();
$ ("# Error"). hide ();
});
$ (Document). ready (function (){
$ ("# Button"). click (function (){
$ ("# Error"). hide ();
$ ("# Load"). show ("slow ");
UserName = $ ("# userName"). val ();
Password = $ ("# password"). val ();
$. Ajax ({type: "post ",
Url: "login. jsp ",
DataType: "html ",
Data: "userName =" + userName + "& password =" + password,
Success: function (result ){
Var res = String ($. trim (result ));
If (res = "OK "){
$ ("# MyTable"). hide ("slow ");
$ ("# Success"). show ("slow ");
} Else if (res = "error "){
$ ("# Error"). show ("slow ");
$ ("# Load"). hide ("slow ");
} Else {
Alert ("Return exception ");}
}
});
});
});
Jsp page
First responseText formatCopy codeThe Code is as follows: <% @ page language = "java" pageEncoding = "gb2312" %>
<%
String userName = request. getParameter ("userName ");
String password = request. getParameter ("password ");
If (password. equals ("longleg") & userName. equals ("thy ")){
Out. print ("OK ");
} Else {out. print ("error ");}
%>