1
votes

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";
        }
    }
2
alert("error"); xhr.responseText is empty – Ievgen
whats the value of xhr.status, are your service actually exists. – RobertPitt

2 Answers

4
votes

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