【问题标题】:React native axios call throws 403 but postman correctly outputs the data反应本机 axios 调用抛出 403 但邮递员正确输出数据
【发布时间】:2019-12-27 12:39:58
【问题描述】:

我正在开发一个包含 redux 的 RN 应用程序。现在我可以在 jwt 的帮助下登录,但是当我尝试从我的其他组件获取数据时,它给了我 403 错误。请在下面找到相关代码。

这是我的减速器:

 const initState = {
        isLoadingCollegeDashList : false,
        collegeDashList:{},
        collegeDashListFail:false
    }
    const collegeReducer = ( state = initState, action) => {
        switch(action.type){
            case 'IS_LOADING_COLLEGE_DASH_LIST' : 
            return{
                ...state,
                isLoadingCollegeDashList: true,
                collegeDashList : false
            }
            case 'COLLEGE_DASH_LIST' : 
            return {
                ...state,
                isLoadingCollegeDashList : false,
                collegeDashList : true,
                userData : action.userData
            }
            case  'COLLEGE_DASH_LIST_FAIL' : 
            return{
                ...state,
                isLoadingCollegeDashList:false,
                collegeDashList: false,
                collegeDashListFail: action.error
            }
            default : 
            return state 
        }
    }
and here's my action that's making get request

export const populateCollege = (token) => {
    const headers = {
        'api-secret' : ...secret...,
        'authorization':...authToken...,
        'Content-Type': 'application/json',

      }
    return dispatch => {
      dispatch(isLoadingCollegeDashList(true));
      return axios.get( '...api/api/...', {
      },{
          headers:headers,
      })
      .then((response) => {
        if(response.status < 300){
          dispatch(isLoadingCollegeDashList(false))
          dispatch(collegeDashList(response))
          console.log(response);
        }
        else{
          response.json().then((responseJSON) => {
            console.log("responseJSON",responseJSON);
            dispatch(isLoadingCollegeDashList(false))
            dispatch(collegeDashListFail(responseJSON.message))
          })
        }
      })
      .catch((error) => {
        console.log("error",error);
        dispatch(isLoadingCollegeDashList(false))
        dispatch(collegeDashListFail(error))

      })
    }
  }

export const isLoadingCollegeDashList = (bool) => {
    return{
      type:'IS_LOADING_COLLEGE_DASH_LIST',
      isLoadingCollegeDashList:bool
    }
  }

  export const collegeDashList = (userData) => {
    return{
      type:'COLLEGE_DASH_LIST',
      userData
    }
  }

  export const collegeDashListFail = (error) => {
    return{
      type:'COLLEGE_DASH_LIST_FAIL',
      error
    }
  }

如果你想检查,我会调用这里的操作

const mapDispatchToProps = dispatch => ({
  populateCollege : (token) => dispatch(actions.populateCollege({token}))
});

PS 我现在将令牌存储在一个状态,因此从这个调度本身传递令牌。

如果您需要任何澄清/更多信息,请告诉我,然后告诉我。提前致谢

【问题讨论】:

    标签: react-native redux axios


    【解决方案1】:

    确保您在令牌之前具有授权架构。根据您的授权详细信息,架构可以类似于 Basic、Bearer 或任何其他值。 (例如,授权:Bearer TOKEN)。 此外,尝试在创建 axios 实例时重用您的 auth 标头,这样您就不需要在每次调用时都注入它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2020-11-02
      • 2020-05-27
      • 2021-01-14
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多