Handling a 401 error with XMLHttprequest (async)

6,982 views
Skip to first unread message

Matt

unread,
May 19, 2011, 4:59:24 AM5/19/11
to phonegap
I have a problem with using XMLHttprequest if the login and password
are incorrect and I'm using the async option. After I do a ".send", I
set my "waiting" animation going and it just hangs there. My theory
is that it's waiting for someone to enter the correct login and
password (although the popup never appears of course). Changing it to
sync at least gives me the 401 and I can add a handler to catch that,
but I want it to work as async.

Any ideas ?

Simeon Bateman

unread,
Mar 21, 2012, 2:25:36 PM3/21/12
to phon...@googlegroups.com
I am resurrecting this thread because I also having this issue.

I do not have this issue in the browser on the desktop, in safari on ios or in Adobe Shadow.  But when I run my code in phonegap I never get readystate change events for progress or completion if the server responds with 401.

Originally I was testing this with jquery an thought I was doing something wrong. But I switched to just straight xmlhttpRequest with a watcher for ready state change.

auth = Base64.encode(email + ":" + password)
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = () =>
alert(xmlhttp.readyState)
if xmlhttp.readyState == 4
alert(xmlhttp.status)

xmlhttp.open('GET', @url + "login/", true)
xmlhttp.setRequestHeader("Authorization", "Basic " + auth );
xmlhttp.send()

If the credentials are valid, then everything proceeds just fine (I get alerts for 1,2,4). However if the credentials are invalid, I get an alert for 1 and never again.  The ui is not locked but we never get completion events.  If I change the async flag on open to false, then I do get all the correct events, but the UI is locked.

I'd think this was a UIWebView bug, but this code runs fine in Safari on the phone as well as in Adobe Shadow, which is a native app that wraps UIWebView (i expect).  So the issue is only present when running this code inside phonegap.

Any suggestions? Is this a bug or am I making some wrong assumptions about how things should work?

sim

Matt

unread,
Apr 25, 2012, 12:41:35 AM4/25/12
to phon...@googlegroups.com
Here's how I got mine working:
 

var request = new XMLHttpRequest(); 

request.open("GET", "https://" + localStorage.login + ":" + localStorage.password + "@www.yourURL.com", true); 
request.setRequestHeader("Content-Type", "text/xml");
request.onreadystatechange = function() { 
                                                                   console.log("readystate: " + request.readyState);
                                                                    if(request.readyState == 4) { 
                                                                          console.log("status: " + request.status);
                                                                           if (request.status == 401) {
                                                                                 navigator.notification.alert("Your login/password is incorrect",null,"Authentication Error","OK");
                                                                                 return;
                                                                           }
                                                                           console.log(request.responseText);
                                                                    }

                                                          } 

console.log("Sending request...");
request.send();
I hope it helps.....

John Arnold

unread,
Apr 27, 2012, 9:57:29 AM4/27/12
to phon...@googlegroups.com
Hi.

I'm pretty new to PhoneGap and just got to the point where I'm seeing this problem, too. (Not getting any 401 response.) I'm using jQuery (Mobile) .ajax call and tried including the header you set. It is, I think, passing user name and password as 4th and 5th arguments to the open request but it doesn't work. Just wondering if you determined whether this works for you if you take the user:pass@ out of the URL string and move user/pass to 4th/5th arguments?

Any insight into which of the things helped you start seeing a 401 response would be very much appreciated.

FWIW: I'm mostly using iPad target from PhoneGap.

Thanks.

- John -

Mike Young

unread,
Aug 20, 2012, 5:21:11 PM8/20/12
to phon...@googlegroups.com
Here is a possible explanation and work-around that I have yet to try: 
https://groups.google.com/d/topic/phonegap/YKVGp8rRj9c/discussion
(if you control server, suppress the "WWW-authenticate" response header when you send back 401):

Reply all
Reply to author
Forward
0 new messages