js 用ajax的POST方式向后台传数据时如何应对JSON中各种特殊字符

ltolll 2017-04-20 05:49:48
可能出现所有可打印字符,单引号,双引号,~@#¥%……&*() /*-+【】{}[] \<>,.?
...全文
679 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ltolll 2017-04-21
  • 打赏
  • 举报
回复
自己找到一种解决方案:

        var postStr = "todJson=" + escEncode(todJson)
……
	function escEncode(strSendToServer){
		strSendToServer = strSendToServer.replace(/\%/g,'%25').replace(/\&/g,'%26').replace(/\#/g,'%23');
		strSendToServer = strSendToServer.replace(/\+/g,'%2B').replace(/\//g,'%2F').replace(/\\/g,'%5C');
		strSendToServer = strSendToServer.replace(/\=/g,'%3D').replace(/\?/g,'%3F').replace(/\ /g,'%20');
		strSendToServer = strSendToServer.replace(/\./g,'%2E').replace(/\:/g,'%3A');
		return strSendToServer;
	}
只要发现不能解析的符号就继续向escEncode函数中添加,虽然有点繁琐,但还算整洁。
ltolll 2017-04-21
  • 打赏
  • 举报
回复
引用 3 楼 lzf200906306 的回复:
http://blog.csdn.net/peterxiaoq/article/details/50913253 可以看看这个
此方案用到了escape,encodeURIComponent函数,貌似他们有各自的局限: escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
幻星☞飞 2017-04-21
  • 打赏
  • 举报
回复
http://blog.csdn.net/peterxiaoq/article/details/50913253 可以看看这个
ltolll 2017-04-20
  • 打赏
  • 举报
回复
引用 1 楼 lrx2011 的回复:
参考http://www.cnblogs.com/hanyun/archive/2012/05/11/2495724.html
这是PHP代码,在下看不懂,我只会JAVA.
podd 2017-04-20
  • 打赏
  • 举报
回复
参考http://www.cnblogs.com/hanyun/archive/2012/05/11/2495724.html

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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