[英]Jquery Ajax and WCF Webservice nightmare
我創建了一個WCF Web服務方法,該方法接收兩個參數(字符串)並返回XmlElement。
我有一個ASP.NET頁面,該頁面帶有對該方法的JQuery AJAX調用。 它不起作用,在線文檔使我一時變得更加困惑。 需要記住的一些方面是,現在我們將參數發送為url編碼。
在這一點上,我不介意將整個內容轉換為JSON。 我只需要使這個工作。
當我使用WCF測試客戶端(Visual Studio 2010)時,我的Web服務可以完美運行。 但是我只是無法讓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
<?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>
您的WCF端點配置為使用哪些綁定?
查看您的WSDL,看起來您正在使用使用SOAP的綁定-這不適用於您嘗試通過jQuery提交數據的方式。
看一下使用webHttpBinding
。 這將使服務能夠響應簡單的Web請求。
http://msdn.microsoft.com/en-us/library/bb412176.aspx
編輯:您還將要裝飾具有以下屬性的AddressVerification OperationContract:
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "AddressVerification ")]
這將在地址“ http:// localhost:16859 / AddressVerification.svc / AddressVerification”處公開該方法,並返回JSON響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.