前后端联调中URLEncoder.encode空格问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34792830/article/details/84316257

最近在前后端联调中,前端使用js的

encodeURIComponent("中国人   好样的");

而如果中间出现空格,那么会将空格转为%2B,

后端使用java的

URLEncoder.encode("中国人   好样的","utf-8");

那么会将空格转为+,

这时问题来了,你会发现前后端联调结果不一致,解决方法如下:

String appValue = URLEncoder.encode("中国人   好样的","utf-8");
appValue = appValue.replace("+","%20");

这样前后端结果一致了。

猜你喜欢

转载自blog.csdn.net/qq_34792830/article/details/84316257
今日推荐