Ajax Response Processing page processpage.asp Tutorial X.cs
Using System.web.script.serialization;
Using Modelgroup.model;
public partial class Admin_processpage:system.web.ui.page
{
Web Effects Serializer Serializer = new JavaScriptSerializer ();
protected void Page_Load (object sender, EventArgs e)
{
String name = request.querystring["Txt_search"].tostring ();
if (Name.equals ("AAA")) {
list<zen_tag> Zentag = new list<zen_tag> ();
Zentag.add (New Zen_tag {
tag_id = 1,
tag_name = "Nicholas",
Url_show = "Nicholas",
First_letter = "N",
Count = 500,
Create_time = "2009-10-10 10:10"
});
String jsonstr = Serializer.serialize (Zentag);
Response.Clear ();
Response.Write (JSONSTR);
Response.End ();
}
}
}
Ajax Request Page Tagmanagement.aspx
<script type= "Text/javascript" language= "JavaScript" >
$ (document). Ready (function () {
$ ("#btn_search"). Click (function () {
$.ajax ({
URL: "Processpage.aspx",
Type: "Get",
DataType: "JSON",
Data: "Txt_search=" + Escape ($ ("#txt_search"). Val ()),
Success:function (data) {
Alert (data[0].tag_name+ "---" +data[0].count);
},
Error:function () {alert ("O no~~~");}
});
});
});
</script>
asp.net tutorial 3.5 already supports JSON and System.web.extensions.dll in its extensions.
Using System.web.script.serialization; Reference this namespace first! The serialize () method within the namespace enables you to serialize data objects into JSON format.
For example, if I now have an entity class object or an array arrayinfo need to serialize to JSON format, then just call the Serialize () method to
Let's look at a detailed analysis and example of jquery JSON return data
jquery delivers data to the server via post (without plug-ins):
<title>untitled page</title>
<script language= "javascript" type= "Text/javascript" src= "Js/jquery-1.3.1.js" ></script>
<script language= "javascript" type= "Text/javascript" src= "Js/json2.js" ></script>
<script language= "javascript" type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#nameedit"). Hide ();
$ ("#aedit"). Click (function () {
var value=$ ("#nameview"). Text ();
alert (value);
$ ("#txtrename"). val (value);
$ ("#aedit"). empty ();
$ ("#nameview"). Hide ();
$ ("#nameedit"). Show ();
});
$ ("#btnsave"). Click (function () {
var value=$ ("#txtrename"). Val ();
var obj=createobj (value);
var objstr=json.stringify (obj);
$.post ("Savedata.aspx", objstr,function (response) {//using post to pass data, parameters: URLs, data, functions to be processed when returned
$ ("#nameview"). Text (response);
$ ("#aedit"). Text ("edit"). Show ();
$ ("#nameview"). Show ();
$ ("#nameedit"). Hide ();
});
});
function Createobj (v)//used to create a JSON object
{
Var test=new object ();
Test.newname=v;
Test.data=getdata ()//
return test;
}
function GetData ()
{
Var data=new object ();
Data.first= "FFF";
Data.second= "SSS";
return data;
}
$ ("#btncancel"). Click (function () {
$ ("#aedit"). Text ("edit"). Show ();
$ ("#nameview"). Show ();
$ ("#nameedit"). Hide ();
});
});
</script>
<body>
<div id= "Showdiv" >
<div id= "Aeditdiv" style= "Background-color: #afadae; width:200px; " >
<a id= "Aedit" >edit</a> </div>
<div id= "Nameview" >
Fnsndgfn</div>
<div id= "Nameedit" >
<input type= "text" id= "Txtrename"/><input id= "btnsave" type= "button" value= "Save"/><input
Id= "Btncancel" type= "button" value= "Cancel"/>
</div>
</div>
</body>