如何将phonegap发送参数ajax到php文件

时间:2013-11-19 15:48:11

标签: javascript php android ajax cordova

我的项目有问题。我将它分为两​​部分:在主机服务器上通过php和On mobile,我希望将所有内容都包含在一个项目中。我该怎么做?

我想通过ajax将数据发送到php并执行以下操作:

我有这个javascript:

    <script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
    $("#button").click(function(evt){
    var txtUsername = $("#txtUsername").val();
    var txtPassword1 = $("#txtPassword1").val();

    var senddata ={'txtUsername' : txtUsername,'txtPassword1':txtPassword1};
    $.ajax({
        type: 'POST',
        data: senddata,
        url: 'http://(Domain_name)/a_project/file/check_login.php',
        cache: false,
        success: function(data) {

            alert('Your comment was successfully added');
        },
        error: function(){

            alert('There was an error adding your comment');

        }
    });

    return false;
});
}
</script>

HTML code:

   <form method="post" data-ajax="false">

 <label for="user">ชื่อผู้ใช้ : </label>
 <input type="text" data-clear-btn="true" name="txtUsername" id="txtUsername"   data-theme="a">
 <label for="pass">รหัสผ่าน :</label>
 <input type="password" data-clear-btn="true" name="txtPassword1" id="txtPassword1"  data-theme="a">
   <table align="center" width="100%">
  <tr >
  <td width="50%"><input type="button" id="button" value="เข้าสู่ระบบ" data-theme="g" ></td>
  <td width="50%"><a href="#popupForgot" data-rel="popup" data-position-to="window" data-role="button"  data-theme="g">ลืมรหัสผ่าน</a></td> </tr>
  </table>

  <a href="#popupRegis" data-rel="popup" data-position-to="window" data-role="button" data-theme="g">สมัครสมาชิก</a>

php代码:

<?
$txtUsername =$_POST['txtUsername'];
$txtPassword1 =$_POST['txtPassword1'];
include("connect.php");
$strSQL = "SELECT * FROM login WHERE username = '".$txtUsername."' 
and password = '".$txtPassword1."'";

 .
 .
 .
 .
 .

 ?>

好吧,看起来数据无法发送到php。我该怎么办?

谢谢你

1 个答案:

答案 0 :(得分:0)

如果您的Access-Control-Allow-Headers请求来自其他主机,则由于服务器引发POST,应用程序正在通过手机运行时,此功能无效。
你需要使用JSONP AJAX Call这基本上是一个GET请求

查看JSON crossdomain communication with PHP file and a local javascript filehttp://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/

不要忘记添加“callback =?”并将$ _POST变量更改为$ _GET