为什么我不能使用 Cordova 发送 XHR 请求?
Why can't I send an XHR request with Cordova?
我是 Apache Cordova 的新手,我听说这是开发 android 应用程序的最简单方法。所以我做的一切都很好(或者至少我相信如此),但我无法在我的应用程序上获得 XHR 请求。手机和电视显示相同的内容,而像 bluestacks 这样的模拟器显示所需的结果。
我看到的(大屏幕 - android 电视,小屏幕 - 我的手机):
我希望看到我的服务器 returns.
这是我的代码(HTML:<div id="i" style="font-size:20px;line-height:auto;">Please wait while we initialize the app...</div>
;index.js 在科尔多瓦上):
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
// Cordova is now initialized. Have fun!
var __domain = 'http://192.168.1.100/android-app/';
var xhr = new XMLHttpRequest();
xhr.open('get', __domain + '.cordova.request.allow.php', 1);
xhr.addEventListener('readystatechange', function() {
document.getElementById('i').innerHTML += '<br>Preparing requested files...';
document.getElementById('i').innerHTML += '<br>ReadyState on ' + this.readyState + ' and Status returned ' + this.status;
if (this.readyState === 4 && this.status === 200 && this.response == 1) {
// It's okay
// Append init
var s = document.createElement('script');
s.src = __domain + 'init.js';
// Force load this script
document.getElementsByTagName('head')[0].appendChild(s);
}
}, 0);
xhr.send();
}
PHP 文件应该 return “1”(作为字符串 - 文件内容:<?php echo 1; return;
),这是 init.js:
document.body.style.background = 'blue';
document.body.style.color = 'white';
alert('success');
我做错了什么?如何使用 XHR 从服务器动态获取数据?
手机和电视都使用 WiFi 连接到同一网络
(使用小米盒子电视和小米红米手机)
更新-@CedricCholley:
<meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src * 'unsafe-inline'; media-src *; img-src * data: content:;">
在@CedricCholley 的帮助下解决了。我将应用程序连接到 chrome 控制台(这里有指南:https://ourcodeworld.com/articles/read/48/how-to-debug-a-cordova-app-on-your-device-with-google-chrome) and got the error Failed to load resource: net::ERR_CACHE_MISS
. after googling i've found out that this is happening because the app has no permission to access the internet, so i've followed this guide also: https://cordova.apache.org/docs/en/2.7.0/cordova/connection/connection.html
添加了以下内容:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
到platforms\android\app\src\main\AndroidManifest.xml
我是 Apache Cordova 的新手,我听说这是开发 android 应用程序的最简单方法。所以我做的一切都很好(或者至少我相信如此),但我无法在我的应用程序上获得 XHR 请求。手机和电视显示相同的内容,而像 bluestacks 这样的模拟器显示所需的结果。
我看到的(大屏幕 - android 电视,小屏幕 - 我的手机):
我希望看到我的服务器 returns.
这是我的代码(HTML:<div id="i" style="font-size:20px;line-height:auto;">Please wait while we initialize the app...</div>
;index.js 在科尔多瓦上):
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
// Cordova is now initialized. Have fun!
var __domain = 'http://192.168.1.100/android-app/';
var xhr = new XMLHttpRequest();
xhr.open('get', __domain + '.cordova.request.allow.php', 1);
xhr.addEventListener('readystatechange', function() {
document.getElementById('i').innerHTML += '<br>Preparing requested files...';
document.getElementById('i').innerHTML += '<br>ReadyState on ' + this.readyState + ' and Status returned ' + this.status;
if (this.readyState === 4 && this.status === 200 && this.response == 1) {
// It's okay
// Append init
var s = document.createElement('script');
s.src = __domain + 'init.js';
// Force load this script
document.getElementsByTagName('head')[0].appendChild(s);
}
}, 0);
xhr.send();
}
PHP 文件应该 return “1”(作为字符串 - 文件内容:<?php echo 1; return;
),这是 init.js:
document.body.style.background = 'blue';
document.body.style.color = 'white';
alert('success');
我做错了什么?如何使用 XHR 从服务器动态获取数据?
手机和电视都使用 WiFi 连接到同一网络
(使用小米盒子电视和小米红米手机)
更新-@CedricCholley:
<meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src * 'unsafe-inline'; media-src *; img-src * data: content:;">
在@CedricCholley 的帮助下解决了。我将应用程序连接到 chrome 控制台(这里有指南:https://ourcodeworld.com/articles/read/48/how-to-debug-a-cordova-app-on-your-device-with-google-chrome) and got the error Failed to load resource: net::ERR_CACHE_MISS
. after googling i've found out that this is happening because the app has no permission to access the internet, so i've followed this guide also: https://cordova.apache.org/docs/en/2.7.0/cordova/connection/connection.html
添加了以下内容:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
到platforms\android\app\src\main\AndroidManifest.xml