使用/axios执行GET请求时出现401错误

401 error when performing GET request w/ axios

我可以在浏览器中查看我的 JSON 数据,但不能在控制台中查看。我对 axios 不是很熟悉,但我认为问题出在 url (见下面的片段)。那,或者我在 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,这意味着您无权访问 URL。

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

现在,您说的是 credentials: "include",,但那是 fetch 识别的 属性,而不是 axiosaxios 等价于 withCredentials: true,因此改为设置它。


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

删除 属性.