如何将XML参数发送到WCF?发送到、如何将、参数、WCF

2023-09-10 19:28:45 作者:无情者 —Desdemo.

如何发送XML参数来调用 WCF 的方法?我的客户端code是在 AJAX,JSON 使用jQuery.I想通过XML值parameter.How传递XML值? 我的XML值

 <值GT;<根和GT; MYDATA< /根>< /值GT;
 

我的客户端code -

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <冠军>< /标题>
    <脚本类型=文/ JavaScript的SRC =htt​​p://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的>

          jQuery.support.cors = TRUE;

            VAR bhRequest =< S:信封XMLNS:S = \HTTP://schemas.xmlsoap.org/soap/envelope/ \>中+
                < S:车身>中+
                &所述; GetSMC的xmlns = \的http://tempuri.org/ \>中+
                  <值GT;<根和GT; myvalue的< /根>< /值GT; +
                &所述; / GetSMC>中+
                < / S:车身>中+
            &所述; /秒:信封>中;

            $(文件)。就绪(函数(){
                $(#btnWCFBasicHttp)。点击(函数(){
                    警报(HI);
                    $阿贾克斯({
                        键入:POST,
                        网址:HTTP://本地主机:8130 / MyService.svc / BH /,
                        数据:bhRequest,
                        超时:10000,
                        的contentType:为text / xml,
                        数据类型:XML,
                        beforeSend:功能(XHR){
                            xhr.setRequestHeader(SOAPAction的,http://tempuri.org/IMyService/GetSMC);

                        },
                        成功:功能(数据){
                            警报(成功);
                            $(数据).find(GetSMCResponse)。每个(函数(){

                               的document.getElementById('Label2的)的innerHTML = $(本).find(GetSMCResult)文本()。
                                                         });
                        },
                        错误:函数(XHR,状态,错误){
                            警报(错误);

                        }
                    });
                });
            });
< / SCRIPT>
< /头>
<身体GT;
    <表格ID =Form1的=服务器>
      < D​​IV>
        <输入ID =btnWCFREST类型=按钮值=呼叫使用WCF的JQuery/>
         <标签ID =标签1=服务器文本=标签>< /标签>

        < / DIV>
    < /形式GT;
   < /身体GT;
< / HTML>
 

解决方案

我不知道这是否会工作,但尽量把该XML中 <![CDATA []]>

还有一个用绳子吊牌一些黑客,但我不知道这是否是必要的(也许只有CDATA标签就足够了): WCF RESTful服务 - 通过XML作为字符串来服务

如何让最新文献自动发送至邮箱

How to send xml parameter to call WCF method? My client side code is in AJAX,JSON using jQuery.I want to pass xml value as parameter.How to pass xml value? My xml value is

<value><Root>mydata</Root></value>

My client side code--

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
        <script type="text/javascript" >

          jQuery.support.cors = true;

            var bhRequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "<s:Body>" +
                "<GetSMC xmlns=\"http://tempuri.org/\">" +
                  "<value><Root>MyValue</Root></value>" +
                "</GetSMC>" +
                "</s:Body>" +
            "</s:Envelope>";

            $(document).ready(function () {
                $("#btnWCFBasicHttp").click(function () {
                    alert("hi");
                    $.ajax({
                        type: "POST",
                        url: "http://localhost:8130/MyService.svc/bh/",
                        data: bhRequest,
                        timeout: 10000,
                        contentType: "text/xml",
                        dataType: "xml",
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IMyService/GetSMC");

                        },
                        success: function (data) {
                            alert("success");
                            $(data).find("GetSMCResponse").each(function () {

                               document.getElementById('Label2').innerHTML = $(this).find("GetSMCResult").text();
                                                         });
                        },
                        error: function (xhr, status, error) {
                            alert(error);

                        }
                    });
                });
            });
</script>
</head>
<body>
    <form id="form1" runat="server">
      <div>
        <input id="btnWCFREST" type="button" value="Call WCF using JQuery" />
         <label ID="Label1" runat="server" Text="Label"></label>

        </div>
    </form>
   </body>
</html>

解决方案

I'm not sure if it'll work, but try to put that XML inside <![CDATA[ and ]]>

There is also some hack with string tag, but I don't know if it is necessary (maybe only CDATA tag will be enough): WCF RESTFul service - Pass XML as string to service