Axios GET请求传递整数参数时出现类型错误的求助
解决Axios GET请求传递整数参数时的类型错误问题
看起来你遇到的问题是因为encodeURIComponent()函数期望接收字符串类型的参数,但this.look_data.value是整数类型——虽然JavaScript通常会做隐式类型转换,但在某些场景下(比如严格模式或特定运行环境),这种隐式转换可能不生效,从而触发"Value of key 'this.look_data.value' is not a string!"的错误。另外我还注意到你代码里有个小笔误:give.data应该是response.data,这个也需要修正。
下面给你两种可行的解决方案:
方案1:显式转换参数为字符串
手动把整数转成字符串后再传入encodeURIComponent(),确保参数类型符合要求:
axios.get(window.App.targetURL+'/ccc/xxx?getint='+encodeURIComponent(String(this.look_data.value)), window.App.configuration) .then(response => { this.value = response.data; this.values = true; }) .catch(e => { alert(e); })
方案2:使用Axios的params配置项(推荐)
Axios内置了参数处理逻辑,会自动帮你完成类型转换和URL编码,不用手动拼接URL,代码更清晰也更不容易出错:
axios.get(window.App.targetURL+'/ccc/xxx', { // 合并原有配置 ...window.App.configuration, params: { getint: this.look_data.value // 整数类型会被自动转成字符串并编码 } }) .then(response => { this.value = response.data; this.values = true; }) .catch(e => { alert(e); })
这个方案不仅解决了类型问题,还避免了手动拼接URL可能带来的其他问题(比如特殊字符处理、参数格式错误等)。
内容的提问来源于stack exchange,提问作者JustStartedProgramming
相关产品推荐
相关产品推荐

