Reputation: 2630
I have a web service; in order to access it I need to use credentials. I want to invoke the web service using a jQuery AJAX call in another project. So how to pass username and password for that web service using that method?
Upvotes: 4
Views: 5867
Reputation: 348
$.ajax({
type: 'GET',
url: 'url',
dataType: 'json',
//whatever you need
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth(user, password));
},
success: function () {});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
Upvotes: 1
Reputation: 5818
Here is the doc for $.ajax. Also this has many extra parameters. Follow the doc when you need anything extra.
$.ajax({
type: 'POST',
url: 'https://webservice url',
data: ({ username: value, password: value; }) //use parameters as such defined in webservice
success: function(data){
}
})
Upvotes: 2