-3

I can view my JSON data in my browser but not in my console. I'm not super familiar with axios, but I think the problem is with the url (see snippet below). That, or I'm missing something within axios.get {.

Any thoughts?

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);
})

console:

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)
Bodrov
  • 840
  • 1
  • 15
  • 29
  • 2
    401 http code means that you are not authorized to request that resource, you probably should send the user credentials diferently? don't know what that site is nor what does it require from you – Krzysztof Krzeszewski Feb 13 '19 at 17:03
  • 1
    `crossDomain: true` is not a property recognised by Axios. (It's a jQuery thing that is almost never useful to modify). Don't use it here. – Quentin Feb 13 '19 at 17:07
  • You cannot send headers when you use mode no cors – Ferrybig Feb 13 '19 at 17:10
  • 1
    Given my previous comment and answer, I think the general take-away is that you need to read the documentation for Axios and not the documentation for jQuery or Fetch. – Quentin Feb 13 '19 at 17:10
  • @Ferrybig — They aren't using mode no-cors … axios doesn't recognise `mode` at all. – Quentin Feb 13 '19 at 17:11

1 Answers1

1

The site is returning a 401 which means you are not authorised to access the URL.

Normally, this means you need to send credentials.

Now, you've said credentials: "include", but that's a property recognised by fetch, not axios. The axios equivalent is withCredentials: true, so set that instead.


You've also said mode: "no-cors", which is another property recognised by fetch, not axios. You don't want it anyway. Sending credentials requires permission via CORS, so by rejecting CORS, reject the possibility to get permission to send the credentials.

Remove that property.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335