I am working on a Java application using JSF that requires JavaScript to connect to a website with Basic authentication. My goal is to replicate the experience of manually entering a username and password in the popup form.
Despite trying various methods found online, none have been successful so far. Interestingly, while the Ajax calls do return a response, I still encounter the Windows security popup. Is there a need to cache the credentials somehow?
For instance, neither of the code snippets below have produced the desired outcome. The first one uses base64 encoding:
$.ajax(
{
'password' : password,
'username' : username,
'url' : url,
'type' : 'GET',
'success' : function(){ alert("success"); },
'error' : function(err){ alert('Bad Login Details' + err);},
}
);
$.ajax({
url : url,
method : 'GET',
beforeSend : function(req) {
req.setRequestHeader('Authorization', auth);
},
error : function(xhr, ajaxOptions, thrownError) {
alert('Invalid username or password. Please try again. thrownError:' + thrownError + 'xhr:' + xhr + 'ajaxOptions:'+ajaxOptions);
},
success: function(result) {
alert('done');
}
});