【问题标题】:How to pass html tags to a webmethod?如何将html标签传递给webmethod?
【发布时间】:2011-05-27 04:40:35
【问题描述】:

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

 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
        });
    }

【问题讨论】:

  • 你试过html编码吗?
  • 你的意思是转换所有那些 符号?没有

标签: javascript jquery asp.net html


【解决方案1】:

您应该在客户端进行 URL 编码并在客户端进行解码。本质上它将“

【讨论】:

    【解决方案2】:

    也许更稳定和更安全的解决方案是在发送数据之前对数据进行 base64 处理? Base 64 会将整个字符串转换为一组严格的 a-zA-Z0-9 和一个等号。 在 ASP 中,您可以使用 base64: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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 2011-06-05
      • 2023-03-06
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多