如何向WCF发送XML参数?

3
如何发送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>
2个回答

2

发生了什么?没有XML它能工作吗?那个参数的类型是什么? - Wojciech Kulik
我正在将字符串作为参数传递。它将接受XML字符串 "<value><Root>OSLWATS</Root></value>" 并返回字符串值。我的意思是它将返回 OSLWATS。 - ShutterSoul
@Wojciech---谢谢.....它起作用了....:).我使用了<![CDATA[和]]>,然后它就工作了...:) - ShutterSoul
我的意思是我将在响应中获得完整的XML。结果将显示<Root><name>OSLWATS</name></Root>。首先,这是否可能? - ShutterSoul
我已经返回了一个字符串中的XML。但是我得到了“内部服务器错误”:( - ShutterSoul
显示剩余8条评论

0

你将使用Base64加密并传递,在服务器客户端上可以解密。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接