2013-02-06 154 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"