我正在编写一个简单的Phonegap应用程序来收集一些数据并将其发送到sql。从浏览器工作正常但是当构建为phonegap应用程序时,数据不会被发送,尽管其他所有内容都有效,包括检查,警报和重定向到index.html。问题可能只是我需要添加deviceready函数,并且我已经尝试添加它但是当我将它包装在代码周围时它只是跳到window.location事件并且没有发布数据。
我试过了:
<script>
document.addEventListener("deviceready", checkSubmit, false);
function Checksubmit() {
...
}
以及
document.addEventListener("deviceready",
function checkSubmit() {
....
}, false);
但这些似乎没有正确执行。
任何帮助都非常感谢!
这是应用代码的缩减版。
<script>
function checkSubmit() {
var todayNum = Number(new Date());
var storedDate = localStorage.getItem("storedDate");
var diff = todayNum - storedDate;
var subDelay = 60000;
if (!storedDate || storedDate === null || diff > subDelay) {
localStorage.setItem("storedDate", todayNum);
$(function(){
$('form').submit(function(){
var postData = $(this).serialize();
$.ajax({
type: 'POST',
data: postData,
url: 'http://mydblocation.com/sqlin.php',
success: function(data){
console.log(data);
},
error: function(){
console.log(data);
alert('There was an error adding your submission, please try again later.');
}
});
alert("Thanks for your submission!");
window.location = "index.html";
return false;
});
});
}
if (todayNum - storedDate < subDelay) {
alert("You've already submitted! You will be able to submit again in 1 minute.");
window.location = "index.html";
}
}
</script>
<body onload="checkSubmit()">
<div class="container">
<form id="form">
<div class="col-md-12">
<div class="form-group" style="text-align: center;">
<p style=" font-family:verdana; text-align:justify">
Name
</p>
<div class="form-group">
<input type="text" name="name" class="form-control" />
</div>
<p style=" font-family:verdana; text-align:justify">
Location:
</p>
<div class="form-group">
<label for="location"></label>
<select class="form-control" name="location">
<option>Birmingham</option>
<option>Manchester</option>
<option>London</option>
<option>New York</option>
<option>Brussels</option>
</select>
</div>
<div class="form-group" style="text-align: center;">
<button class="btn btn-primary" type="submit">send</button>
</form>
</div>
</div>
</body>