Sql server html内容存取

小猪八Q 2015-07-27 09:59:00
我在前段通过文本框输入html内容,
使用ajax 将数据post到后台
由于html的<>等原因,所以需要先转义,怎么post这种json数据

提交到后端以后,再恢复成html内容,这时候空格是正常显示的

但是存取到数据库的时候,原本空格的内容变成了???

哪位大神指导这是什么情况??
...全文
209 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ajianchina 2015-07-27
  • 打赏
  • 举报
回复
nvarchar类型字段默认为unicode编码格式,插入后空格成了?号,我怀疑是插入前的字符串存入后转为unicode格式时出的问题,不过,对于你的问题,我不建议继续在这上面继续深究,看下面吧。 还是按规矩办,html字符串我一般前都先进行编码,你也不要按自己的方式进行转义了,你用ajax进行post的话,我将js编码解码过程代码给你

function HTMLEncode(html) 
{ 
	var temp = document.createElement ("div"); 
	(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html); 
	var output = temp.innerHTML; 
	temp = null; 
	return output; 
} 

function HTMLDecode(text) 
{ 
	var temp = document.createElement("div"); 
	temp.innerHTML = text; 
	var output = temp.innerText || temp.textContent; 
	temp = null; 
	return output; 
} 
存数据库后,存进去后再取字符串,asp.net中可以通过HtmlDecode解码显示,编码、解码方式:

string strHtmlEncode = Server.HtmlEncode(strHtml); 
string strHtmlDecode = Server.HtmlDecode(strHtmlEncode);
小猪八Q 2015-07-27
  • 打赏
  • 举报
回复
引用 1 楼 ajianchina 的回复:
sql语句,该字段类型先说一下。
nvarchar(max)
ajianchina 2015-07-27
  • 打赏
  • 举报
回复
sql语句,该字段类型先说一下。
小猪八Q 2015-07-27
  • 打赏
  • 举报
回复
引用 3 楼 ajianchina 的回复:
nvarchar类型字段默认为unicode编码格式,插入后空格成了?号,我怀疑是插入前的字符串存入后转为unicode格式时出的问题,不过,对于你的问题,我不建议继续在这上面继续深究,看下面吧。 还是按规矩办,html字符串我一般前都先进行编码,你也不要按自己的方式进行转义了,你用ajax进行post的话,我将js编码解码过程代码给你

function HTMLEncode(html) 
{ 
	var temp = document.createElement ("div"); 
	(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html); 
	var output = temp.innerHTML; 
	temp = null; 
	return output; 
} 

function HTMLDecode(text) 
{ 
	var temp = document.createElement("div"); 
	temp.innerHTML = text; 
	var output = temp.innerText || temp.textContent; 
	temp = null; 
	return output; 
} 
存数据库后,存进去后再取字符串,asp.net中可以通过HtmlDecode解码显示,编码、解码方式:

string strHtmlEncode = Server.HtmlEncode(strHtml); 
string strHtmlDecode = Server.HtmlDecode(strHtmlEncode);
谢谢了 已经解决了, 主要可能在于通过json post html 数据时,产生的问题,将html数据单拿出来作为一个新的参数,传过去,解决了 去除了页面验证,前端页不需要htmlEncode

110,473

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧