$. Ajax method (passing JSON data)

Source: Internet
Author: User

Front-end

  1. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Title> No title page </title>
  5. <Style type = "text/CSS">
  6. . Show {display: block ;}
  7. . Hide {display: none ;}
  8. </Style>
  9. <SCRIPT type = "text/JavaScript" src = "jquery/jquery-1.2.6.js"> </SCRIPT>
  10. <SCRIPT type = "text/JavaScript">
  11. // This method encapsulates the Ajax Method for convenient calling.
  12. Function myajax (){
  13. // Var OBJ = jsondata ();
  14. $. Ajax ({
  15. Type: 'post ',
  16. URL: 'ajax. aspx ',
  17. Data: jsondata (), // you can add a function name directly.
  18. Datatype: 'json ',
  19. Beforesend: beforecall,
  20. Success: callback
  21. });
  22. }
  23. // Encapsulate JSON data for clear code
  24. Function jsondata (){
  25. VaR jsonstr = "({";
  26. Jsonstr + = "/" name /":";
  27. Jsonstr + = "/" tree /"";
  28. Jsonstr + = ",";
  29. Jsonstr + = "/" ID /":";
  30. Jsonstr ++ = "/" 123 /"";
  31. Jsonstr + = "})";
  32. Return eval (jsonstr); // The key lies in conversion.
  33. }
  34. // Method before call, failed
  35. Function beforecall (){
  36. $ ('# Wait'). addclass ("show"). append ('calling ...');
  37. // Alert (''); // test whether to call
  38. }
  39. // Callback function
  40. Function callback (data ){
  41. $ ('# Response'). append (data. Name + data. ID );
  42. Certificate ('{wait'}.css ("display", "NONE ");
  43. }
  44. // Onload () event
  45. $ (Function (){
  46. $ ('# Confirm'). Click (myajax );
  47. })
  48. </SCRIPT>
  49. </Head>
  50. <Body>
  51. <Div id = "Confirm"> click </div>
  52. <Div id = "response"> receive background data </div>
  53. <Div id = "wait" class = "hide"> Hello </div>
  54. </Body>
  55. </Html>

Background

  1. Protected void page_load (Object sender, eventargs E)
  2. {
  3. Hashtable ht = new hashtable ();
  4. String name = request. Params ["name"]. tostring ();
  5. String birth = request. Params ["Birthday"]. tostring ();
  6. If (! String. isnullorempty (name )&&! String. isnullorempty (birth ))
  7. {
  8. // Response. contenttype = "application/JSON ";
  9. // Response. Write (crearejson ("this is OK! ", 1, name, birth ));
  10. Ht. Add ("info", "succeeded ");
  11. Ht. Add ("sta", "status ");
  12. Ht. Add ("name", name );
  13. Ht. Add ("birth", birth );
  14. Response. Write (createjsonparams (HT ));
  15. }
  16. Response. End ();
  17. }
  18. Private string createjsonparams (hashtable items)
  19. {
  20. String returnstr = "";
  21. Foreach (dictionaryentry item in items)
  22. {
  23. Returnstr + = "/" "+ item. Key. tostring () +"/":/" "+ item. value. tostring () + "/",";
  24. }
  25. Return "{" + returnstr. substring (0, returnstr. Length-1) + "}";
  26. }
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.