Android Phonegap 3 webapp白名单不起作用

Posted

技术标签:

【中文标题】Android Phonegap 3 webapp白名单不起作用【英文标题】:Android Phonegap 3 webapp whitelist not working 【发布时间】:2013-12-31 18:02:11 【问题描述】:

我有一个 phonegap android 应用程序正在尝试从外部服务器获取数据:

服务器是一个 .net WebAPI - 并通过 JSONP 使用通信。相同的代码/服务器在 ios 中使用 phonegap 2.7。对于 Android,我使用的是 phonegap 3.1。我已经仔细检查了 URL 以确保它是正确的。我的 config.xml 文件中也有以下行。

<access origin="*" subdomains="true" />

请求详情在这里:

Request URL:http://XXXXXXXXXXXX.com/server/api/messagetypeopt/1?callback=jQuery17209047717128414661_1386902222072&_=1386902252817
Request Method:GET
Status Code:404 Not Found (from cache)
Request Headersview source
Accept:*/*
User-Agent:Mozilla/5.0 (Linux; Android 4.4; sdk Build/KRT16L) AppleWebKit/537.36 (Khtml, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jQuery17209047717128414661_1386902222072
_:1386902252817

并且javascript请求代码是

$.support.cors = true;
        $.mobile.allowCrossDomainPages = true;
        $.mobile.loading('show');

        // enableFrontPageButtons(1)
        enableButtons(0);
        console.log('about to get message types');
        try 
        $.ajax(
            //url: 'http://localhost:8956/api/messagetypeopt/' + SENDER_ID ,
            url: SERVER + '/api/messagetypeopt/' + SENDER_ID ,
            type: 'GET',
            dataType: 'jsonp',
            jsonp: 'callback',
            crossDomain: true,
            async:   false,
            success: function (data) 
                //alert('1');
                localStorage.setItem("rawMessageTypes", JSON.stringify(data.branches));
                Sender.updateBranchesData(data);
                Sender.updateSenderData(data);
                //alert('2');
                setupBranches(data.branches);
                setMessageTypesDB(data.branches); // store in local database.
                updateMessageTypes(data.branches);
                WriteMessageList( getMessageDataForBranch(JSON.parse(localStorage.getItem("rawMessageTypes"))));
                //alert('3');
                // save sender data and branch data
                enableButtons(1);
                //$('#libName').val(ConfigData.SENDER_NAME);
                changeTheBranch(CURRENT_BRANCH);
               // alert('4');
                //alert('Update Complete');
                $('messagetypepopuptext').text('Update Complete');
                $('messagetypepopup').popup();

            ,
            statusCode: 
                500: function() 
                    alert("An incorrect request went to the server, check your UserID? ");
                    enableButtons(1);
                    $.mobile.changePage('#pageSetup', changeHash: false);
                 ,
                400: function() 
                    alert("An incorrect request went to the server, check your UserID? ");
                    enableButtons(1);
                    $.mobile.changePage('#pageSetup', changeHash: false);
                
            ,
            error: function (x, y, z) 
                //alert('There was an error getting data from the server');
                enableButtons(0);
                $.mobile.changePage('#pageSetup', changeHash: false);
            ,
            complete: function (a, b) 
                //alert('complete:' + a + '\n' + b);
               $.mobile.loading('hide');
            
        );

我真的被困在这个问题上 - 所以我真的很想得到一些帮助。谢谢:)

【问题讨论】:

【参考方案1】:

我和你在同一页。它可以在 iOS7 甚至 Android 2.3 虚拟机上完美运行,但在 Android 4.0.3 上无法正常运行我使用的是 Phonegap 3.1

--- 编辑 ---

我有各种要列入白名单的网站,我把它们都放在不同的地方

<access origin="*://site.com" subdomains="true"/>
<access origin="*://site.com/*" subdomains="true"/>
<access origin="*://site.com/something/*" subdomains="true"/>
<access origin="https://site.com/" subdomains="true"/>

由于我使用的是 AngularJS,我不知道如何在您的代码中为您提供帮助,但是一旦我使用 jquery 进行测试并且我没有您的支持 CORS 代码。我还添加了

<access origin="..." subdomains="true"/>

在位于 res/xml/config.xml 的 config.xml 上。

【讨论】:

以上是关于Android Phonegap 3 webapp白名单不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在PhoneGap中包装移动网站(webapp)的基本过程是啥[关闭]

如何为 webapp 构建 phonegap 应用程序?

如何从 PhoneGap/webapp 将文件 ftp 到服务器?

iOS - Phonegap 或 WebApp 键盘问题

使用 Android 配置 Phonegap 3.0

Android 上的 PhoneGap 3.3 和移动服务 HTML 应用程序