javascript 使用 Jquery 和 Soap-XML 连接到 WCF Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8729228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:23:46  来源:igfitidea点击:

Connect to WCF Web Service using Jquery and Soap-XML

javascriptweb-servicesjqueryasp.net-ajax

提问by Mahdi jokar

I have a simple WCF web service I tried to connect with jquery and SOAP-XML ( dataType: "xml" ) .but when i send my request i get "BAD REQUEST Error 400" from my server. here is my SOAP-XML:

我有一个简单的 WCF Web 服务,我试图与 jquery 和 SOAP-XML ( dataType: "xml" ) 连接。但是当我发送我的请求时,我从我的服务器收到“BAD REQUEST Error 400”。这是我的 SOAP-XML:

var soapMessage =
            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> \
             <soap:Header> \
             <Action soap:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IService/HelloWorld</Action> \
            </soap:Header> \
            <soap:Body> \
            <HelloWorld xmlns="http://tempuri.org/"> \
            </HelloWorld> \
            </soap:Body> \
            </soap:Envelope>';

and this is my $.Ajax :

这是我的 $.Ajax :

var productServiceUrl = 'http://localhost:3523/Service.svc/HelloWorld';
    $.ajax({
                url: productServiceUrl,
                type: "POST",
                dataType: "xml",
                data: soapMessage,
                complete: endSaveProduct,
                contentType: "text/xml; charset=\"utf-8\"",
                async: true,
                error: function (xhr, textStatus, errorThrown) {
                    alert(errorThrown);

                }

            });

and here is Detail of Request and Responce (I trace this in Google Chrome):

这是请求和响应的详细信息(我在谷歌浏览器中跟踪):

Request Hedear

请求赫迪尔

POST /Service.svc/HelloWorld HTTP/1.1

Host: localhost:3523

Connection: keep-alive

Content-Length: 550

Origin: http://localhost:3523

X-Requested-With: XMLHttpRequest

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.15 Safari/535.2

Content-Type: text/xml; charset="UTF-8"

Accept: application/xml, text/xml, */*; q=0.01

Referer: http://localhost:3523/WcfService.htm

Accept-Encoding: gzip,deflate,sdch

Accept-Language: en-US,en;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
POST /Service.svc/HelloWorld HTTP/1.1

Host: localhost:3523

Connection: keep-alive

Content-Length: 550

Origin: http://localhost:3523

X-Requested-With: XMLHttpRequest

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.15 Safari/535.2

Content-Type: text/xml; charset="UTF-8"

Accept: application/xml, text/xml, */*; q=0.01

Referer: http://localhost:3523/WcfService.htm

Accept-Encoding: gzip,deflate,sdch

Accept-Language: en-US,en;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

and my Response Header

和我的响应标题

    HTTP/1.1 400 Bad Request

    Server: ASP.NET Development Server/10.0.0.0

    Date: Wed, 04 Jan 2012 14:56:06 GMT

    X-AspNet-Version: 4.0.30319

    Cache-Control: private

    Content-Length: 0

    Connection: Close

Request payload:

请求有效载荷:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">                  <soap:Header><Action soap:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/HelloWorld</Action>                 </soap:Header><soap:Body><HelloWorld xmlns="http://tempuri.org/"></HelloWorld></soap:Body> </soap:Envelope>

and this if my WCF web service:

这如果我的 WCF Web 服务:

[OperationContract]
    [WebInvoke(Method = "POST",
                 BodyStyle = WebMessageBodyStyle.Wrapped,
                 ResponseFormat = WebMessageFormat.Xml,
                 RequestFormat = WebMessageFormat.Xml)]
    String HelloWorld();

回答by Mahdi jokar

I found the way. I use this solution : this is my Web Service interface :

我找到了方法。我使用这个解决方案:这是我的 Web 服务界面:

public interface IService
{
    [OperationContract]
    //[WebGet(UriTemplate = "/data?id={value}", ResponseFormat = WebMessageFormat.Json)]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    string GetData(int value);

}

this is my impliment of this function in Web service :

这是我在 Web 服务中对这个功能的实现:

public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

and here is the Script to connect to WCF Web servise:

这是连接到 WCF Web 服务的脚本:

<script type="text/javascript">



    $(document).ready(function () {
        var bhRequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
            "<s:Body>" +
            "<GetData xmlns=\"http://tempuri.org/\">" +
            "<value>10</value>" +
            "</GetData>" +
            "</s:Body>" +
        "</s:Envelope>";
        $("#btnWCFBasicHttp").click(function () {
            $.ajax({
                type: "POST",
                url: "Service.svc",
                data: bhRequest,
                timeout: 10000,
                contentType: "text/xml",
                dataType: "xml",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IService/GetData");
                },
                success: function (data) {
                    $(data).find("GetDataResponse").each(function () {
                        alert($(this).find("GetDataResult").text());
                    });
                },
                error: function (xhr, status, error) {
                    alert(error);

                }
            });
        });
    });


</script>

remember the WCF (url: "Service.svc") is near my html Page.

记住 WCF ( url: "Service.svc") 靠近我的 html 页面。