为什么url拼接+号不显示

因为缺+号导致接口报错请问为什么url里有,但是到query里缺没了,请问我怎么处理,感谢

console.log(mobile)//+8613771705654
url : '../ticket?mobileNo='+mobile+'&verifyCode='+params.password+'&type=2'
url : '../ticket?mobileNo=%2B'+mobile+'&verifyCode='+params.password+'&type=2'//这样子可以但是 `+ 861377...`中间有个空格

1592993219(1).png

阅读 2.6k
3 个回答

值里面有符号的尽量使用encodeURIComponent编译后再解析就不会有问题。

location.href = 'https://segmentfault.com/q/1010000023017018?m=' + encodeURIComponent('+131...');

image.png

取到m的值后记得decodeURIComponent(m)一下;

拼的时候要 encodeURI 编码,要不然 + 在 URL 编码看来就是个空格。

用字符串模板拼接

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
1 篇内容引用
推荐问题