如何将xml参数发送到WCF

本文关键字:WCF 参数 xml | 更新日期: 2023-09-27 18:19:37

如何发送xml参数来调用WCF方法?我的客户端代码在AJAX,JSON中使用jQuery。我想将xml值作为参数传递。如何传递xml值?我的xml值是

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

我的客户端代码——

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

如何将xml参数发送到WCF

我不确定它是否有效,但请尝试将XML放入
<![CDATA[]]>

字符串标签也有一些破解,但我不知道是否有必要(也许只有CDATA标签就足够了):
WCF RESTFul服务-将XML作为字符串传递给服务

您将通过base64加密并传递它,在服务器客户端上您可以解密