How to pass html tags to a webmethod?
I'm trying to pass a parameter containing html tags as its value to a webmethod. But it seems not working and i'm only getting the error. Is that po开发者_开发技巧ssible to do so. Can you suggest any other way to do the same.
htmlContent = htmlContent + document.getElementById("divFollowsTestDiv").innerHTML;
// Here im storing the tags and the following method calls the 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
});
}
You should URL Encode on the client side and decode on the client side. essentially it changes "<" to "<".
Perhaps a more stable and safer solution would be to base64 the data before it is sent? Base 64 will convert the entire string to a strict set of a-zA-Z0-9 and an equals sign. In ASP you can base64 using this: 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();
}
精彩评论