如何将html标签传递给webmethod?

时间:2011-05-27 04:40:36

标签: javascript jquery asp.net html

我正在尝试将包含html标记的参数作为其值传递给webmethod。但它似乎不起作用,我只是得到错误。这可能吗?你能否提出任何其他方法来做同样的事情。

 htmlContent = htmlContent + document.getElementById("divFollowsTestDiv").innerHTML;

//这里我存储标签和以下方法调用webmethod。

    function StoreSessionForHtml(htmlContent) {


        var requesthtmlContentParameter = '{' +
                        'htmlContent:"' + htmlContent + '"}';
        $.ajax({
            type: "POST",
            url: "Webtop.aspx/HTMLTableContent",
            data: requesthtmlContentParameter,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert("Success", msg.d);
            }, //Event that'll be fired on Success
            error: function() {
                alert("Try Again");

            } //Event that'll be fired on Error
        });
    }

2 个答案:

答案 0 :(得分:0)

您应该在客户端进行URL编码并在客户端进行解码。基本上它改变了“<”到“<”。

答案 1 :(得分:0)

也许一个更稳定,更安全的解决方案是在数据发送之前对其进行base64? Base 64将整个字符串转换为严格的a-zA-Z0-9和等号。 在ASP中,您可以使用以下代码来完成基础:http://www.freevbcode.com/ShowCode.asp?ID=5248

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
    var output = new StringMaker();
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    while (i < input.length) {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
   }

   return output.toString();
}