【问题标题】:401 error when performing GET request w/ axios使用/axios 执行 GET 请求时出现 401 错误
【发布时间】:2019-02-13 17:01:46
【问题描述】:

我可以在浏览器中查看 JSON 数据,但不能在控制台中查看。我对 axios 不是很熟悉,但我认为问题出在 url 上(见下面的 sn-p)。那,或者我在axios.get { 中遗漏了一些东西。

有什么想法吗?

server.js:

axios.get("https://handshake.[redacted].com/HandshakeWebServices/Odata/Odata.ashx/HS[redacted]?hsf=@UserName=%27[redacted]%27&$select=PreferredName,Initials,JobTitle,Office", {
    crossDomain: true,
    method: "GET",
    credentials: "include",
    mode: "no-cors",
    headers: {
        "Accept": "application/json; odata=verbose"
    }
})
.then(function(res){
  console.log(res.data);
})
.catch(function(e){
  console.log(e);
})

控制台:

GET https://handshake.[redacted].com/HandshakeWebServices/Odata/Odata.ashx/HS[redacted]?hsf=@UserName=[redacted]&$select=PreferredName...etc 401 (Unauthorized)

server.js?1d69:18 Error: Request failed with status code 401
    at createError (createError.js?2d83:16)
    at settle (settle.js?467f:18)
    at XMLHttpRequest.handleLoad (xhr.js?b50d:77)

【问题讨论】:

  • 401 http 代码意味着您无权请求该资源,您可能应该以不同的方式发送用户凭据?不知道那个网站是什么,也不知道你需要什么
  • crossDomain: true 不是 Axios 识别的属性。 (这是一个 jQuery 的东西,几乎从来没有修改过)。不要在这里使用它。
  • 使用模式 no cors 时无法发送标头
  • 鉴于我之前的评论和回答,我认为您需要阅读 Axios 的文档,而不是 jQuery 或 Fetch 的文档。
  • @Ferrybig — 他们没有使用 no-cors 模式……axios 根本无法识别 mode

标签: javascript get axios http-status-code-401


【解决方案1】:

网站返回401,这意味着您无权访问该网址。

通常,这意味着您需要发送凭据。

现在,您说的是 credentials: "include",,但这是 fetch 识别的属性,而不是 axiosaxios 的等效项是 withCredentials: true,因此请改为设置。


您还说过mode: "no-cors",,这是fetch 认可的另一个属性,而不是axios。反正你也不想要。发送凭据需要通过 CORS 获得权限,因此通过拒绝 CORS,拒绝获得发送凭据的权限。

删除该属性。

【讨论】:

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