Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax authentication with jQuery

Tags:

jquery

ajax

I'm able to recieve the requested xml with

curl -X GET -u username:password url

but not with

$.get('url',
        {username:"username",password:"password"},
        function (data) {


        });

No javascript errors, no cross domain policy issues. It might be a syntax error but I failed to find a decent tutorial yet. Any suggestions please?

like image 542
Fotis Adamakis Avatar asked Apr 19 '12 10:04

Fotis Adamakis


1 Answers

I think you'd need the plain format :

$.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;
}
like image 119
Royi Namir Avatar answered Nov 04 '22 22:11

Royi Namir