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)

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
Chayma Atallah
  • 725
  • 2
  • 13
  • 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
  • 63,293
  • 14
  • 100
  • 115