如何修复未经授权的axios /反应

时间:2019-04-15 19:06:58

标签: reactjs axios

未经授权的axios /反应响应

嗨,朋友们,我正在尝试通过Axios和React连接到api,但是出现一条错误消息,提示我没有访问权限,这是我的操作:

import {SHOW_PROMOTIONS} from './action-types';
import axios from 'axios';

export const showPromo = () => async dispatch =>{
    const url= 'https://payment-promotions-dev.travelit.com.ar/api/promotions/packages/';
    let config = {
        "Content-type": "application/x-www-form-urlencoded",
        "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtdW5kaWdlYSIsImp0aSI6ImQ0ODE1ZDk4LTJlYmQtNDRjYS04NGViLTU4N2JjNTY5NzgzZCIsImlhdCI6MTU1NTM0ODUwMCwibm9tYnJlIjoiTXVuZGlnZWEiLCJhcHBsaWNhdGlvbklkIjoiMSIsInBhaXNJZCI6IjEiLCJ0aXBvQXBsaWNhY2lvbklkIjoiMSIsImFjdGl2YSI6IlRydWUiLCJuYmYiOjE1NTUzNDg1MDAsImV4cCI6MTU1NTk1MzMwMCwiaXNzIjoiVHJhdmVsSVQiLCJhdWQiOiJUcmF2ZWxJVCJ9.o4Tv6Cw1Mj5xmHIQQ7abm6k6Ean6s6eQ3IDEkHY6Frk"
    };

    axios.get('http://<host>:<port>/<path>', url,config)
        .then((res) => {
            console.log("RESPONSE RECEIVED: ", res);
        })
        .catch((err) => {
            console.log("AXIOS ERROR: ", err);
        })

    const respuesta = await axios.get(url,config);
    dispatch({
        type: SHOW_PROMOTIONS,
        payload: respuesta.data
    })
}

当我执行组件时,出现此错误:(请参见下图https://imgur.com/LuKnBv9

令牌位于其相应的标头上,我似乎无法识别我做错了什么。

我什至尝试通过200邮递员来完成请求:(请参见image2 https://imgur.com/7UFksPR

感谢帮助人员!

1 个答案:

答案 0 :(得分:1)

您当前实际上并未为请求指定headers。您需要将headers属性添加到config对象,并将所需的标头放入该属性。另外,正如您所说的那样,您还需要为type请求标头指定Authorization,例如Bearer

const config = {
  headers: {
    "Content-type": "application/x-www-form-urlencoded",
    "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtdW5kaWdlYSIsImp0aSI6ImQ0ODE1ZDk4LTJlYmQtNDRjYS04NGViLTU4N2JjNTY5NzgzZCIsImlhdCI6MTU1NTM0ODUwMCwibm9tYnJlIjoiTXVuZGlnZWEiLCJhcHBsaWNhdGlvbklkIjoiMSIsInBhaXNJZCI6IjEiLCJ0aXBvQXBsaWNhY2lvbklkIjoiMSIsImFjdGl2YSI6IlRydWUiLCJuYmYiOjE1NTUzNDg1MDAsImV4cCI6MTU1NTk1MzMwMCwiaXNzIjoiVHJhdmVsSVQiLCJhdWQiOiJUcmF2ZWxJVCJ9.o4Tv6Cw1Mj5xmHIQQ7abm6k6Ean6s6eQ3IDEkHY6Frk"
  }
};

希望有帮助!