Sharepoint 2010 wcf服务。 用jquery调用方法
我在sharepoint应用程序中使用了wcf服务。 但是当我尝试从jquery调用方法时,它总是失败。
我用这样的字符串测试了服务:
ttp://localhost/_vti_bin/webservices/wcfservice.svc/**mex**
并有正常的反应。
WCF测试客户端也有响应。
但在我的JavaScript代码中,我总是收到错误。 为什么? 我做错了什么?
$.ajax({ url: "/_vti_bin/webservices/wcfservice.svc/HelloWorld", dataType: 'json', contentType: "application/json", success: function (res) { alert("good answer"); }, error: function (xhr) { alert("error"); return; } });
浏览器响应:
服务代码:
[BasicHttpBindingServiceMetadataExchangeEndpoint] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] [ServiceContract] public class WCFService { // To test this service, use the Visual Studio WCF Test client // set the endpoint to http:///_vti_bin/WebServices/WCFService.svc/mex [OperationContract] //[WebGet(UriTemplate = "HelloWorld", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] [WebInvoke(RequestFormat = WebMessageFormat.Json, UriTemplate="/HelloWorldM", BodyStyle = WebMessageBodyStyle.Wrapped)] public string HelloWorld() { return "Hello World from WCF and SharePoint 2010"; } }
尝试在SVC文件中使用MultipleBaseAddressWebServiceHostFactory而不是MultipleBaseAddressBasicHttpBindingServiceHostFactory。
请尝试以下2个资源,因为它们可能会对此问题有所了解。