Android Phonegap: $ .ajax does not launch callback for unauthorized access (401)

I am using PhoneGap to create an Android application. Using jQuery or Zepto, I can make website API calls using the $ .ajax call. However, when the website returns a 401 response (unauthorized), there seems to be no callback from the ajax call - none of the “successes”, “error”, or “complete” were called.

Note that the same code works fine when the answer is 200 or 500.

I am using Zepto 1.0rc1 and / or jQuery 1.7.2 with PhoneGap 1.6.1.

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

$('#button').on('touchstart', function() {
    console.log("UPLOAD --- ");
    $.ajax({
        url: 'https://mywebsite/api/v1.0/test/?ts=' + new Date().getTime(),
        type: 'GET',
        beforeSend: function (xhr){ 
            xhr.setRequestHeader('Authorization', make_base_auth('username', 'password')); 
        },
        success: function(data, status, xhr) {
            console.log("AJAX: SUCCESS: " + data);
            $('h1').text("AJAX!!");
        },
        error: function(xhr, errortype, error) {
            console.log("AJAX: FAIL: " + errortype + " - " + error);
            $('h1').text("AJAX FAIL");
        },
        complete: function() {
            console.log("--- Complete");
        }
    });
    return false;
});
+3
source share
2 answers

Lower the handset. Yes. You read it right.

I tried 1.4.1: did not work.

I tried 1.3.0: it works.

, .

-2

. HTTP 401

$.ajax({
    ...
    timeout: 5000, // Some timeout value that makes sense
    ...
});

{"readyState":0,"status":0,"statusText":"timeout"}. , HTTP 401.

$.ajax({
    ...
    async: false, // :-(
    ...
});

- {"readyState":4,"responseText":"<html>...</html>","status":401,"statusText":"Unauthorized"} .

+1

All Articles