Front desk
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled 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 easy invocation.
function Myajax () {
var obj=jsondata ();
$.ajax ({
Type: ' Post ',
URL: ' ajax.aspx ',
Data:jsondata (),//can be directly added to a function name.
DataType: ' JSON ',
Beforesend:beforecall,
Success:callback
});
}
Encapsulates JSON data for code clarity
function Jsondata () {
var jsonstr= "({";
jsonstr+= "\" name\ ":";
jsonstr+= "\" Tree\ "";
jsonstr+= ",";
jsonstr+= "\" id\ ":";
jsonstr+= "\" 123\ "";
jsonstr+= "})";
Return eval (JSONSTR);/The key is the conversion.
}
Pre-Invoke method, unsuccessful
function Beforecall () {
$ (' #wait '). AddClass ("Show"). Append (' pull in ... ');
Alert (');/whether the test calls the
}
callback function
function callback (data) {
$ (' #response '). Append (data.name+data.id);
$ (' #wait '). CSS ("display", "none");
}
OnLoad () event
$ (function () {
$ (' #confirm '). Click (Myajax);
})
</script>
<body>
<div id= "Confirm" > Click </div>
<div id= "Response" > Receive background Data </div>
<div id= "Wait" class= "Hide" >hello</div>
</body>
Background
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Hashtable HT = new Hashtable ();
String name = request.params["Name"]. ToString ();
String birth = request.params["Birthday"]. ToString ();
if (!string. IsNullOrEmpty (name) &&!string. IsNullOrEmpty (birth))
{
//response.contenttype = "Application/json";
//response.write (Crearejson ("This is ok!", 1, name, birth));
Ht. ADD ("Info", "successful");
Ht. ADD ("STA", "state");
Ht. ADD ("name", name);
Ht. ADD ("Birth", birth);
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) + "}";
}