反应原生axios调用抛出403,但邮递员正确输出了数据

时间:2019-12-27 12:39:58

标签: react-native redux axios

我正在开发一个RN应用,其中包含Redux。现在,我可以在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的令牌存储为1,因此从此调度程序本身传递令牌。

如果您需要任何澄清/更多信息,请告诉我,然后告诉我。预先感谢

1 个答案:

答案 0 :(得分:1)

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