PhoneGap登录错误

时间:2016-12-08 17:42:57

标签: javascript php jquery cordova phonegap-build

我已经制作了一个phonegap应用程序,当记录日志记录是否成功甚至没有succsess即时获取在else部分处理的相同错误时我很困惑。 这是我的Javascript Ajax登录代码。

$(document).ready(function() {
  $("#login").click(function() {
    var email = $("#email").val();
    var password = $("#password").val();
    var dataString = "email=" + email + "&password=" + password + "&login=";
    if ($.trim(email).length > 0 & $.trim(password).length > 0) {
      $.ajax({
        type: "POST",
        url: "http://192.168.43.173/viamobile/login.php",
        data: dataString,
        crossDomain: true,
        cache: false,
        beforeSend: function() {
          $("#login").html('Connecting...');
        },
        success: function(data) {
          if (data == "success") {
            localStorage.login = "true";
            localStorage.email = email;
            window.location.href = "index.html";
          } else if (data = "failed") {
            alert("Login error");
            $("#login").html('Login');
          }
        }
      });
    }
    return false;
  });
});

和我的PHP脚本

<?php
include "db.php";
if(isset($_POST['login']))
{
  $email = $_POST['email'];
  $password = $_POST['password'];

  $login = mysqli_num_rows(mysqli_query($con,"select * from `admin` where `username`='$email' and `password`='$password'"));
  if($login!=0)
  {
    echo "success";
  }
  else
  {
    echo "failed";
  }
}
?>


<?php
 header("Access-Control-Allow-Origin: *");
 $con= mysqli_connect("localhost","root","","phonegap");
?>

我实际上需要根据用户访问权限验证用户并重定向到不同的页面。但由于这个错误,我无法这样做。有人可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:1)

我可以建议你像我的工作代码。客户端JS代码:

$.ajax({
  type: /* GET or POST */,
  url: /* your url */,
  dataType: 'json',
  contentType: "application/json",
  beforeSend: function (event) {
    /* before funcs */
  }
})
.done(function (data) {
  if (data.status == "ok" || !data.error) {
    /* good */
  } else {
    /* manage error */
  }
})
.fail(function (data)  {
  /* manage error */
});