Ajax Sharepoint 2010 wcf服务。使用jquery调用方法
我在sharepoint应用程序中安装了wcf服务。但当我尝试从jquery调用方法时,它总是失败 我已经用这样的字符串测试了服务:Ajax Sharepoint 2010 wcf服务。使用jquery调用方法,ajax,wcf,jquery,sharepoint-2010,Ajax,Wcf,Jquery,Sharepoint 2010,我在sharepoint应用程序中安装了wcf服务。但当我尝试从jquery调用方法时,它总是失败 我已经用这样的字符串测试了服务: ttp://localhost/_vti_bin/webservices/wcfservice.svc/**mex** 反应正常 WCF测试客户端也有响应 但在我的javascript代码中,我总是收到错误。为什么?我做错了什么 $.ajax({ url: "/_vti_bin/webservices/wcfservice.svc/Hell
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";
}
}
[BasicHttpBindingServiceMetadataExchangeEndpoint]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
[服务合同]
公共类WCF服务
{
//要测试此服务,请使用Visual Studio WCF测试客户端
//将端点设置为http:///_vti_bin/WebServices/WCFService.svc/mex
[经营合同]
//[WebGet(UriTemplate=“HelloWorld”,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]
[WebInvoke(RequestFormat=WebMessageFormat.Json,UriTemplate=“/HelloWorldM”,BodyStyle=WebMessageBodyStyle.Wrapped)]
公共字符串HelloWorld()
{
返回“来自WCF和SharePoint 2010的Hello World”;
}
}
请尝试以下两种资源,因为它们可能会对这个问题有所帮助
xhr.status
的值是什么,您的服务是否实际存在。警报(“错误”);xhr.responseText为空xhr.status
的值是什么,您的服务是否实际存在。1)我无法访问web.config。由于sharepoint和second,我可以从wcf测试客户端访问所有方法。所以我认为这可以通过ajax实现。但url应该是什么样子的呢?为什么不能访问web.config?它就像另一个asp.net web应用程序一样。因为在sharepoint2010中,wcf服务的端点和其他内容是自动生成的。我只有一个包含内容的文件:1)我不能访问web.config。由于sharepoint和second,我可以从wcf测试客户端访问所有方法。所以我认为这可以通过ajax实现。但是url应该是什么样子?为什么不能访问web.config?它就像另一个asp.net web应用程序一样。因为在sharepoint2010中,wcf服务的端点和其他内容是自动生成的。我只有一个包含内容的文件: