繁体   English   中英

jQuery和RESTful WCF服务

[英]jquery and RESTful wcf service

我在vs2012中使用了RESTful wcf服务。 我可以访问此sservice frfom网站浏览器。 我的接口文件是这样的:

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运行服务时,它会给我该URL localhost:50046/RestServiceImpl.svc

当我向Web浏览器写该代码时,我可以访问我的服务,并且出现以下响应:

代码为//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,数据类型等。请帮助我已经工作了几天

从客户端调用它时会遇到什么错误?

由于它是WCF,我想这是一个配置问题,请在您的.config文件中尝试一下。

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

这是一个很好的示例: http : //www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call

我不确定,但您可以尝试以下方法:

在ajax调用更改URL到这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM