2012-01-27 2 views
1

두 개의 매개 변수 인 string을 받고 XmlElement를 반환하는 WCF Webservice 메서드를 만들었습니다.Jquery Ajax 및 WCF Webservice 악몽

ASP.NET 페이지에 해당 메서드에 대한 JQuery AJAX 호출이 있습니다. 그것은 작동하지 않으며 온라인 설명서는 그저 분만에 나를 혼란스럽게 만듭니다. 명심해야 할 몇 가지 측면은 바로 지금 url-encoded로 매개 변수를 전송한다는 것입니다.

이 시점에서 모든 것을 JSON으로 만들지 않겠습니다. 난 이걸 작동시켜야 해.

내 webservice는 WCF 테스트 클라이언트 (Visual Studio 2010)를 사용할 때 완벽하게 작동합니다. 하지만 JQuery AJAX와 대화 할 수는 없습니다. 잘못된 요청 오류가 발생합니다.

자세한 내용이 필요하면 언제든지 물어보십시오. 내 JQuery와 여기에

<?xml version="1.0" encoding="UTF-8"?> 
<wsdl:definitions xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
     xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" 
     xmlns:wsa10="http://www.w3.org/2005/08/addressing" 
     xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" 
     xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
     xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" 
     xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" 
     xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
     xmlns:tns="http://tempuri.org/" 
     xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
     targetNamespace="http://tempuri.org/" 
     name="AddressVerification"> 
    <wsdl:types> 
    <xsd:schema targetNamespace="http://tempuri.org/Imports"> 
     <xsd:import namespace="http://tempuri.org/" 
      schemaLocation="http://localhost:16859/AddressVerification.svc?xsd=xsd0"/> 
     <xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" 
      schemaLocation="http://localhost:16859/AddressVerification.svc?xsd=xsd1"/> 
    </xsd:schema> 
    </wsdl:types> 
    <wsdl:message name="IAddressVerification_DoWork_InputMessage"> 
    <wsdl:part name="parameters" element="tns:DoWork"/> 
    </wsdl:message> 
    <wsdl:message name="IAddressVerification_DoWork_OutputMessage"> 
    <wsdl:part name="parameters" element="tns:DoWorkResponse"/> 
    </wsdl:message> 
    <wsdl:message name="IAddressVerification_VerifyAddress_InputMessage"> 
    <wsdl:part name="parameters" element="tns:VerifyAddress"/> 
    </wsdl:message> 
    <wsdl:message name="IAddressVerification_VerifyAddress_OutputMessage"> 
    <wsdl:part name="parameters" element="tns:VerifyAddressResponse"/> 
    </wsdl:message> 
    <wsdl:portType name="IAddressVerification"> 
    <wsdl:operation name="DoWork"> 
     <wsdl:input message="tns:IAddressVerification_DoWork_InputMessage" 
      wsaw:Action="http://tempuri.org/IAddressVerification/DoWork"/> 
     <wsdl:output message="tns:IAddressVerification_DoWork_OutputMessage" 
      wsaw:Action="http://tempuri.org/IAddressVerification/DoWorkResponse"/> 
    </wsdl:operation> 
    <wsdl:operation name="VerifyAddress"> 
     <wsdl:input message="tns:IAddressVerification_VerifyAddress_InputMessage" 
      wsaw:Action="http://tempuri.org/IAddressVerification/VerifyAddress"/> 
     <wsdl:output message="tns:IAddressVerification_VerifyAddress_OutputMessage" 
      wsaw:Action="http://tempuri.org/IAddressVerification/VerifyAddressResponse"/> 
    </wsdl:operation> 
    </wsdl:portType> 
    <wsdl:binding name="BasicHttpBinding_IAddressVerification" 
     type="tns:IAddressVerification"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 
    <wsdl:operation name="DoWork"> 
     <soap:operation style="document" 
      soapAction="http://tempuri.org/IAddressVerification/DoWork"/> 
     <wsdl:input> 
     <soap:body use="literal"/> 
     </wsdl:input> 
     <wsdl:output> 
     <soap:body use="literal"/> 
     </wsdl:output> 
    </wsdl:operation> 
    <wsdl:operation name="VerifyAddress"> 
     <soap:operation style="document" 
      soapAction="http://tempuri.org/IAddressVerification/VerifyAddress"/> 
     <wsdl:input> 
     <soap:body use="literal"/> 
     </wsdl:input> 
     <wsdl:output> 
     <soap:body use="literal"/> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:service name="AddressVerification"> 
    <wsdl:port name="BasicHttpBinding_IAddressVerification" 
      binding="tns:BasicHttpBinding_IAddressVerification"> 
     <soap:address location="http://localhost:16859/AddressVerification.svc"/> 
    </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 

됩니다 : 여기

은 웹 서비스에 대한 내 WSDL이다 여기
$(document).ready(function() { 

    $("#btnSubmit").click(function (e) { 
     e.preventDefault(); 
     var address1 = $("#txtAddress1").val(); 
     var address2 = $("#txtAddress2").val(); 
     var city = $("#txtCity").val(); 
     var state = $("#txtState").val(); 
     var zipcode = $("#txtZipcode").val(); 

     var dataToSend = "address=" + address1 + "&city=" + city + "&state=" + state + "&zip=" + zipcode; 

     $.ajax(
     { 
      method: "POST", 
      url: "http://localhost:16859/AddressVerification.svc/AddressVerification", 
      data: dataToSend, 
      success: function (dataReturned) { 
       alert("YAY!!!!!!!"); 
      }, 
      error: function (dataReturned, status, errorT) { 
       alert('error thrown: ' + errorT); 
       alert('status: ' + status); 
       alert('dataReturned: ' + dataReturned.toString()); 
      }, 
      cache: false 
     }); 
    }); 
}); 

내 Web.config의입니다

바인딩이 WCF 엔드 포인트는
<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="ServiceBehavior" name="Service"> 
     <endpoint address="" binding="webHttpBinding" 
      contract="IAddressVerification" behaviorConfiguration="EndpBehavior"/> 
     </service> 
    </services> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

답변

0

사용하도록 구성 되었습니까?

WSDL을 보면, SOAP을 사용하는 바인딩을 사용하고있는 것처럼 보입니다. jQuery를 통해 데이터를 제출하는 방법과 작동하지 않습니다.

webHttpBinding을 사용하십시오. 이렇게하면 서비스가 간단한 웹 요청에 응답 할 수 있습니다.

http://msdn.microsoft.com/en-us/library/bb412176.aspx

편집 : 이것은 주소에 "http의 방법을 노출됩니다

[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, 
    ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "AddressVerification ")] 

: 당신은 또한 다음과 같은 속성으로 AddressVerification OperationContract를 장식 할 것이다 // 로컬 호스트 : 16859/AddressVerification.svc/AddressVerification "을 검색하고 JSON 응답을 반환합니다.

+0

안녕하세요 Rocco !!! 원래 web.config에 질문을 추가했습니다. 내가 제대로 구성했다고 생각해. 어떻게 생각해? –

+0

이 말이 맞습니다. 당신의 서비스'OperationContract'에 어떤 속성이 있습니까? 내 편집을 참조하십시오. – RoccoC5

+0

Rocco !!! 내 잘못이야. 내 jquery 메서드를했다 : 형식 대신 "POST": "게시물".... :(바보 코드 오늘 –