reactjs - React Redux Axios Api Get 调用带路径参数
问题描述
我正在尝试使用路径变量对我的后端服务器进行 axios get 调用,并将响应数据保存到存储中。不幸的是,它只适用于没有路径变量的 get 调用。我仍然可以在控制台中记录响应,但我无法将 response.data 发送到商店
fetchByCardNumber: (CardNumber) => axios.get(baseURL+'loyalty/loyaltyCustomer/card/'+ CardNumber)
export const fetchByCardNumber = (CardNumber) => dispatch => {
LoyaltyAPI().fetchByCardNumber(CardNumber)
.then(response => {
if (response.status !== 200){
dispatch(Customer(null))
} else {
dispatch(Customer(response.data))
}
}).catch(error => {
return error;
})
}
但是,无论我在 axios 中使用没有 pathVaribale 的 url,都可以调用它。
例如:fetchCards: () => axios.get(baseURL+'loyalty/loyaltyCard/all')
使用上面的 URL 时,我可以将响应发送到商店并获取它。
解决方案
问题在于将响应发送到商店的方式。下面的代码有效。
export const fetchByCardNumber = (CardNumber) => dispatch => {
LoyaltyAPI().fetchByCardNumber(CardNumber)
.then(response => {
if (response.status !== 200){
store.dispatch(SelectedCustomer(null))
} else {
store.dispatch(SelectedCustomer(response.data))
}
}).catch(error => {
return error;
})
}
推荐阅读
- java - Java:如何在三元运算符中访问实例变量?
- python - 跨页面或等效的 Sphinx 脚注
- python - 如何在linux命令中间传递来自文本文件的参数
- html - 如何将 Navbar-Brand 居中在链接项的中间
- javascript - Javascript Uncaught TypeError:.split 不是函数
- sql-server - 显示第三个表中的所有记录,但在其他 2 个表中未找到的记录除外
- c# - 如何修复“Cudafy 在 CUDA 上出现错误 719”
- haskell - 非内射封闭类型族
- regex - 正则表达式匹配除以数字开头的字符串以外的所有内容
- ios - 通过 Cocoapods 在依赖目标中共享库