1

I have a phonegap application. I query a REST web service which requires HTTP Basic Authentication

here is my code:

var username = 'test';
var pass = 'test';
var string = username+':'+pass;
var hash = btoa(string);
$.ajax({
    type: 'GET',
    dataType: 'jsonp',
    crossdomain: true,
    //tried this
    beforeSend: function (xhr){ 
        xhr.setRequestHeader('Authorization', make_base_auth(username, pass)); 
    },
    //also this
    username: 'test',
    password: 'test',
    //also this
    data: {username: 'test', password: 'test'},
    //and this
    url: 'http://'+username+':'+pass+'@testserver.local/ProductService/getProductList.json?testQuery&_jsonp=availableProducts',
    error: function (xhr, ajaxOptions, thrownError) {
        console.warn(xhr.status);
        console.warn(thrownError);
        alert(xhr.status);
        alert(thrownError);
    }
    });

function make_base_auth(user, password) {
  var tok = user + ':' + password;
  var hash = btoa(tok);
  alert("HASH: "+hash);
  return "Basic " + hash;
}

All I know from the server side is that apache says "User attempted to log in with no credentials" when I query from Android 4.x. So it seems no data is arriving at the server, but why?

Strangely, it works on iPhone (iOS 4.1)and Android versions 2.2 and 2.3, but not on 4.x (4.0.4 and also 4.1). Anybody has any hints, this is driving me crazy.

EDIT: I was able to look into the apache access log, it returns a 401 on every request with Android 4.x, a 200 for every request with pre 2.2 and 2.3. I will log all HTTP Request Headers now to check what´s arriving.

最白目
  • 3,505
  • 6
  • 59
  • 114
  • Bumped into same issue today(Droid 4.3), were you able to make any progress ? if so could you share it. – GoodSp33d Aug 29 '13 at 10:16
  • hopefully you'll find here an answer..at least worked for me: http://stackoverflow.com/questions/11810447/httpurlconnection-worked-fine-in-android-2-x-but-not-in-4-1-no-authentication-c – Mike Spike Dec 16 '13 at 10:38

0 Answers0