如何在JQUERY中调用WCF Rest服务 [英] How to call WCF Rest service in JQUERY

查看:72
本文介绍了如何在JQUERY中调用WCF Rest服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我是wcf休息服务的新手,我对端点和行为没有太多了解,我们在web.config中提到过。但是我得到了一些教程并且能够创建服务。下面是服务合同,wcf服务文件,we.cong和jquery服务调用代码片段。我不确定,每次调用服务时都会转到错误块。



请帮帮我。谢谢。



1)服务合同:

Hello,
I am new to wcf rest services and I do not have that much idea about endpoint and behavior, we mention in web.config. However I got some tutorial and was able to create a service. Below is the service contract, wcf service file, we.cong and jquery service call code snippet. I am not sure about, every time when the service is getting called is goes to error block.

Please help me out. Thanks.

1)Service contract:

namespace IDM.Sedaru.Service
{
  
    [ServiceContract]
    public interface IHydraulicsService
    {
        [OperationContract]
        [WebGet(UriTemplate = "TestService", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        string TestService();
    }
}



2)服务(.svc)文件:


2) Service(.svc) file:

namespace IDM.Sedaru.Service
{
    public class HydraulicsService : IHydraulicsService
    {
      public string TestService()
        {
            return "Test service is called";
        }
    }
}



3)Web.config文件:


3) Web.config file:

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
    <services>
     
      <service behaviorConfiguration="WebServiceBehavior" name="IDM.Sedaru.Service.HydraulicsService">
        <endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="IDM.Sedaru.Service.IHydraulicsService" />
        <endpoint address="soap" binding="basicHttpBinding" contract="IDM.Sedaru.Service.IHydraulicsService" />
      </service>
    </services>
    
    <behaviors>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WebServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None"></security>
        </binding>
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered" useDefaultWebProxy="true" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
  </system.serviceModel>



4)JQUERY Call,在新项目文件中:


4) JQUERY Call, inside a new project file:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/jscript">

    //Call Rest WCF Service
    $(document).ready(function () {
        $(".btn").click(function () {
                $.ajax({
                type: "GET",
                url: "http://localhost:56158/HydraulicsService.svc/TestService",
                processData: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data:{},
                success: function (data) {
                    alert(data);
                },
                error: function (xhr, status, error) {
                    alert(error);

                }

            });
        });
    });
</script>

推荐答案

document )。ready( function (){
(document).ready(function () {


。btn)。click(功能() {
(".btn").click(function () {


.ajax({
type: GET
url: http:// localhost:56158 / HydraulicsService.svc / TestService
processData: false
contentType: 应用/ JSON; charset = utf-8
dataType: json
数据:{},
成功:功能(数据){
alert(data);
},
错误: function (xhr,status,error){
alert(error);

}

});
});
});
< / script >
.ajax({ type: "GET", url: "http://localhost:56158/HydraulicsService.svc/TestService", processData: false, contentType: "application/json; charset=utf-8", dataType: "json", data:{}, success: function (data) { alert(data); }, error: function (xhr, status, error) { alert(error); } }); }); }); </script>

这篇关于如何在JQUERY中调用WCF Rest服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆