【问题标题】:how to send xml parameter to WCF?如何将xml参数发送到WCF?
【发布时间】:2013-06-20 00:00:20
【问题描述】:

如何发送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>

【问题讨论】:

    标签: c# jquery ajax json wcf


    【解决方案1】:

    我不确定它是否会起作用,但请尝试将 XML 放入其中
    &lt;![CDATA[]]&gt;

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

    【讨论】:

    • 会发生什么?没有 XML 它可以工作吗?该参数的类型是什么?
    • 我将字符串作为参数传递。它将接受“OSLWATS”的 XML 字符串,它会返回字符串值。我的意思是它会返回 OSLWATS。
    • @Wojciech---谢谢.....它可以工作....:)。我使用了 然后它的工作......跨度>
    • 我还有一个疑问,如何将完整的 xml 字符串作为参数传递并在结果中获取该 xml 字符串。我的意思是它将返回 mydata
    • how to pass full xml string 同样的方式:-)?在 CDATA 标记内? and get that xml string in result我不明白? WCF 服务将 XML 作为字符串返回应该没有问题。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多