6

Any idea why XMLHttpRequest with correct credentials in Pebble JS Framework fails basic authentication on Android but works in iOS?

Exactly the same code, along the lines of:

var req = new XMLHttpRequest();
req.open(method, url, true, user, pass);
req.send(data);
req.onreadystatechange = function() { ... }

Returns 401 in from Android Pebble app, but authenticates correctly in iOS.

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Have you looked at this? http://stackoverflow.com/questions/11025213/ios-authentication-using-xmlhttprequest-handling-401-reponse – Giacomo1968 Jun 14 '14 at 04:07
  • 2
    Downvoters unite :) or at least 'splain yourselves. Is it "what havr you tried" ? Or "show your effort/code"? I will gladly listen. – Yuriy Galanter Jun 14 '14 at 04:09
  • @JakeGould unfortunately it doesn't apply to me. JS part of Pebble app work correctly on iOS already. And always fails in Android Pebble JS framework - even with correct credentials – Yuriy Galanter Jun 14 '14 at 04:13
  • What about this? http://stackoverflow.com/questions/11318703/access-control-allow-origin-error-at-android-4-1 – Giacomo1968 Jun 14 '14 at 04:17
  • @JakeGould this code runs not inside of WebView I don't think it applies either. – Yuriy Galanter Jun 14 '14 at 22:44

1 Answers1

17

I found a workaround that worked for me on Android.

Don’t know why but direct authenticated request:

    req.open(method, fullurl, true, user, pass);
    req.send(data);

didn’t work for me – it always returned 401. So Instead I tried to set basic authentication via header:

    req.open(method, fullurl, true);
    req.setRequestHeader("Authorization", "Basic " + Base64.encode(user + ":" + pass)); 
    req.send(data);

(where Base64 is taken from here: https://stackoverflow.com/a/246813/961695) – and it worked! Perhaps there’s a bug in XmlHttpRequest implementation on android.

Community
  • 1
  • 1
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136