Cordova Async XMLHttpRequest().open 在 Android 中不起作用

Cordova Async XMLHttpRequest().open does not work in Android

我正在尝试通过 GET 请求向我的应用程序服务器提交表单。但是我发现这是不可能的。我已经检查并确保安装了白名单插件,并在下面包含了我的 config.xml。我还设置了宽松的内容安全策略;

内容安全政策

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

config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.notifyme" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>notifyme</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="http://app-server-1.cloudapp.net/*" />
    <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:*" />
    </platform>
    <engine name="android" spec="~6.0.0" />
</widget>

HTML形式

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' gcm-android-1.cloudapp.net 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">
        <title>notifyme</title>
    </head>
    <body>
        <h1>notifyme</h1>
            Our clairvoyant service will notify you when things happen <br/>
            <p id="messages"></p>
            <p id="gcm_id"></p>
            Name of Feed:<br>
            <input type="text" name="name" id="name">
            <br>
            Url of Feed:<br>
            <input type="text" name="url" id="url">
            <br><br>
            <button id="submit-feed">Notify me</button>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

Javascript 提交表单

document.getElementById("submit-feed").addEventListener("click", function () {
    alert('Request Sent!');
    var feed_url = document.getElementById('url').value;
    var gcm_id = window.localStorage.getItem("gcm_id");
    var url = "http://app-server-1.cloudapp.net/schedule/"+encodeURIComponent(feed_url)+"/"+gcm_id;
    log( "<b>Url:</b> "+url);
    log("Before request block");
    var request = new XMLHttpRequest();
    log("Before opening request");
    alert(request.open("GET", url, true));
    log("After opening request]");
    request.onreadystatechange = function() { 
        if (request.readyState == 4) {
            if (request.status == 200 || request.status == 0) {
                alert(request.responseText);
            } else {
                alert("didn't work")
            }
        }
    log('Reached onreadystatechange');
    };
    log("After onreadystatechange block");
    log('Before GET request');
    request.send();
    log('After GET request');

    function log(message) {
        document.getElementById("messages").innerHTML = message;
    }
});

提交表单时,代码只运行到 Before opening request

警报正在冻结脚本。 替换:警报(request.open("GET",url,真)); 其中:request.open("GET", url, true)

卸载并重新安装白名单插件为我解决了这个问题。我安装了错误版本的白名单插件。我已将 cordova 升级到 6.6,但我仍然有版本 1 的白名单插件。卸载并使用 --save 选项重新安装后。我的版本是 ~1.3.1。在我重建应用程序后,一切都运行良好。