Jquery+ajax use JSON to return data (1/2)

Source: Internet
Author: User
Tags serialization

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>&nbsp;</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>

Home 1 2 last page
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.