cordova 访问远程服务器,ondeviceready不触发
用的是android5.1.1的手机
在config.xml中将src设置成http://myserver/index.html,
同时也把platform_www目录下的文件发到了服务器上
源码附上:
index.html文件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<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>Test Map</title>
<!--- <link rel="stylesheet" href="http://202.193.52.242/arcgis_js_v40_api/library/4.0/4.0/dijit/themes/claro/claro.css" />
<link rel="stylesheet" href="http://202.193.52.242/arcgis_js_v40_api/library/4.0/4.0/esri/css/main.css" /> --->
<link rel="stylesheet" type="text/css" href="jquery.mobile-1.4.5.min.css" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<style>
html, body, #ui-map-view {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>
<style>
/* Hide the default PhoneGap div */
.app {
//display : none;
}
/* Set html, body height */
/* Set the map view div height */
html,body,#viewDiv {
padding: 0;
margin: 0;
height: 400px;
width: 300px;
}
</style>
</head>
<body>
<section id="page1" data-role="page" name="page1">
<div data-role="header" data-position="fixed" data-theme="b">
<h1> 交通设施管理</h1>
</div>
<div data-role="content" class="content">
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready!</p>
</div>
</div>
<div id="viewDiv" >hi</div>
</div>
<div data-role="footer" data-position="fixed" data-theme="b">
<p>Image of GUET</p>
</div>
</section>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
js/index.js文件
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
alert("listen!");
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
alert("Device model:"+device.cordova+" is ready");
// $("div.content").html("<ul data-role='listview'><li>devicemodel:"+device.platform+"</li></ul>");
// document.querySelector("#startCamera").addEventListener("touchend", this.startCamera, false);
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
//console.log('Received Event: ' + id);
alert("received!");
}
};
app.initialize();
问题是:只能弹出“listen!",ondeviceready不执行。
但是如果config.xml中src=index.html,即放在本地,ondeviceready不执行就能。
另外,还有奇怪的事,android 4.0.1的手机本地和远程服务器都能成功。
但我的cordova-android是5.1.1的
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
cordova 需要有js bridge配合的 不光是一个js文件
回复
能说得更具体一些吗?谢谢
@leisureguy 搜索下jsbridge吧
我也遇到了相同的问题,文件都放了 怎么还是不行呢,请问大神么 还有什么需要注意的么?提示信息就是 deviceready has not fired after 5 seconds. Channel not fired: onCordovaInfoReady,而用ripple emulate 就能正常工作 在真机上 就什么都没显示出来
回复
请问后来您解决了吗?解决了的话可以分享一下吗?
cordova是app端使用的,编译后会自带cordova.js 。你放在服务器端了么?