我正在尝试在ReactJs的axios调用中根据我的 {accountId} 值更改常规的axios调用:
ServiceFactory.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()调用。
答案 0 :(得分:0)
也许您的编译器无法将此行转换为纯字符串代码:
axios.get(devURl + `accounts/${accountId}/health`, {
因此将其更改为:
axios.get(`${devURl}accounts/${accountId}/health`, {
或
axios.get(devURl + "accounts/" + accountId + "/health", {