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>
<Style type = "text/CSS">
. Show {display: block ;}
. Hide {display: none ;}
</Style>
<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 (){
// Var OBJ = jsondata ();
$. Ajax ({
Type: 'post ',
URL: 'ajax. aspx ',
Data: jsondata (), // you can add a function name directly.
Datatype: 'json ',
Beforesend: beforecall,
Success: callback
});
}
// Encapsulate JSON data for clear code
Function jsondata (){
VaR jsonstr = "({";
Jsonstr + = "\" Name \":";
Jsonstr + = "\" tree \"";
Jsonstr + = ",";
Jsonstr + = "\" ID \":";
Jsonstr + = "\" 123 \"";
Jsonstr + = "})";
Return eval (jsonstr); // The key lies in conversion.
}
// Method before call, failed
Function beforecall (){
$ ('# Wait'). addclass ("show"). append ('calling ...');
// Alert (''); // test whether to call
}
// Callback function
Function callback (data ){
$ ('# Response'). append (data. Name + data. ID );
Certificate ('{wait'}.css ("display", "NONE ");
}
// Onload () event
$ (Function (){
$ ('# Confirm'). Click (myajax );
})
</SCRIPT>
</Head>
<Body>
<Div id = "Confirm"> click </div>
<Div id = "response"> receive background data </div>
<Div id = "wait" class = "hide"> Hello </div>
</Body>
</Html>
copy the Code In the background: protected void page_load (Object sender, eventargs e)
{< br> hashtable ht = new hashtable ();
string name = request. params ["name"]. tostring ();
string birth = request. params ["Birthday"]. tostring ();
If (! String. isnullorempty (name )&&! String. isnullorempty (birth)
{< br> // response. contenttype = "application/JSON";
// response. write (crearejson ("this is OK! ", 1, name, birth);
ht. add ("info", "succeeded");
ht. add ("sta", "status");
ht. add ("name", name);
ht. add ("birth", birth);
response. write (createjsonparams (HT);
}< br> response. end ();
}< br> private string createjsonparams (hashtable items)
{< br> string returnstr = "";
foreach (dictionaryentry item in items)
{< br> returnstr + = "\" "+ item. key. tostring () + "\": \ "" + item. value. tostring () + "\", ";
}< br> return" {"+ returnstr. substring (0, returnstr. length-1) + "}";
}