Copy codeThe 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> example of jQueryAjaxJson value </title>
<Script type = "text/javascript" src = "Scripts/jquery-1.4.4.min.js"> </script>
<Script type = "text/javascript">
$ (Function (){
JsonAjax ("AjaxQuery. aspx", "type = json", "json", callBack );
JsonAjax ("AjaxQuery. aspx", "id = 1 & name = 2 & type = text", "text", callBackTxt );
});
Function callBack (data ){
$ ("# Ddd" ).html ('');
Var json = eval (data); // Array
$. Each (json, function (index, item ){
// Obtain data cyclically
Var name = json [index]. Name;
Var age = json [index]. Age;
Var sex = json [index]. Sex;
$ ("# Ddd" ).html ($ ("# ddd" ).html () + "<br>" + name + "" + age + "" + sex + "<br/> ");
});
};
Function callBackTxt (data ){
$ ("# Ccc" pai.html (data );
};
/**
* Ajax post submission
* @ Param url
* @ Param
* @ Param datat: html, json, and text
* @ Param callback function
* @ Return
*/
Function jsonAjax (url, param, datat, callback ){
$. Ajax ({
Type: "post ",
Url: url,
Data: param,
DataType: datat,
Success: callback,
Error: function (){
JQuery. fn. mBox ({
Message: 'recovery failed'
});
}
});
}
</Script>
</Head>
<Body>
<Span id = "ccc"> </span>
<Span id = "ddd"> </span>
</Body>
</Html>
Copy codeThe Code is as follows:
Using System;
// Add
Using System. Web. Script. Serialization;
Using System. Collections. Generic;
Public partial class AjaxQuery: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
// Data simulation, for reference only
String messgage = string. Empty;
String id = Request ["id"];
String name = Request ["name"];
String gettype = Request ["type"];
If (gettype = "text ")
{
Messgage = (id = "1" & name = "2 ")? "OK meets the condition": "sorry does not meet the condition ";
}
Else if (gettype = "json ")
{
List <Student> list = new List <Student> ();
For (int I = 0; I <50; I ++)
{
Student a = new Student ();
A. Name = "Zhang San" + I;
A. Age = I;
A. Sex = "male ";
List. Add ();
}
Messgage = new JavaScriptSerializer (). Serialize (list );
}
Else
{}
Response. Write (messgage );
Response. End ();
}
}
Public struct Student
{
Public string Name;
Public int Age;
Public string Sex;
}
}