$. Ajax JSON Data Transfer Method

Source: Internet
Author: User

Front-end CopyCode The 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> No title 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 convenient calling.
Function myajax (){
// Var OBJ = jsondata ();
$. Ajax ({
Type: 'post ',
URL: 'ajax. aspx ',
Data: jsondata (), // you can add a function name directly.
Datatype: 'json ',
Beforesend: beforecall,
Success: callback
});
}
// Encapsulate JSON data for clear code
Function jsondata (){
VaR jsonstr = "({";
Jsonstr + = "\" Name \":";
Jsonstr + = "\" tree \"";
Jsonstr + = ",";
Jsonstr + = "\" ID \":";
Jsonstr + = "\" 123 \"";
Jsonstr + = "})";
Return eval (jsonstr); // The key lies in conversion.
}
// Method before call, failed
Function beforecall (){
$ ('# Wait'). addclass ("show"). append ('calling ...');
// Alert (''); // test whether to call
}
// Callback function
Function callback (data ){
$ ('# Response'). append (data. Name + data. ID );
Certificate ('{wait'}.css ("display", "NONE ");
}
// Onload () event
$ (Function (){
$ ('# Confirm'). Click (myajax );
})
</SCRIPT>
</Head>
<Body>
<Div id = "Confirm"> click </div>
<Div id = "response"> receive background data </div>
<Div id = "wait" class = "hide"> Hello </div>
</Body>
</Html>

copy the Code In the background: protected void page_load (Object sender, eventargs e)
{< br> hashtable ht = new hashtable ();
string name = request. params ["name"]. tostring ();
string birth = request. params ["Birthday"]. tostring ();
If (! String. isnullorempty (name )&&! String. isnullorempty (birth)
{< br> // response. contenttype = "application/JSON";
// response. write (crearejson ("this is OK! ", 1, name, birth);
ht. add ("info", "succeeded");
ht. add ("sta", "status");
ht. add ("name", name);
ht. add ("birth", birth);
response. write (createjsonparams (HT);
}< br> response. end ();
}< br> private string createjsonparams (hashtable items)
{< br> string returnstr = "";
foreach (dictionaryentry item in items)
{< br> returnstr + = "\" "+ item. key. tostring () + "\": \ "" + item. value. tostring () + "\", ";
}< br> return" {"+ returnstr. substring (0, returnstr. length-1) + "}";
}

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.