当URL中有#符号时,axios会将#后面的部分作为URL的一部分放在请求参数中,导致请求失败。为了解决这个问题,可以使用encodeURIComponent函数将#符号及其后面的部分进行编码,然后再将编码后的参数加到URL中。示例代码如下:
const url = "http://example.com/path#hash";
const params = { key: "value" };
const q = Object.keys(params)
.map(key => ${encodeURIComponent(key)}=${encodeURIComponent(params[key])}
)
.join('&');
axios.get(${url.split("#")[0]}?${q}#${encodeURIComponent(window.location.hash)}
)
.then(response => console.log(response))
.catch(error => console.error(error));