我想使用AJAX将一些HTML内容从视图发布到控制器。但是我不知道如何在控制器中获取发布的HTML。
var form = $('.Container').html();
$.ajax({
url: "/Public/Support",
type: 'POST',
cache: false,
contentType: "test/plain",
dataType: "html",
data: { form: form },
success: function (data) {
if (data.Result == false) {
} else {
}
},
});
答案 0 :(得分:0)
默认情况下,由于安全原因(cross-site scripting (XSS)攻击和HTML注入攻击),禁止将内容发布到服务器上。您必须允许使用数据注释中的属性。
**型号:**
public class SomeModel
{
[AllowHtml]
public string HtmlContent { get; set; }
}
above will allow HTML content to post, however, if you are saving this content to
database save it encoded and retrieve as it is and display by decoding it
var form = $('.Container').html();
$.ajax({
url: "/Public/Support",
type: 'POST',
cache: false,
contentType: "test/plain",
dataType: "html",
data: { form: htmlEntities(form) },
success: function (data) {
if (data.Result == false) {
} else {
}
},
});
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g,
'<').replace(/>/g, '>').replace(/"/g, '"');
} /// you can reverse this funtion to decode to display
或者您也可以在服务器端使用不同的编码和解码技术