2011-02-23 68 views
1

我有wcf服务在sharepoint应用程序中崩溃。但是当我尝试从jquery调用方法时,它总是失败。与这样的字符串Sharepoint 2010 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; 
     } 
    }); 

浏览器响应:

服务
<wsdl:definitions name="WCFService" targetNamespace="http://tempuri.org/"> 

<wsp:Policy wsu:Id="BasicHttpBinding_WCFService_policy"> 

<wsp:ExactlyOne> 

<wsp:All> 
<http:NtlmAuthentication/> 
</wsp:All> 
</wsp:ExactlyOne> 
</wsp:Policy> 

<wsp:Policy wsu:Id="BasicHttpBinding_WCFService2_policy"> 

<wsp:ExactlyOne> 

<wsp:All> 
<http:NtlmAuthentication/> 
</wsp:All> 
</wsp:ExactlyOne> 
</wsp:Policy> 

<wsdl:types> 

<xsd:schema targetNamespace="http://tempuri.org/Imports"> 
<xsd:import schemaLocation="http://localcomputer/_vti_bin/.WebServices/WCFService.svc/mex xsd=xsd0" namespace="http://tempuri.org/"/> 
<xsd:import schemaLocation="http://localcomputer/_vti_bin/.WebServices/WCFService.svc/mex xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/> 
</xsd:schema> 
</wsdl:types> 

<wsdl:message name="WCFService_HelloWorld_InputMessage"> 
<wsdl:part name="parameters" element="tns:HelloWorld"/> 
</wsdl:message> 

<wsdl:message name="WCFService_HelloWorld_OutputMessage"> 
<wsdl:part name="parameters" element="tns:HelloWorldResponse"/> 
</wsdl:message> 

<wsdl:portType name="WCFService"> 

<wsdl:operation name="HelloWorld"> 
<wsdl:input wsaw:Action="http://tempuri.org/WCFService/HelloWorld" message="tns:WCFService_HelloWorld_InputMessage"/> 
<wsdl:output wsaw:Action="http://tempuri.org/WCFService/HelloWorldResponse" message="tns:WCFService_HelloWorld_OutputMessage"/> 
</wsdl:operation> 
</wsdl:portType> 

<wsdl:binding name="BasicHttpBinding_WCFService" type="tns:WCFService"> 
<wsp:PolicyReference URI="#BasicHttpBinding_WCFService_policy"/> 
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 

<wsdl:operation name="HelloWorld"> 
<soap:operation soapAction="http://tempuri.org/WCFService/HelloWorld" style="document"/> 

<wsdl:input> 
<soap:body use="literal"/> 
</wsdl:input> 

<wsdl:output> 
<soap:body use="literal"/> 
</wsdl:output> 
</wsdl:operation> 
</wsdl:binding> 

<wsdl:binding name="BasicHttpBinding_WCFService1" type="tns:WCFService"> 
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 

<wsdl:operation name="HelloWorld"> 
<soap:operation soapAction="http://tempuri.org/WCFService/HelloWorld" style="document"/> 

<wsdl:input> 
<soap:body use="literal"/> 
</wsdl:input> 

<wsdl:output> 
<soap:body use="literal"/> 
</wsdl:output> 
</wsdl:operation> 
</wsdl:binding> 

<wsdl:binding name="BasicHttpBinding_WCFService2" type="tns:WCFService"> 
<wsp:PolicyReference URI="#BasicHttpBinding_WCFService2_policy"/> 
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 

<wsdl:operation name="HelloWorld"> 
<soap:operation soapAction="http://tempuri.org/WCFService/HelloWorld" style="document"/> 

<wsdl:input> 
<soap:body use="literal"/> 
</wsdl:input> 

<wsdl:output> 
<soap:body use="literal"/> 
</wsdl:output> 
</wsdl:operation> 
</wsdl:binding> 

<wsdl:service name="WCFService"> 

<wsdl:port name="BasicHttpBinding_WCFService" binding="tns:BasicHttpBinding_WCFService"> 
<soap:address location="http://localcomputer/_vti_bin/.WebServices/WCFService.svc"/> 
</wsdl:port> 
</wsdl:service> 
</wsdl:definitions> 

代码:

[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://<Your server name>/_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"; 
     } 
    } 
+2

....什么错误? – Shoban 2011-02-23 19:06:29

+1

警报(“错误”); xhr.responseText为空 – Evgeny 2011-02-23 19:09:47

+1

xhr.status的值是多少,您的服务实际上是否存在。 – RobertPitt 2011-02-23 19:14:51

回答

4

尝试使用MultipleBaseAddressWebServiceHostFactory代替MultipleBaseAddressBasicHttpBindingServiceHostFactory在SVC文件。

2

请尝试以下2个资源,因为他们可以摆脱对这个问题的一些情况。

+2

1)我无法访问web.config。因为sharepoint和第二我可以达到所有方法从wcf测试客户端。所以我认为这是可能的从ajax做。但是,url应该如何看起来像? – Evgeny 2011-02-23 19:25:03

+2

为什么不能访问web.config? – Shoban 2011-02-23 19:30:09

+2

它就像另一个asp.net web应用程序。 – Shoban 2011-02-23 19:36:06