title: Sharepoint 2010 wcf service. call method with jquery
body: <p>i have wcf service hosed in sharepoint application. But when i try to call method from jquery it always fails.</p>
  
  <p>I have tested service with such string:</p>
  
  <pre><code>ttp://localhost/_vti_bin/webservices/wcfservice.svc/**mex**
  </code></pre>
  
  <p>and have normal response.</p>
  
  <p>WCF test client have response too.</p>
  
  <p>But in my javascript code i recieve always error. Why? What i am doing wrong?</p>
  
  <pre><code>   $.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;
          }
      });
  </code></pre>
  
  <p>Browser response:</p>
  
  <pre><code>&lt;wsdl:definitions name="WCFService" targetNamespace="http://tempuri.org/"&gt;
  
  &lt;wsp:Policy wsu:Id="BasicHttpBinding_WCFService_policy"&gt;
  
  &lt;wsp:ExactlyOne&gt;
  
  &lt;wsp:All&gt;
  &lt;http:NtlmAuthentication/&gt;
  &lt;/wsp:All&gt;
  &lt;/wsp:ExactlyOne&gt;
  &lt;/wsp:Policy&gt;
  
  &lt;wsp:Policy wsu:Id="BasicHttpBinding_WCFService2_policy"&gt;
  
  &lt;wsp:ExactlyOne&gt;
  
  &lt;wsp:All&gt;
  &lt;http:NtlmAuthentication/&gt;
  &lt;/wsp:All&gt;
  &lt;/wsp:ExactlyOne&gt;
  &lt;/wsp:Policy&gt;
  
  &lt;wsdl:types&gt;
  
  &lt;xsd:schema targetNamespace="http://tempuri.org/Imports"&gt;
  &lt;xsd:import schemaLocation="http://localcomputer/_vti_bin/.WebServices/WCFService.svc/mex xsd=xsd0" namespace="http://tempuri.org/"/&gt;
  &lt;xsd:import schemaLocation="http://localcomputer/_vti_bin/.WebServices/WCFService.svc/mex xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/&gt;
  &lt;/xsd:schema&gt;
  &lt;/wsdl:types&gt;
  
  &lt;wsdl:message name="WCFService_HelloWorld_InputMessage"&gt;
  &lt;wsdl:part name="parameters" element="tns:HelloWorld"/&gt;
  &lt;/wsdl:message&gt;
  
  &lt;wsdl:message name="WCFService_HelloWorld_OutputMessage"&gt;
  &lt;wsdl:part name="parameters" element="tns:HelloWorldResponse"/&gt;
  &lt;/wsdl:message&gt;
  
  &lt;wsdl:portType name="WCFService"&gt;
  
  &lt;wsdl:operation name="HelloWorld"&gt;
  &lt;wsdl:input wsaw:Action="http://tempuri.org/WCFService/HelloWorld" message="tns:WCFService_HelloWorld_InputMessage"/&gt;
  &lt;wsdl:output wsaw:Action="http://tempuri.org/WCFService/HelloWorldResponse" message="tns:WCFService_HelloWorld_OutputMessage"/&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:portType&gt;
  
  &lt;wsdl:binding name="BasicHttpBinding_WCFService" type="tns:WCFService"&gt;
  &lt;wsp:PolicyReference URI="#BasicHttpBinding_WCFService_policy"/&gt;
  &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
  
  &lt;wsdl:operation name="HelloWorld"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/WCFService/HelloWorld" style="document"/&gt;
  
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  
  &lt;wsdl:binding name="BasicHttpBinding_WCFService1" type="tns:WCFService"&gt;
  &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
  
  &lt;wsdl:operation name="HelloWorld"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/WCFService/HelloWorld" style="document"/&gt;
  
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  
  &lt;wsdl:binding name="BasicHttpBinding_WCFService2" type="tns:WCFService"&gt;
  &lt;wsp:PolicyReference URI="#BasicHttpBinding_WCFService2_policy"/&gt;
  &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
  
  &lt;wsdl:operation name="HelloWorld"&gt;
  &lt;soap:operation soapAction="http://tempuri.org/WCFService/HelloWorld" style="document"/&gt;
  
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  
  &lt;wsdl:service name="WCFService"&gt;
  
  &lt;wsdl:port name="BasicHttpBinding_WCFService" binding="tns:BasicHttpBinding_WCFService"&gt;
  &lt;soap:address location="http://localcomputer/_vti_bin/.WebServices/WCFService.svc"/&gt;
  &lt;/wsdl:port&gt;
  &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p>Code of service:</p>
  
  <pre><code>[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://&lt;Your server name&gt;/_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";
          }
      }
  </code></pre>