首頁 > web前端 > js教程 > 主體

jQuery呼叫RESTful WCF範例程式碼(GET方法/POST方法)_jquery

WBOY
發布: 2016-05-16 17:02:23
原創
1166 人瀏覽過

不廢話了,直奔主題吧

wcf端:

近年來比較流行restful,為了能讓ajax調用,同時也為了支持restful風格的uri,在創建一個Ajax-enabled Wcf Service後,必須手動修改svc文件,指定Factory,即:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"Factory="System.ServiceModel.Activation.WebServiceHostFactory"

%>

附註:如果不加入Factory,則wcf將無法以類似http://localhost/helloWorld.svc/Hello/person/name 的restful方式直接存取。

同時也要去掉web.config中的即類似:



   
     
        
       
     

      multipleSiteBindingsEnabled="true" />
   
     
       
     
   

 


好了,開始寫程式碼,鑑於wcf呼叫時有GET/POST二種方式,下面把幾種常用的情況都寫一個範例方法:


複製程式碼 程式碼如下:

使用 System.Collections.Generic;
使用System.ServiceModel;
使用System.ServiceModel.Activation;
使用System.ServiceModel.Web;

命名空間 ajaxSamplepleple
{
    [ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")]
    [AspNetCompatibilityRequirements(Requirements. 🎜>
        /// ;
        /// 只能發表 Restful 方法
        /// ;
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome} public List; PostRestfulTest(string person,stringwelcome)
        {
            List;結果 = new List();

            result.Add("PostRestfulTest -> 來自伺服器:");
           結果。 }

        /// ;
        /// 只能取得Restful方法
        /// ;
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome} public List; GETRestfulTest(string person, stringwelcome)
        {
            List;結果 = new List;

            result.Add("GETRestfulTest -> 從伺服器:");
           結果。 }

        /// ;
        /// 即可Get與Post的Restful方法
        /// ;
        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List RestfulTest(string person, stringwelcome)
  = new List();

            result.Add("RestfulTest -> 來自伺服器:");
           result.Add(person);
 
        ///         /// 只能Post的常規方法(註:Post方式,BodyStyle必須設定為WrappedRequest或Wrapped)
        ///
        ///                   List結果= new List();

            result.Add("PostRestfulTest -> 來自伺服器:");       結果。
            回傳結果;
        }

           ///

        ///
        ///
        ///         [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        public List; GETTest(string p.            List;結果= new List ();

            result.Add("GETTest -> 來自伺服器:");
      新增(人);
            的結果。新增(歡迎);
            返回結果;
        }

       }

   >    }
}


jQuery 呼叫代碼:




複製程式碼


程式碼如下:




有時候,WCF暴露的方法中可能需要一些敏感資訊做為參數(例如用戶名/用戶ID之類),這時如果直接用js來呼叫wcf,可能會把這部分資訊洩漏在客戶端,這種場景下,我們也經常用一個服務端的ashx來做中轉

TestService.svc




複製程式碼


程式碼如下:


程式碼如下:


Service; 🎜>

         [OperationContract]
        public double Get       if (month == 1)//只是示範而已
            {
                return 5000;
            }
            else
            {
                return 1000;
            }
        }
    }
}


AjaxProcess.ashx
複製代碼代碼如下:


代碼如下:



代碼如下:



代碼如下:



代碼如下:



代碼如下:



代碼如下:



代碼如下:



命名空間ashx_jQuery
{
    /// ;
    /// AjaxProcess 的摘要說明    {

        public void ProcessRequest(HttpContext context)
    "text/plain";
            string Month = context.Request["month" ];

            TestService wcf = new TestService();
                 context.Response.Write("{薪資:"薪資"}");
        }
          /// ;         ///         private int GetUserId()    🎜>        }         public bool IsReusable
        {             get            {          }         }     } }jQuery叫複製程式碼程式碼如下:



渡//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >


    jQuery ashx 範例
   
   
      y  $().read (function () {
            $("#btnTest").click(function () {
               "AjaxProcess.ashx",
                   {月:1 },
function (e) {
                      var d = eval("(" e ")");                    }, "html"); 🎜>       
    腳本>;
頭>

   

       
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!