实例入门Ajax(改进)

来源:互联网 发布:淘宝客工具 编辑:程序博客网 时间:2024/05/25 18:12

 前台

  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. }