Reputation: 4783
$.ajax({
type: 'GET',
url: 'myurl.com',
headers: {
"Authorization": "Basic " + btoa('username' + ":" + 'password')
},
dataType: 'JSON',
success: function(data) {
console.log(data);
}
});
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
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