c# - 如何将 xml 参数发送到 WCF?

标签 c# jquery ajax json wcf

如何发送xml参数调用WCF方法?我的客户端代码在使用 jQuery 的 AJAX,JSON 中。我想将 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 放入其中
<![CDATA[]]>

还有一些字符串标签的hack,但我不知道是否有必要(也许只有CDATA标签就足够了):
WCF RESTFul service - Pass XML as string to service

关于c# - 如何将 xml 参数发送到 WCF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17259218/

相关文章:

c# - 本地连接但不通过网络连接的客户端服务器(服务)应用程序

c# - Asp.net 身份哈希安全吗?

c# - 带有依赖注入(inject)的工厂模式,serviceProvider 总是返回 null

Javascript innerHTML/jQuery .html() 每个 id 只能工作一次?

javascript - jQuery 自定义选择方法仅适用于集合的第一个元素

javascript - Ajax/Json - 当列名是数字时如何将返回数据保存在变量中?

jquery - HTML 复选框元素未将值传递到 jQuery 和 AJAX 字符串中

c# - 无锁线程安全状态同步?

javascript - 如何在将鼠标悬停在按钮上时将背景图像更改为另一个图像?

jQuery使用经典asp在div中记录分页