2012-01-27 13 views
1

2つのパラメータstringを受け取り、XmlElementを返すWCF Webサービスメソッドを作成しました。Jquery AjaxとWCF Webserviceの悪夢

私はそのメソッドへのJQuery AJAX呼び出しでASP.NETページを持っています。それは動作していません。オンラインのドキュメンテーションでは、分かりにくくなってきています。いくつかの側面を念頭に置いておくと、今はパラメータをURLエンコードして送信しています。

この時点で、すべてをJSONにすることはできません。私はちょうどこれを働かせる必要があります。

私のWebサービスは、WCFテストクライアント(Visual Studio 2010)を使用すると完璧に動作します。しかし、私はJQuery AJAXにそれを話すことができません。私は間違った要求のエラーを取得します。

詳細が必要な場合は、お気軽にお問い合わせください。ここで

は、Webサービスのための私のWSDLである:ここでは

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

は私のjQueryのです:ここでは

$(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をご覧ください。これにより、サービスは簡単なWeb要求に応答できます。

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

EDIT:あなたはまた、次の属性を使用してAddressVerification OperationContractを飾ることになるでしょう:// localhostを:16859/

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

これはアドレス「でhttpメソッドを公開しますAddressVerification.svc/AddressVerification "を返し、JSON応答を返します。

+0

Hello Rocco !!! 元の質問に私のweb.configを追加しました。私はそれが適切に構成されていると思う。どう思いますか? –

+0

これは正しく見えます。あなたのサービス 'OperationContract'にはどんな属性がありますか?私の編集を参照してください。 – RoccoC5

+0

Rocco !!!私の悪い。私は私のjqueryメソッドに持っていた: "ポスト"の代わりに: "ポスト" .... :(今日の馬鹿のコード –

関連する問題