秋風的蕭瑟 又見湖邊木葉飛

          歡迎來到梁良 | LonBlog,這里記錄下了我生活點點滴滴。

          jQuery調用WebService

          1、編寫4種WebService方法

           

              [WebService(Namespace = "http://tempuri.org/")]
              [WebServiceBinding(ConformsTo 
          = WsiProfiles.BasicProfile1_1)]
              [ScriptService]                             
          //令WebService成功傳入Json參數,并以Json形式返回結果
              [GenerateScriptType(typeof(Person))]        //不是必要,但推薦添加(如果Person里面再嵌套另一個復雜類型,則必要聲明)
              [ToolboxItem(false)]
              
          public class WebService1 : System.Web.Services.WebService
              {
                  
          /// 
                  
          /// 無任何參數
                  
          /// 
                  
          /// 
                  [WebMethod]
                  
          public string HelloWorld()
                  {
                      
          return "Hello World";
                  }

                  
          /// 
                  
          /// 傳入參數
                  
          /// 
                  
          /// 
                  
          /// 
                  [WebMethod]
                  
          public string Hello(string name)
                  {
                      
          return string.Format("Hello {0}", name);
                  }

                  
          /// 
                  
          /// 返回泛型列表
                  
          /// 
                  
          /// 
                  
          /// 
                  [WebMethod]
                  
          public List<int> CreateArray(int i)
                  {
                      List
          <int> list = new List<int>();

                      
          while (i >= 0)
                      {
                          list.Add(i
          --);
                      }

                      
          return list;
                  }

                  
          /// 
                  
          /// 返回復雜類型
                  
          /// 
                  
          /// 
                  
          /// 
                  
          /// 
                  [WebMethod]
                  
          public Person GetPerson(string name, int age)
                  {
                      
          return new Person()
                      {
                          Name 
          = name,
                          Age 
          = age
                      };
                  }
              }

              
          /// 
              
          /// 復雜類型
              
          /// 
              public class Person
              {
                  
          public string Name { getset; }

                  
          public int Age { getset; }
              }

           


          2、編寫js調用以上方法

           

          <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

          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 runat="server">
              
          <title>無標題頁title>
              
          <style type="text/css">
              input
              
          {
                  width
          :200px;
              
          }
              
          <style>

              
          <script type="text/javascript" src="jquery-1[1].2.6.min.js">
          </script>
              
          <script type="text/javascript">
              $(
          function(){  
                
                  
          /*
                      1、WebService請求類型都為Post,WebService的Url為“[WebServiceUrl]/[WebMethod]”
                      2、contentType聲明為Json
                      3、data要用Json的字符串格式傳入
                      4、設置了dataType為json后,result就直接為返回的Json對象。

                  
          */
                  
                  
          //調用無參數方法
                  $("#btnHelloWorld").click(function(){
                      $.ajax({
                          type: 
          "POST",
                          contentType:
          "application/json",
                          url:
          "WebService1.asmx/HelloWorld",
                          data:
          "{}",
                          dataType:'json',
                          success:
          function(result){                    
                              alert(result.d);
                          }
                      });
                  });        
                  
                  
          //傳入1個參數
                  $("#btnHello").click(function(){
                      $.ajax({
                          type: 
          "POST",
                          contentType:
          "application/json",
                          url:
          "WebService1.asmx/Hello",
                          data:
          "{name:'KiMoGiGi'}",
                          dataType:'json',
                          success:
          function(result){                    
                              alert(result.d);
                          }
                      });
                  });
                  
                   
          //返回泛型列表
                  $("#btnArray").click(function(){
                      $.ajax({
                          type: 
          "POST",
                          contentType:
          "application/json",
                          url:
          "WebService1.asmx/CreateArray",
                          data:
          "{i:10}",
                          dataType:'json',
                          success:
          function(result){                    
                              alert(result.d.join(
          " | "));
                          }
                      });
                  });
                  
                   
          //返回復雜類型
                  $("#btnPerson").click(function(){
                      $.ajax({
                          type: 
          "POST",
                          contentType:
          "application/json",
                          url:
          "WebService1.asmx/GetPerson",
                          data:
          "{name:'KiMoGiGi',age:26}",
                          dataType:'json',
                          success:
          function(result){
                              
          var person = result.d;
                              
          var showText = [];
                              
          for(var p in person){
                                  showText.push(p 
          + ":" + person[p]);
                              }
                              alert(showText.join(
          "\r\n"));
                          }
                      });
                  });
              });
             
          </script>
          <head>
              
          <body>
                  
          <form id="form1" runat="server">
                      
          <p>
                          
          <input type="button" id="btnHelloWorld" value="HelloWorld" />
                     
          <p>
                      
          <p>
                          
          <input type="button" id="btnHello" value="Hello" />
                      
          <p>
                      
          <p>
                          
          <input type="button" id="btnArray" value="CreateArray" />
                      
          <p>
                      
          <p>
                          
          <input type="button" id="btnPerson" value="GetPerson" />
                      
          <p>
                 
          <form>
             
          <body>
          <html>

          posted on 2010-06-10 18:02 梁良 閱讀(2389) 評論(0)  編輯  收藏 所屬分類: jQuery

          <2010年6月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導航

          公告

              歡迎光臨

          常用鏈接

          隨筆分類

          隨筆檔案

          相冊

          .我的網站.

          友情博客

          開源社區

          科技博客

          資料網站

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          Powered by:
          LonLeung
          Copyright © 梁良

          本頁生成時間:毫秒

          主站蜘蛛池模板: 桐城市| 修文县| 冷水江市| 鄂伦春自治旗| 赤水市| 巴彦县| 玛沁县| 岳阳市| 平潭县| 绥滨县| 永春县| 南安市| 三明市| 兴国县| 安丘市| 贡觉县| 太白县| 高平市| 壶关县| 怀来县| 行唐县| 沧源| 玉门市| 布尔津县| 金华市| 云林县| 咸阳市| 东兴市| 潍坊市| 临漳县| 苍溪县| 崇明县| 永德县| 静海县| 田东县| 吴旗县| 桦甸市| 合水县| 塘沽区| 大洼县| 临海市|