2013-02-06 152 views
0

我在vs2012中創建了一個RESTful wcf服務。我可以訪問這個服務frfom網絡brovsers。我的接口文件是這樣:jquery和RESTful wcf服務

namespace RestService 
{ 
    [ServiceContract] 
    public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Xml, 
      BodyStyle=WebMessageBodyStyle.Wrapped, 
      UriTemplate = "xml/{id}" 
      )] 
     string XMLData(string id); 

     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "json/{id}" 
      )] 
     string JSONData(string id); 

    } 
} 

和我的配置文件是:

<system.serviceModel> 
    <services> 
     <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehavior"> 
     <endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"> 
     </endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

當我跑步時我與CTR + F5服務它給我的網址localhost:50046/RestServiceImpl.svc

當我寫信給網絡瀏覽器,我可以訪問我的服務的代碼,並回應:

代碼是//localhost:50046/RestServiceImpl.svc/json/123 響應是{"JSONDataResult":"you requested product 123"}

現在我的問題是,我將從我的html代碼訪問該服務。而且我會發送一個數據到服務器並收到該服務的響應。我簡單的jQuery代碼是:

$("input:#giris").click(function(){ 
    function GetCategoriesJson() { 

     $.ajax(
     { 
      type:"GET", 
      url:"localhost:50046/RestServiceImpl.svc", 
      data:"{123}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json/{123}", 
      success: OnSuccessJson, 
      error: OnErrorJson 
     }); 
     function OnSuccessJson(data, status) { 
      alert("basarili") 
     } 
     function OnErrorJson(data, status) { 
      alert("Exception"); 
     } 
     } 
    }); 

我必須做什麼,我必須寫類型,URL,數據,contentype,數據類型等,請幫我已經工作了幾天

+0

可以在任何請幫助我 –

回答

0

什麼從客戶端調用時出現錯誤?

因爲它是WCF我猜它是一個配置問題,請在你的.config文件中試試這個。

<standardEndpoint name="" helpEnabled="true" 
    automaticFormatSelectionEnabled="false" 
    defaultOutgoingResponseFormat ="Json" /> 

這裏是一個很好的例子:http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call

+0

沒有例外,我什麼都不做 –

+0

我看例子 –

+0

嘗試captu將來自客戶端的http流量(例如Fiddler)重新設置爲請求的樣子。 – Jocke

0

我不知道,但你可以試試這個: -

在AJAX調用改變URL這個

url:"localhost:50046/RestServiceImpl.svc/json/123"