jquery $.ajax JSON data transfer method

Source: Internet
Author: User
protected void Page_Load (object sender, EventArgs e)
{
Hashtable ht = new Hashtable ();
String name = request.params["Name"].tostring ();
String birth = request.params["Birthday"].tostring ();
if (!string.isnullorempty (name) &&!string.isnullorempty (birth))
{
//response.contenttype = AP Plication/json ";
//response.write (Crearejson ("This is ok!", 1, name, birth));
Ht.add ("info", "successful");
Ht.add ("STA", "state");
Ht.add ("name", name);
Ht.add ("Birth", birth);
Response.Write (Createjsonparams (HT));
}
Response.End ();
}
Private String Createjsonparams (hashtable items)
{
String returnstr = ";
foreach (DictionaryEntry item in items)
{
Returnstr + = "" + item.key.tostring () + "": "+ item.value.to String () + "", ";
}
Return "{" + returnstr.substring (0,returnstr.length-1) + "}";
}

Front Code

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.111cn.net/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<style type= "Text/css Tutorial" >
. show{Display:block;}
. hide{Display:none;}
</style>
<script type= "text/web Effects" src= "Jquery/jquery-1.2.6.js" ></script>
<script type= "text/web Effects" >

This method encapsulates the Ajax method for easy invocation.
function Myajax () {
var obj=jsondata ();
$.ajax ({
Type: ' Post ',
URL: ' ajax.asp tutorial x ',
Data:jsondata (),//can be directly added to a function name.
DataType: ' JSON ',
Beforesend:beforecall,
Success:callback
});
}
Encapsulates JSON data for code clarity
function Jsondata () {
var jsonstr= "({";
jsonstr+= "" Name ":";
jsonstr+= "" Tree "";
jsonstr+= ",";
jsonstr+= "" id ":";
jsonstr+= "" 123 "";
jsonstr+= "})";
Return eval (JSONSTR);/The key is the conversion.
}
Pre-Invoke method, unsuccessful
function Beforecall () {
$ (' #wait '). AddClass ("Show"). Append (' pull in ... ');
Alert (');/whether the test calls the
}
callback function
function callback (data) {
$ (' #response '). Append (data.name+data.id);
$ (' #wait '). CSS ("display", "none");
}
OnLoad () event
$ (function () {
$ (' #confirm '). Click (Myajax);
})
</script>
<body>
<div id= "Confirm" > Click </div>
<div id= "Response" > Receive background Data </div>
<div id= "Wait" class= "Hide" >hello</div>
</body>

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.