1

I am trying to make an Ajax call as follows:

$.ajax({ 
        type: "GET",
        dataType: "json",
        contentType: "application/json",
        username: "user",
        password: "admin",
        url: "http://localhost:8080/projects/1",
        xhrFields: {
            withCredentials: true
       },
        success: function(data){     
            console.log(data);
        }
    });

But I get the following error:

Failed to load resource: the server responded with a status of 401 ()

Am I missing something? (Authentication on the endpoint is HTTPBasicAuthorization)

MrGeek
  • 21,097
  • 4
  • 28
  • 52
Chayma Atallah
  • 685
  • 2
  • 10
  • 30

1 Answers1

0

Seems that you need to do it like this:

Full code:

headers: {
    "Authorization": "Basic " + btoa(username + ":" + password)
  },

Full code:

$.ajax({ 
        type: "GET",
        dataType: "json",
        contentType: "application/json",
        url: "http://localhost:8080/projects/1",
        headers: {
            "Authorization": "Basic " + btoa(username+ ":" + password)
        },
        success: function(data){     
            console.log(data);
        }
    });

If that doesn't work, then this should work:

 beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
Icarus
  • 61,819
  • 14
  • 96
  • 113