我是 Phonegap 和 jQuery 的新手,但研究过如何通过 jQuery ajax 提交表单。此代码在网页中有效,但在由 Phonegap 构建和安装时在 android 模拟器中失败。如您所见,我已经设置了$.mobile.allowCrossDomainPages = true;
. 当我在模拟器中测试时,屏幕闪烁了几次,表单被清除,好像页面重新加载一样,并且没有任何内容写入服务器。
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="css/jquery.mobile-1.2.0.css" />
<link rel="stylesheet" href="enter.css" />
<style>
/* App custom styles */
</style>
<script>
$( document ).bind( "mobileinit", function() {
// Make your jQuery Mobile framework configuration changes here!
$.mobile.allowCrossDomainPages = true;
});
function doform()
{
var datas = $("#enteractivity").serialize();
$.ajax({
url: "http://www.scottallencarter.com/llp/record.logactivity",
data: datas,
async: false,
cache: false,
dataType: 'text',
type: 'POST',
jsonp: 'jsoncallback',
timeout: 120000,
crossDomain: true,
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
},
success: function(msg){
alert('done success');
return true;
}
});
}
</script>
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>
<script src="enter.js"></script>
</head>
<body>
<!-- Home -->
<div data-role="page" data-theme="a" id="page1">
<div data-theme="c" data-role="header">
<h3>
Activity
</h3>
</div>
<div data-role="content">
<form id="enteractivity" name="enteractivity" onsubmit="doform()">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<label for="activityspecifics">
Activity
</label>
<input name="activityspecifics" id="activityspecifics" placeholder="Enter Activity" value="" type="text" />
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="category">
Category:
</label>
<select name="category" data-theme="c">
<option value="">(choose one)</option>
...
<option value="Test">Test</option>
</select>
</div>
<div data-role="fieldcontain">
<label for="activityInCategory">
Specific Category:
</label>
<select name="activityInCategory" data-theme="c">
<option value="AddSubCategory">+ Add New Specific Category</option>
<option value="">------------</option>
...
</select>
</div>
<input type="hidden" name="userid" value="1-1-1-1-1">
<input type="hidden" name="mobile" value="true">
<input type="submit" data-inline="true" data-theme="b" data-icon="arrow-r" data-iconpos="left" value="Submit" />
</form>
</div>
</div>
</body>
此外,当表单确实从网页提交时,它总是返回错误。不确定我需要从服务器端返回什么才能成功。