安卓的PhoneGap:$就不会触发回调未经授权(401) [英] Android Phonegap: $.ajax does not fire callback for unauthorized (401)

查看:124
本文介绍了安卓的PhoneGap:$就不会触发回调未经授权(401)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的PhoneGap构建一个Android应用程序。无论是使用jQuery的或的Zepto,我能够拨打电话使用$就调用该网站的API。但是,当网站返回401(未授权)响应,但似乎没有从AJAX调用回调 - 没有成功,错误的,还是完全被调用

请注意,同样的code正常工作时的反应是200或500。

我使用的Zepto 1.0rc1和/或jQuery的1.7.2与PhoneGap的1.6.1。

 函数make_base_auth(用户名,密码){
    VAR TOK =用户+:+密码;
    VAR哈希= BTOA(TOK);
    返回基本+散;
}

$('#按钮')。在('touchstart',函数(){
    的console.log(UPLOAD ---);
    $阿贾克斯({
        网址:将https://mywebsite/api/v1.0/test/ TS ='+新的日期()的getTime()
        键入:GET,
        beforeSend:功能(XHR){
            xhr.setRequestHeader('授权',make_base_auth(用户名,密码));
        },
        成功:功能(数据,状态,XHR){
            的console.log(AJAX:成功:+数据);
            $(H1)文本(AJAX !!)。
        },
        错误:函数(XHR,ERRORTYPE,错误){
            的console.log(AJAX:失败:+ ERRORTYPE + - +误差);
            $(H1)文本(AJAX FAIL)。
        },
        完成:函数(){
            的console.log(---完成);
        }
    });
    返回false;
});
 

解决方案

降级PhoneGap的。是。你看的没错。

我试过1.4.1:没有工作

我试过1.3.0:作品

所以是的,就是这样。

I'm using PhoneGap to build an Android app. Using either jQuery or Zepto, I'm able to make calls to the website APIs using the $.ajax call. However, when the website returns a 401 (unauthorized) response, there seems to be no callback from the ajax call - none of "success", "error", or "complete" get called.

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

I'm 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;
});

解决方案

Downgrade phonegap. Yes. You read that right.

I tried 1.4.1: didn't work.

I tried 1.3.0: works.

So yeah, that's that.

这篇关于安卓的PhoneGap:$就不会触发回调未经授权(401)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆