jskidd3
jskidd3

Reputation: 4783

Auth headers not working ajax 401 Unauthorized

$.ajax({
  type: 'GET',
  url: 'myurl.com',
  headers: {
    "Authorization": "Basic " + btoa('username' + ":" + 'password')
  },
  dataType: 'JSON',
  success: function(data) {
    console.log(data);
  }
});

enter image description here

Any ideas why the server is responding with 401 Unauthorized? Username and password are correct and the request works fine outside of JavaScript.

Upvotes: 0

Views: 733

Answers (1)

Devstr
Devstr

Reputation: 4641

You see response to so called pre-flight OPTIONS request, not the GET request. Your server should handle it properly.

For more information please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

Upvotes: 1

Related Questions