我試圖通過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
我已經嘗試過沒有警報,但我仍然無法通過調用request.open(「GET」,url,true),此後沒有任何執行 – Jonathan
將日誌更改爲console.log – Harminder