这是我的问题: 我在index.html中有一个表单,它将有一个文本输入,该值将是 $.post 到一个应该处理它并返回一个值的php页面。我的代码:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>
<script>
function get(){
$.post('http://xxx/xxx/get.php', {name: form.name.value},
function(output){
$('#result').html(output).show();
});
}
</script>
</head>
<body>
<div data-role="page" id="header">
<div data-role="header">
<h1>Header</h1>
</div>
<div data-role="content">
<form name="form" id="form">
<input type="text" name="name"><input type="button" value="Get Output" onClick="get();">
</form>
<p> result here: </p>
<div id="result"></div>
</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
</body>
</body>
</html>
和 get.php
<?php
$name = $_POST['name'];
echo $name;
?>
这是出于我的测试目的的简单编码。从代码中我们可以看到,我们在文本字段中输入的任何文本都将在 get.php 中进行处理。因为我把 echo $name 放在那里,所以名称应该显示在 index.html 中的 div id = result 中。我认为我的代码是正确的,但它不起作用。所以我需要这里专家的帮助...我做错了什么?
最佳答案
几点:
1. 将远程服务器添加到白名单...如果您使用的是黑莓,则必须编辑config.xml文件,如果是ios应用程序,则必须更改项目的plist文件。
编辑 2011-11-03: 在 Android 中: 确保此行位于phonegap应用程序 list 文件中: < 使用权限 android:name="android.permission.INTERNET">
2. 尝试将此 header 添加到 php 文件 header ('Access-Control-Allow-Origin: *');
编辑 2011-11-03:此 header 必须包含在 PHP 文件的开头。
- $('#result').html(output).show(); show 方法不是必需的,只有当字段具有隐藏属性时才必须使用该方法。
祝你好运!
关于forms - PhoneGap表单提交到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7962527/