【问题标题】:How do i make a post request with my token coming from local storage如何使用来自本地存储的令牌发出发布请求
【发布时间】:2021-02-19 14:40:34
【问题描述】:

无法使其正常工作。虽然它在用邮递员测试时有效

export const addAdmin = (values) => async (dispatch) => {


var add = localStorage.getItem('user');

const res = await fetch('https://capitalluxurydev.xyz/v1/admin/createadmin', {
    method: 'POST',
    body: JSON.stringify(values),
    headers: {
        Authorization: add,
        'Content-Type': 'application/json; charset=utf-8',
    },
    credentials: 'same-origin',
});
try {
    const data = await res.json();
    console.log(data);

    return;
} catch (error) {
    console.log(error);

    return;
}

};

我不断收到无效 JSON 构造的响应

    {data: null
error: "invalid json construct"
message: ""
statuscode: 422
}

但在 https://hoppscotch.io/ 上完美运行

【问题讨论】:

    标签: javascript reactjs function api fetch


    【解决方案1】:

    我是巴西人,因此我会说葡萄牙语,但我会使用翻译来帮助您。

    O postman coloca automaticamente na Authorization o prefixo Bearer [your_yoken]。 Tente colocar algo como:

        headers: {
            Authorization: `Bearer ${add}`,
            'Content-Type': 'application/json; charset=utf-8',
        }
    

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2016-06-22
      • 2013-03-06
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      相关资源
      最近更新 更多