繁体   English   中英

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

警报冻结了脚本。 替换:alert(request.open(“ GET”,url,true)); 使用:request.open(“ GET”,url,true)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM