web页面之间url传输时字符串编码问题

起初觉得编码只是为了放置黑客窃取数据,后来发现编码后,对特殊字符进行了处理,所以可以放置&将参数截断。
因为url传值的时候是这样写的:

xxx.html?str1=xxx&str2=xxx&str3=xxx

如果str1中含有‘&’符号的话,提取str1中数据的时候以&分割就会将str1中的数据截断,造成数据丢失。
在传输页面使用encodeURIComponent进行编码:

string str1 = encodeURIComponent($('#txt1').val());
string str2 = encodeURIComponent($('#txt2').val());
string str3 = encodeURIComponent($('#txt3').val());

在接收页面使用decodeURIComponent进行解码:

string _str1 = decodeURIComponent(q('str1'));
string _str2 = decodeURIComponent(q('str2'));
string _str3 = decodeURIComponent(q('str3'));

如果是传到后台的话,那么在后台进行解码就行了,这个具体语言具体分析了。
一定要注意的是,如果传递的是用户输入的参数,由于存在不可控性,所以最好进行编码。

发布了37 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38404507/article/details/102968147