2016-12-31 95 views
0

我试图通过GET请求提交表单到我的应用服务器。但是我发现不可能做到。我已经检查并确保安装了白名单插件,并在下面包含了我的config.xml文件。我还设置了宽容的内容安全策略;科尔多瓦异步XMLHttpRequest()。开放在Android中不起作用

内容安全策略

<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="[email protected]" 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

回答

0

卸载并重新安装白名单插件为我解决了这个问题。我安装了白名单插件的错误版本。我将cordova升级到了6.6,但是我仍然拥有版本1中的白名单插件。在卸载并使用--save选项重新安装之后。我有版本〜1.3.1。并且在我重新构建应用程序后,一切都很完美。

0

警报正冻结脚本。 replace:alert(request.open(“GET”,url,true)); with:request.open(“GET”,url,true)

+0

我已经尝试过没有警报,但我仍然无法通过调用request.open(“GET”,url,true),此后没有任何执行 – Jonathan

+0

将日志更改为console.log – Harminder