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>
<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 () {
Passing data in this way
var arr={
"Name": "Tree",
"id": "123"
};
$.get (
"Ajax.aspx",
Arr
Callback
"JSON"
);
}
callback function
function callback (data) {
$ (' #response '). Append (data.name+data.id);
$ (' #wait '). CSS ("display", "none");
}
OnLoad () event
$ (function () {
$ (' #confirm '). Click (Myajax);
})
Encapsulates the JSON data, and for the code to be clear, this approach doesn't work.
/* Seems to be because the array format is not correct.
var datastr=jsondata ();
alert (DATASTR);
*/
function Jsondata () {
var jsonstr= "";
jsonstr+= "\" name\ ":";
jsonstr+= "\" Tree\ "";
jsonstr+= ",";
jsonstr+= "\" id\ ":";
jsonstr+= "\" 123\ "";
jsonstr+= "}";
return jsonstr;
}
</script>
<body>
<div id= "Confirm" > Click </div>
<div id= "Response" > Receive background Data </div>
<div id= "Wait" class= "Hide" >hello</div>
</body>
Backstage
Copy Code code 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) + "}";
}
}