How to pass username and password to web browser using jQuery ajax?

I have a web service; I need to use credentials to access it. I want to call a web service using jQuery AJAX call in another project. So how can I pass the username and password for this web service using this method?

+3


source to share


2 answers


Here is the doc for $. ajax . It also has many additional parameters. Follow the docs when you need something extra.



$.ajax({
     type: 'POST',
     url:   'https://webservice url',
     data: ({ username: value, password: value; }) //use parameters as such defined in webservice
     success: function(data){

       }
})

      

+2


source


$.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;
}

      



+1


source







All Articles