Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS: Authentication using XMLHttpRequest - Handling 401 reponse
    text
    copied!<p>I'm writing an iOS application using PhoneGap (aka Cordova), I have a simple html login page that logs the user in using an XMLHttpRequest with basic authentication over SSL. Everything works splendidly when you enter your username and password correctly. However, if you enter the wrong username/password none of my callbacks are ever called.</p> <p>If you run the same code on Chrome for example, with the wrong username/password, chrome behaves in a similar manner, except it pops up an authentication challenge dialog. Hitting cancel on chrome's dialog returns control to my javascript code. Unfortunately, on iOS, the UIWebView wont even popup an auth dialog, it just hangs. I need a way to tell the user that they entered the wrong username or password so they can retry.</p> <p>The closest thing to an answer I could find was this <a href="http://www.freelock.com/2008/06/technical-note-http-auth-with-ajax" rel="noreferrer">http://www.freelock.com/2008/06/technical-note-http-auth-with-ajax</a> but changing the response status from the server doesn't seem like the right thing to do.</p> <p>Here's basically what my request code looks like, but when a bad username or password is sent it never reaches my onload callback (in fact the onreadystatechange callback only gets called once and thats for readyState 1, aka OPEN).</p> <pre><code>var req = new XMLHttpRequest(); req.onload = function(ev) { if (req.status == 401) { alert("Invalid Username/Password"); document.getElementById('password').focus(); } else if (req.status == 200) { window.location.href = some_secure_site; } else { // edit // alert("Some other status"); } } req.onerror = function (ev) { alert('Error'); }; req.ontimeout = function(ev) { alert('Timeout'); }; req.open('GET', uri, true, userValue, passValue); req.withCredentials = true; req.send(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload