为什么axios GET调用无法用于基本身份验证?

时间:2019-01-31 07:31:18

标签: javascript axios fetch-api

我在http://localhost:8085/api/v1/users处有一些用户数据,该数据已通过基本身份验证保护。如果我从浏览器(URL框)访问此URL,它将发送一个GET请求,并出现登录(身份验证)提示。填写正确的详细信息后,浏览器会向我显示数据。甚至像POSTMAN这样的客户端应用程序都可以正常工作。

但是axios调用不会发生这种情况。以下是GET调用:

axios
    .get(
      "http://localhost:8085/api/v1/users",
      {},
      {
        auth: {
          username: "sample@gmail.com",
          password: "pass@"
        }
      }
    )
    .then(resp => console.log(resp))
    .catch(err => console.log(err));

我什至尝试以下方法:

axios({
      method: "get",
      baseURL: endpoint,
      auth: {
            username: "sample@gmail.com",
            password: "pass@"
           }
      })
      .then(resp => console.log(resp))
      .catch(err => console.log(err));

我认为axios没有向请求添加基本身份验证标头。 我在这里做什么错?请提出建议。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用POST而不是GET?如果这不起作用,则可以尝试将用户名和密码放在headers属性而不是auth属性中。您收到什么错误?

希望这会有所帮助。

相关问题