JQuery调用WCF服务
JQuery可以采用JS的方式来调用AJAX. 如下面的示例:
$.ajax({
type: 'GET',
url: 'http://localhost:9008/Service1.svc/GetData', //WCF的URL
contentType:'text/json',
data:{value:104403},
success: function(msg) {
alert(msg);
}
});
URL: 指向WCF的URL地址
contentType:text/json,需要设置为此类型
data:JSON格式的参数清单
下面列出在WCF中需要注意的配置:
1. 在 configuration/system.serviceModel/behaviors节点下增加下面的内容
<!-- 新增的基于JS访问的 START-->
<endpointBehaviors>
<behavior name="ServiceBehavior"> <!--这个名称在后面的 services/service/endpoint 节点中的 behaviorConfiguration 属性名称 -->
<webHttp/>
<enableWebScript/>
</behavior>
</endpointBehaviors>
<!-- 新增的基于JS访问的 END-->
2. 在 configuration/system.serviceModel 节点下新增 services 节点 (如下)
<!-- 新增的基于JS访问的 START-->
<services>
<service name="WcfService1.Service1">
<endpoint address="" behaviorConfiguration="ServiceBehavior" binding="webHttpBinding" contract="WcfService1.Service1"/>
</service>
</services>
<!-- 新增的基于JS访问的 END-->
3. 在WCF对应的实现类中添加如下的配置
a) 在类名上 添加 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
b) 在方法上添加 [WebInvoke(RequestFormat = WebMessageFormat.Json, Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
只能使用GET方式,但怎么设置都用不了POST方式。
浙公网安备 33010602011771号