首页 > 解决方案 > Phonegap - 调用外部 PHP 的问题

问题描述

我正在创建一个需要 ping 托管在随机服务器上的外部 PHP 脚本的 Phonegap 应用程序(cli-8.0.0、ios-4.5.4、android-7.0.0)。

当我使用 Phonegap Developer App 进行测试时 - 成功。

当我从 Phonegap Build 安装 APK 后进行测试时 - 错误

我已经安装了cordova-plugin-whitelist,我正在等待'DEVICE READY...'。

这是我的config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="xx.xx.xx.xx" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>xxxxx</name>
    <description>xxxxx</description>
    <author email="xxxx" href="xxxx">xxxx</author>
    <preference name="phonegap-version" value="cli-8.0.0" />
    <preference name="orientation" value="portrait" />
    <preference name="SplashScreen" value="none" />
    <content src="index.html" />
    <access origin="*" />
    <allow-navigation href="http://*/*" />
    <allow-navigation href="https://*/*" />
    <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:*" />
        <resource-file src="google-services.json" target="app/google-services.json" />\
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <resource-file src="GoogleService-Info.plist" />
    </platform>
    <plugin name="phonegap-plugin-push" spec="~2.1.2">
        <variable name="FCM_VERSION" value="11.0.1" />
    </plugin>
    <engine name="android" spec="~7.0.0" />
    <engine name="ios" spec="~4.5.4" />
    <engine name="browser" spec="~5.0.4" />
    <plugin name="cordova-plugin-file" spec="^6.0.1" />
    <plugin name="cordova-plugin-network-information" spec="^2.0.1" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
</widget>

这是我的index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title></title>
    </head>
    <body>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

这是我的index.js

var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
        console.log('DEVICE READY...');

        var url = "https://www.xxx.xxx.com/xxx.php?id=" + encodeURIComponent('xxx') + "&title=" + encodeURIComponent('xxx');

        var xhr = new XMLHttpRequest();
        xhr.onload = function() {console.log('success');}
        xhr.onerror = function() {console.log('error');}
        xhr.open('POST', url, true);
        xhr.send();
    }
};

app.initialize();

显然,所有的 XXX 都是任意的。

有任何想法吗?

标签: androidcordovaphonegapwhitelist

解决方案


好的,问题是我在第 4 行的index.html中的内容安全策略。

我忽略了允许任何其他域与应用程序通信。default-src需要包含允许的域,否则将不允许任何域。

现在我使用了 * 通配符,它​​可以工作。

<meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">

推荐阅读