【问题标题】:Why my Authorization value not passed through Request Headers?为什么我的授权值没有通过请求标头传递?
【发布时间】:2022-11-27 17:39:02
【问题描述】:

我尝试使用我的发布请求通过配置发送我的令牌值,但它没有出现在请求标头中最终给我 500 错误 在这里,是以下代码:



export const moneyTransfer = (id, account, amount) => async ( dispatch, getState) => {
  try {
    dispatch({
      type: MONEY_TRANSFER_REQUEST,
    });

    const {
      userLogin: { userInfo },
    } = getState();

    const config = {
      headers: {
        Authorization: `Bearer ${userInfo.token}`,
      },
    };
    console.log(config);
    const { data } = await axios.post(
      `http://localhost:6969/v1/api/transferToAccountByAccountNo?senderAccountNo=${id}&receiverAccountNo=${account}&amount=${amount}`,config );
    console.log(data);
    dispatch({
      type: MONEY_TRANSFER_SUCCESS,
    });
  } catch (error) {
    dispatch({
      type: MONEY_TRANSFER_FAIL,
      payload:
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message,
    });
  }
};

但我无法在我的 chrome 请求标头中看到它:

POST /v1/api/transferToAccountByAccountNo?senderAccountNo=5&receiverAccountNo=2&amount=123 HTTP/1.1
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-IN,en-US;q=0.9,en;q=0.8
Connection: keep-alive
Content-Length: 103
Content-Type: application/json
Host: localhost:6969
Origin: http://localhost:3000
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
sec-ch-ua: "Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"

【问题讨论】:

  • 试试这个 const config = headers: { Authorization: Bearer ${userInfo.token}, }

标签: reactjs redux axios authorization token


【解决方案1】:

这是因为,在 Axios POST API 中,第二个参数是主体对象,第三个参数是配置。

因此,如果您在第二个参数中传递一个 null 或空对象,并将配置移动到第三个参数,它将起作用

 const { data } = await axios.post(
  `http://localhost:6969/v1/api/transferToAccountByAccountNo?senderAccountNo=${id}&receiverAccountNo=${account}&amount=${amount}`,{}, config );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 2018-03-11
    • 1970-01-01
    • 2020-05-10
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多