js前端和php后端对url编解码处理方式不一致的问题

js前端和php后端对url编解码处理方式不一致的问题

蓝科迪梦
2022-09-25 / 0 评论 / 109 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年09月25日,已超过550天没有更新,若内容或图片失效,请留言反馈。
问题:php的解码方式是urldecode,前端的编码方式有escape,encodeURI,encodeURIComponent这三种,escape出来的url,php可以正常解析出来,但是escape不支持ES6,其他两种方式php不能解析正确



解决方法一,根据情况转:


URI部分用encodeURI,参数部分用encodeURIComponent,这样才是相对完美的编码方式。



比如`https://example.com/?next=abc...`,得到的结果分别是:



encodeURI(`https://example.com/?next=abc...`):



"https://example.com/?next=abc.com/def&encoding=utf-8"

encodeURIComponent(`https://example.com/?next=abc...`):



"https%3A%2F%2Fexample.com%2F%3Fnext%3Dabc.com%2Fdef%26encoding%3Dutf-8"

encodeURI(https://example.com/?next=${encodeURIComponent('abc.com/def')}&encoding=${encodeURIComponent('utf-8')}):



"https://example.com/?next=abc.com%252Fdef&encoding=utf-8"





解决方法二,base64:

json_encode()用eval()还原
base64_decode()用base64_encode()还原



解决方法三,最简单的方法,前后端都不使用转义!
0

评论

博主关闭了所有页面的评论