Front-end
CopyCode The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
<SCRIPT type = "text/JavaScript" src = "jquery/jquery-1.2.6.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
// This method encapsulates the Ajax Method for convenient calling.
Function myajax (){
// Use this method to transmit data
VaR arr = {
"Name": "Tree ",
"ID": "123"
};
$. Get (
"Ajax. aspx ",
Arr,
Callback,
"JSON"
);
}
// Callback function
Function callback (data ){
$ ('# Response'). append (data. Name + data. ID );
Certificate ('{wait'}.css ("display", "NONE ");
}
// Onload () event
$ (Function (){
$ ('# Confirm'). Click (myajax );
})
// Encapsulate JSON data. This method does not work for code clarity.
/* It seems that the array format is incorrect.
VaR datastr = jsondata ();
Alert (datastr );
*/
Function jsondata (){
VaR jsonstr = "";
Jsonstr + = "\" Name \":";
Jsonstr + = "\" tree \"";
Jsonstr + = ",";
Jsonstr + = "\" ID \":";
Jsonstr + = "\" 123 \"";
Jsonstr + = "}";
Return jsonstr;
}
</SCRIPT>
</Head>
<Body>
<Div id = "Confirm"> click </div>
<Div id = "response"> receive background data </div>
<Div id = "wait" class = "hide"> Hello </div>
</Body>
</Html>
Background
Copy codeThe Code is as follows: public partial class Ajax: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Response. Write ("hello" + request ["name"]);
Hashtable ht = new hashtable ();
Ht. Add ("name", request. Params ["name"]);
Ht. Add ("ID", request. Params ["ID"]);
Response. Write (createjsonparams (HT ));
Response. End ();
}
Private string createjsonparams (hashtable items)
{
String returnstr = "";
Foreach (dictionaryentry item in items)
{
Returnstr + = "\" "+ item. Key. tostring () +" \ ": \" "+ item. value. tostring () + "\",";
}
Return "{" + returnstr. substring (0, returnstr. Length-1) + "}";
}
}