我在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;
}
});
浏览器回复:
<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";
}
}
答案 0 :(得分:4)
尝试在SVC文件中使用MultipleBaseAddressWebServiceHostFactory而不是MultipleBaseAddressBasicHttpBindingServiceHostFactory。
答案 1 :(得分:2)
请尝试以下2种资源,因为它们可能会对此问题有所了解。