php - phonegap 构建 + ajax 调用不适用于 android

标签 php android jquery ajax cordova

我正在使用 phonegap build 构建一个 phonegap 应用程序。我的 ajax 调用在我的浏览器和 Ripple 模拟器上运行良好,但是一旦我从 phonegap build 获取 apk 并将其安装到 Samsung Galaxy 4 上,我就会收到错误消息。

这是我的 ajax 调用:

 var usuario = $('#usuario').val();
    var password = $('#password').val();

    $.ajax({
        url: 'http://www.example.com/page.php',
        jsonp: "callback",
        dataType: "jsonp",
        data: {nombre:usuario, password:password},
        success: function (data) {
            console.log(data);
            var respuesta = data.resp;

            if (respuesta == 0) {
                alert('Hubo un error, inténtalo de nuevo');
            };
            if (respuesta == 1) {
                var id = data.id;
                window.localStorage.setItem("usuario", id);
                window.location.replace('bici.html');
            };
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText+status+error);

        }
    });

我的 php header

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST,GET,OPTIONS");
header("Access-Control-Allow-Headers: content-type");
header("Access-Control-Allow-Headers: NCZ");
header('Content-type: application/json;charset=utf-8');
date_default_timezone_set('America/Mexico_City');

和我的 config.xml

-->

<access origin="*"/>
<access origin="http://www.example.com" />
<!-- Added the following intents to support the removal of whitelist code from    base cordova to a plugin -->
<!-- Whitelist configuration. Refer to       https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
<plugin name="cordova-plugin-whitelist" version="1" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />

最佳答案

我成功了!!正是沿着这条线,我只需要也添加插件。

config.xml 中的插件行是 <gap:plugin name="cordova-plugin-whitelist" source="npm"/>

还有我的 html 的元标记 <meta http-equiv="Content-Security-Policy" content="default-src data: gap: https://ssl.gstatic.com 'unsafe-eval' *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http://www.example.com; connect-src 'self' http://www.example.com">

关于php - phonegap 构建 + ajax 调用不适用于 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32955776/

相关文章:

android - 使用 OpenGL(和 OpenGL ES)渲染 SVG

java - 可扩展列表 : different background colors for ListGroup Items

javascript - 表 th(相对)内的 CSS div(绝对),div 被隐藏

javascript - 如何通过 id 使用 JQuery 动态选择元素

php - 我一直收到错误 "syntax error, unexpected ' catch' (T_CATCH) "

php - 存储用户搜索电子邮件警报的最佳策略是什么?

java - 如何解决这个 gradle 同步问题

jquery:使用上下键迭代列表项 ->光标位置?

php - array_search 找不到字符串?

javascript - 使用ajax重新执行php循环