旧方法:
wef.config里加上节点
<httpRuntime requestValidationMode="2.0" />
下面可选
validateRequest="false"
<httpRuntime requestValidationMode="2.0" />
下面可选
validateRequest="false"
新方法:
一:前端对富文本字符串进行encodeURI编码,服务端进行HttpUtility.UrlDecode解码操作
前端:
var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
$(function() {
$.ajax({
type: "post",
url: "TestHandle.ashx",
data: { Title: 'jack', Content: encodeURI(str) },
success: function (data) {
$("#div").html(data);
}
});
});
后端:
public void ProcessRequest(HttpContext context)
{
string str = context.Request["content"];
string content = HttpUtility.UrlDecode(str);
context.Response.ContentType = "text/plain";
context.Response.Write(content);
}
二:前端不以form的方式提交,直接以json方式提交,服务端从request的body中读取数据,然后反序列化,得到信息
前端:
var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
var temp = { Title: 'jack', Content: str };
$.ajax({
type: "post",
url: "TestHandle.ashx",
contentType:"application/json;charset=utf-8",
data: JSON.stringify(temp),
success: function (data) {
$("#div").html(data);
}
});
后端:
string bodyText;
using (var bodyReader = new System.IO.StreamReader(context.Request.InputStream))
{
bodyText = bodyReader.ReadToEnd();
}
dynamic bodyObj = JsonConvert.DeserializeObject(bodyText);
context.Response.ContentType = "text/plain";
context.Response.Write(bodyObj.Content);