如何在JQUERY中调用WCF Rest服务

编程入门 行业动态 更新时间:2024-10-11 09:27:34
本文介绍了如何在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: "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: "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服务

本文发布于:2023-11-28 02:55:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   JQUERY   Rest   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!