You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Axios传递含#的URL异常问题求助

解决Axios中传递含#的URL被截断的问题

问题的核心是**#在URL中属于片段标识符(Fragment Identifier)**,这部分内容由浏览器端解析,不会被发送到服务器,所以直接写在URL里时,Axios只会发送#之前的部分。

要让#被正确传递到服务器,你需要对#进行URL编码,将其转换为%23,具体有两种实现方式:

1. 手动编码URL后直接传递

把原URL中的#替换成%23,直接传给Axios:

axios.get('https://jsonplaceholder.typicode.com/todos/%23abc?pt=1')
  .then(res => console.log(res.data))
  .catch(err => console.error(err));

2. 动态编码路径部分

如果URL是动态生成的,使用encodeURIComponent()方法对包含#的路径片段进行编码:

const targetPath = '#abc';
const encodedPath = encodeURIComponent(targetPath);

axios.get(`https://jsonplaceholder.typicode.com/todos/${encodedPath}?pt=1`)
  .then(res => console.log(res.data))
  .catch(err => console.error(err));

这样处理后,服务器收到的请求URL就会完整包含%23abc?pt=1部分,不会被截断。

内容的提问来源于stack exchange,提问作者Pawan Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:12:03