執行個體入門Ajax(改進)

來源:互聯網
上載者:User

 前台

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>浮動廣告</title>  
  6.     <script src="jquery/jquery-1.2.6.js" type="text/javascript"></script>  
  7.     <script type="text/javascript">  
  8.      $(document).ready(function(){
  9.       $("#getids").click(function(){
  10.          doRequestUsingGET();
  11.       })
  12.        $("#postids").click(function(){
  13.          doRequestUsingPOST();
  14.       })
  15.     });
  16.      function doRequestUsingGET(){
  17.       var firstName=$("#firstName").val();
  18.        var birthday=$("#birthday").val();
  19.         $.get("AjaxServe.aspx",{name:firstName,birthday:birthday},
  20.             callback_state,"json");
  21.      }
  22.      function doRequestUsingPOST(){
  23.      var firstName=$("#firstName").val();
  24.        var birthday=$("#birthday").val();
  25.         $.post("AjaxServe.aspx",{name:firstName,birthday:birthday},
  26.             function(data,textStatus){
  27.                $("#serverResponse").html(data.info);
  28.             },"json");
  29.      }
  30.      
  31.      function callback_state(data){
  32.         $("#serverResponse").html(data.info+"<br/>"+data.name);
  33.      }
  34.      
  35.     </script>  
  36. </head>  
  37. <body>    
  38.    <h2>輸入姓名和生日</h2>
  39.  <form>
  40.   <input type="text" id="firstName" /><br />
  41.   <input type="text" id="birthday" />
  42.   <input type="button" value="GET"  id="getids"/><br />
  43.   <input type="button" value="POST"  id="postids"/>
  44.  </form>
  45.  <div id="serverResponse"></div>
  46. </body>  
  47. </html>  

後台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class AjaxServe : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         Hashtable ht = new Hashtable();
  16.         string name = Request.Params["name"].ToString();
  17.         string birth = Request.Params["birthday"].ToString();
  18.         if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(birth))
  19.         {
  20.             //Response.ContentType = "Application/json";
  21.             ht.Add("info", "成功了");
  22.             ht.Add("sta", "狀態");
  23.             ht.Add("name", name);
  24.             ht.Add("birth", birth);
  25.             Response.Write(CreateJsonParams(ht));
  26.         }
  27.         Response.End();
  28.     }
  29.     private string CreateJsonParams(Hashtable items)
  30.     {
  31.         string returnStr = "";
  32.         foreach(DictionaryEntry item in items)
  33.         {
  34.             returnStr += "/"" + item.Key.ToString() + "/":/"" + item.Value.ToString() + "/",";
  35.         }
  36.         return "{" + returnStr.Substring(0,returnStr.Length-1) + "}";
  37.     }
  38. }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.