Sasi Kumar M
Sasi Kumar M

Reputation: 2630

How to pass username and password for a webserivce using jquery ajax call?

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

Answers (2)

Rahul Tathod
Rahul Tathod

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

Hary
Hary

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

Related Questions