2011-05-27 108 views
0

我想將包含html標籤的參數作爲其值傳遞給webmethod。但它似乎不工作,我只是得到錯誤。有可能這樣做。你能建議任何其他方式來做同樣的事情嗎?如何將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 
     }); 
    } 
+0

您是否嘗試過的HTML編碼嗎? – bevacqua 2011-05-27 04:42:55

+0

您的意思是轉換所有那些< >符號?沒有 – NewBie 2011-05-27 04:47:14

回答

0

您應該在客戶端對URL進行編碼並在客戶端進行解碼。基本上它將「<」更改爲「<」。

+0

我該怎麼做? – NewBie 2011-05-27 04:58:03

+0

http://stackoverflow.com/questions/3541711/javascript-url-encode – 2011-05-27 13:23:23

0

也許一個更穩定和更安全的解決方案是在發送數據之前對數據進行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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

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(); 
} 
+0

我怎麼能解碼這回到它是什麼...? – NewBie 2011-05-27 05:08:44

+0

HttpUtility.HtmlDecode – bevacqua 2011-05-27 10:40:32