SharePoint 2010的WCF服务。调用方法与jQuery [英] Sharepoint 2010 wcf service. call method with jquery

查看:177
本文介绍了SharePoint 2010的WCF服务。调用方法与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WCF服务大清洗的SharePoint应用程序。但是,当我试图从jQuery的调用方法,它总是失败。

这样的字符串

我已经测试服务:

  TTP://localhost/_vti_bin/webservices/wcfservice.svc/**mex**
 

和有正常的反应。

WCF测试客户端有​​回应了。

但在我的JavaScript codeI接受她总是错误。为什么?我在做什么错了?

  $。阿贾克斯({
        网址:/_vti_bin/webservices/wcfservice.svc/HelloWorld
        数据类型:JSON,
        的contentType:应用/ JSON
        成功:函数(RES){
            警报(好回答);
        },
        错误:函数(XHR){
            警报(错误);

            返回;
        }
    });
 

浏览器的响应:

 < WSDL:定义名称=WCFService的targetNamespace =htt​​p://tempuri.org/>

< WSP:政策WSU:ID =BasicHttpBinding_WCFService_policy>

< WSP:ExactlyOne上>

< WSP:所有>
< HTTP:NtlmAuthentication />
< / WSP:所有>
< / WSP:ExactlyOne上>
< / WSP:政策>

< WSP:政策WSU:ID =BasicHttpBinding_WCFService2_policy>

< WSP:ExactlyOne上>

< WSP:所有>
< HTTP:NtlmAuthentication />
< / WSP:所有>
< / WSP:ExactlyOne上>
< / WSP:政策>

< WSDL:类型>

< XSD:架构的targetNamespace =htt​​p://tempuri.org/Imports>
< XSD:导入的schemaLocation =HTTP://localcomputer/_vti_bin/.WebServices/WCFService.svc/mex XSD = xsd0命名空间=htt​​p://tempuri.org//>
< XSD:导入的schemaLocation =HTTP://localcomputer/_vti_bin/.WebServices/WCFService.svc/mex XSD = xsd1命名空间=htt​​p://schemas.microsoft.com/2003/10/Serialization//&GT ;
< / XSD:模式>
< / WSDL:类型>

< WSDL:消息名称=WCFService_HelloWorld_InputMessage>
< WSDL:部分名称=参数元素=TNS:HelloWorld的/>
< / WSDL:消息>

< WSDL:消息名称=WCFService_HelloWorld_OutputMessage>
< WSDL:部分名称=参数元素=TNS:HelloWorldResponse/>
< / WSDL:消息>

< WSDL:portType的名称=WCFService>

< WSDL:操作名称=HelloWorld的>
< WSDL:输入WSAW:行动=htt​​p://tempuri.org/WCFService/HelloWorld消息=TNS:WCFService_HelloWorld_InputMessage/>
< WSDL:输出WSAW:行动=htt​​p://tempuri.org/WCFService/HelloWorldResponse消息=TNS:WCFService_HelloWorld_OutputMessage/>
< / WSDL:操作>
< / WSDL:端口类型>

< WSDL:绑定名称=BasicHttpBinding_WCFServiceTYPE =TNS:WCFService>
< WSP:PolicyReference URI =#BasicHttpBinding_WCFService_policy/>
<肥皂:绑定传输=htt​​p://schemas.xmlsoap.org/soap/http/>

< WSDL:操作名称=HelloWorld的>
<肥皂:操作的soapAction =htt​​p://tempuri.org/WCFService/HelloWorld的风格=文件/>

< WSDL:输入>
<肥皂:身体使用=文字/>
< / WSDL:输入>

< WSDL:输出>
<肥皂:身体使用=文字/>
< / WSDL:输出>
< / WSDL:操作>
< / WSDL:绑定>

< WSDL:绑定名称=BasicHttpBinding_WCFService1TYPE =TNS:WCFService>
<肥皂:绑定传输=htt​​p://schemas.xmlsoap.org/soap/http/>

< WSDL:操作名称=HelloWorld的>
<肥皂:操作的soapAction =htt​​p://tempuri.org/WCFService/HelloWorld的风格=文件/>

< WSDL:输入>
<肥皂:身体使用=文字/>
< / WSDL:输入>

< WSDL:输出>
<肥皂:身体使用=文字/>
< / WSDL:输出>
< / WSDL:操作>
< / WSDL:绑定>

< WSDL:绑定名称=BasicHttpBinding_WCFService2TYPE =TNS:WCFService>
< WSP:PolicyReference URI =#BasicHttpBinding_WCFService2_policy/>
<肥皂:绑定传输=htt​​p://schemas.xmlsoap.org/soap/http/>

< WSDL:操作名称=HelloWorld的>
<肥皂:操作的soapAction =htt​​p://tempuri.org/WCFService/HelloWorld的风格=文件/>

< WSDL:输入>
<肥皂:身体使用=文字/>
< / WSDL:输入>

< WSDL:输出>
<肥皂:身体使用=文字/>
< / WSDL:输出>
< / WSDL:操作>
< / WSDL:绑定>

< WSDL:服务名称=WCFService>

< WSDL:端口名称=BasicHttpBinding_WCFService绑定=TNS:BasicHttpBinding_WCFService>
<肥皂:地址位置=HTTP://localcomputer/_vti_bin/.WebServices/WCFService.svc/>
< / WSDL:端口>
< / WSDL:服务>
< / WSDL:定义>
 

code服务的:

  [BasicHttpBindingServiceMetadataExchangeEndpoint]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)
    [的ServiceContract]
    公共类WCFService
    {
        //为了测试这项服务,使用Visual Studio WCF测试客户端
        //设置端点为http://<您的服务器名称> / _ vti_bin / Web服务/ WCFService.svc / MEX
        [OperationContract的]
        // [WebGet(UriTemplate =HelloWorld的,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)
        [WebInvoke(RequestFormat = WebMessageFormat.Json,UriTemplate =/ HelloWorldM,BodyStyle = WebMessageBodyStyle.Wrapped)
        公共字符串的HelloWorld()
        {
            从WCF和SharePoint 2010 Hello World的回归;
        }
    }
 

解决方案

尽量使用MultipleBaseAddressWebServiceHostFactory代替MultipleBaseAddressBasicHttpBindingServiceHostFactory的SVC文件。

i have wcf service hosed in sharepoint application. But when i try to call method from jquery it always fails.

I have tested service with such string:

ttp://localhost/_vti_bin/webservices/wcfservice.svc/**mex**

and have normal response.

WCF test client have response too.

But in my javascript code i recieve always error. Why? What i am doing wrong?

   $.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;
        }
    });

Browser response:

<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>

Code of service:

[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";
        }
    }

解决方案

Try to use MultipleBaseAddressWebServiceHostFactory instead of MultipleBaseAddressBasicHttpBindin­gServiceHostFactory in SVC files.

这篇关于SharePoint 2010的WCF服务。调用方法与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆