axios 无法在react中获取响应标头

w6mmgewl  于 2023-08-04  发布在  iOS
关注(0)|答案(1)|浏览(128)
const sendRequest = async () => {
  try {
    const response = await axios.post("http://localhost:8080/auth/login", {
      username: username,
      password: password,
    });

    console.log(response.data);

    const authorizationHeader = response.headers["Authorization"];
    if (authorizationHeader) {
      localStorage.setItem("token", authorizationHeader);
      console.log("Token stored in localStorage.");
    } else {
      console.log("No Authorization header received.");
    }
  } catch (error) {
    console.log("Error:", error);
  }
};

字符串
这是我上面的功能,但即使我看到的标题,我想得到“授权”在两个网络上的 chrome 浏览器和 Postman 我不能得到它。我做错了什么?

klh5stk1

klh5stk11#

const authorizationHeader = response.headers['authorization'];

字符串
将标题名称的第一个字母改为小写解决了我的问题

相关问题