首页 > 解决方案 > 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 时,我可以将响应发送到商店并获取它。

标签: reactjsreduxreact-reduxaxiosreact-hooks

解决方案


问题在于将响应发送到商店的方式。下面的代码有效。

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;
        })
}

推荐阅读