I am looking to create an ajax call with authorization headers only when the user enters a username and password. If these variables are empty, I need to create an ajax call without authorization headers. Is it possible to achieve this using just one ajax call, or do I need to create two separate ajax calls for each scenario?
var username = "user123";
var password = "pass123";
//var username = "";
//var password = "";
$.ajax({
type: "GET",
url: url_survey,
dataType: "json",
headers: {
'Authorization': "Basic " + btoa(username + ":" + password)
},
success:
function (data) {
alert("SUCCESS");
},
error:
function (data) {
alert("ERROR");
}
});