无法从jQuery调用WCF服务
问题描述:
您有十几篇文章介绍如何从jQuery调用WCF方法,但我无法使其工作。我有简单的WCF服务应用无法从jQuery调用WCF服务
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
}
这是实现
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
这是我的服务
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="TestWebApp.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="TestWebApp.Service1AspNetAjaxBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="jQueryToWCF.Service1">
<endpoint address=""
behaviorConfiguration="TestWebApp.Service1AspNetAjaxBehavior"
binding="webHttpBinding"
contract="jQueryToWCF.IService1" />
</service>
</services>
现在,我想在web.config从jQuery调用此函数(来自html页面)
$(document).ready(function() {
var param = "{value: 'Hello World!'}";
$.ajax({
type: "GET",
url: "http://localhost:5555/Service1.svc/GetData",
data: param,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
}
});
});
但是没有呼叫甚至使服务。我检查了小提琴手。但是当我把这个url放入浏览器时,我可以得到回应。任何人都可以帮我弄清楚这一点吗?
答
我一直在苦苦挣扎,并持续了一段时间。
终于让我感动的页面这一个:http://forums.asp.net/t/1765610.aspx/1
同时:这是一个功能性的项目文件,应该工作。您可能需要将JavaScript中的网址替换为正确的端口#。
另外,这CodeProject上的文章是我实际上设法去上班跨域调用的第一个例子。当我与上述项目集成时,我会发布一个更新的文件,它使用jsonp进行跨域:http://www.codeproject.com/Articles/223572/Calling-Cross-Domain-WCF-service-using-Jquery- Java – theo 2012-05-28 05:48:24
http://submissiv.com/share/playground.wcf.service_jsonp.zip - 这是一个跨域的工程。 – theo 2012-05-28 07:14:27