reactjs - 传递路径参数在 URL 调用中返回 object%20Object
问题描述
我正在尝试在 ReactJs 中的 axios 调用中根据我的{accountId}值进行一般 axios 调用,如下所示:
服务工厂.js
const devURl = 'https://localhost:3000/service/';
const authToken = '<some_string>';
const accounts = (event) => (
(accountId) => (
axios.get(devURl + `accounts/${accountId}/health`, {
headers: {
Authorization: authToken,
},
}).then(response => ({ data: response.data })).catch(error => ({ error }))
)
);
但是当我在我的 componentDidMount() 中调用它时,如下所示:
constructor(props) {
super(props);
this.executeService = this.executeService.bind(this)
this.state = {
id : 707
};
}
componentDidMount() {
this.Service = ServiceFactory.accounts();
this.executeService();
}
executeService() {
console.log("in execute")
this.Service({accountId: this.state.id}).then((response) => {
if (response.data) {
this.serviceSuccess(response.data);
} else {
this.serviceFailure(response.error);
}
});
}
它在我的浏览器中返回这个 URL:
https://localhost:3000/service/accounts/[object%20Object]/health
谁能帮助我如何将路径参数正确传递给我的 axios GET() 调用。
解决方案
也许您的转译器无法将此行转译为纯字符串代码:
axios.get(devURl + `accounts/${accountId}/health`, {
所以改成:
axios.get(`${devURl}accounts/${accountId}/health`, {
或者
axios.get(devURl + "accounts/" + accountId + "/health", {
推荐阅读
- c# - 自动完成在 C# 和 Unity 的 Atom 中不起作用
- android - 当应用程序被用户或后台应用程序杀死时,活动未启动单击 FCM 推送通知
- angular - 在 Angular 中进行单元测试时出错:-“TypeError: Illegal constructor at new SignalrWindow”
- html - 我的背景 遇到问题 如何解决?
- java - 在 WebLogic -DUseSunHttpHandler=true 上添加服务器启动参数
- javascript - 带有可排序的工具提示给出了意想不到的结果
- jakarta-mail - javamail 抛出 javax.activation.UnsupportedDataTypeException: text/related; 字符集=UTF-8
- javascript - 选中的复选框不适用于扩展 chrome
- c# - 为什么我没有在 C# winForms 应用程序中获取 datagridview 可编辑列的最后一行的值?
- android - 从 shell 运行 react native android 应用程序时出错