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.

3
  • 2
    Downvoters unite :) or at least 'splain yourselves. Is it "what havr you tried" ? Or "show your effort/code"? I will gladly listen.
    – suff trek
    Commented Jun 14, 2014 at 4: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
    – suff trek
    Commented Jun 14, 2014 at 4:13
  • @JakeGould this code runs not inside of WebView I don't think it applies either.
    – suff trek
    Commented Jun 14, 2014 at 22:44

2 Answers 2

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.

2
  • 4
    Why not try btoa? is javascript pure.. ¬.¬ a try req.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass));
    – KingRider
    Commented Jan 19, 2016 at 18:24
  • btoa is ASCII only Commented Aug 29, 2017 at 13:15
0

If the request is authenticated via cookie/headers, we can set headers and make that request withCredentials value as false.

withCredentials attribute must to false in IOS otherwise, you may be getting 401 responses from the server.

Note:- In Android and web platforms works without 'withCredentials' property also

 req.open(method, fullurl, true);
 req.setRequestHeader("Cookie", "CookieInfo")); 
 req.send(data);    
 request.withCredentials = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.