繁体   English   中英

AJAX请求无法在cordova中工作?

[英]AJAX requests not working in cordova?

从我收集到的数据来看,与数据库的每一个通信都应该在cordova应用程序中经过ajax请求。 所以我建立了一个本地主机,建立了一个数据库,在那儿的某个地方创建了我的php文件,并开始设置cordova。 我首先在浏览器中开发了它,并设法使其得以运行,但是现在每个ajax请求都没有运行。 例如,这是我的login.html文件:

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
  <script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script>
  <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
  <link rel="stylesheet" href="css/style.css" />

  <script type="text/javascript">
    $(document).ready(function() {
      $("#insert").click(function() {
        var username = $("#username").val();
        var password = $("#password").val();
        var dataString = "username=" + username + "&password=" + password;
        if ($.trim(username).length > 0 && $.trim(password).length > 0) {
          $.ajax({
            type: "POST",
            url: "http://localhost/jan/login.php",
            data: dataString,
            crossDomain: true,
            cache: false,
            success: function(data) {
              if (data == "success") {
                window.sessionStorage.setItem("loggedIn", 1);
                window.sessionStorage.setItem("username", username);
                window.location.replace("index.html");
              } else if (data == "error") {
                alert('error');
              }
            }
          });
        }
        return false;
      });
    });
  </script>
</head>

<body>
  <div data-role="page">
    <div data-role="header"><h1>Login</h1></div>
    <div data-role="content">
      <div class="list">
        <div class="item">
          <label>Username</label>
          <input type="text" id="username" value="" />
        </div>
        <div class="item">
          <label>Password</label>
          <input type="password" id="password" value="" />
        </div>
        <div class="item">
          <input type="button" id="insert" class="button button-block" value="Login" />
        </div>
      </div>  
    </div>
    <div data-role="footer" class="center">
      <a href="register.html">Register</a>
    </div> 
  </div>
</body>

</html>

这是login.php

<?php
    include 'db.php';

    $data=array();
    $q=mysqli_query($con,"select password from users where username=\"{$_POST['username']}\"");

    if(mysqli_num_rows($q) === 0)
        die('error'); 

    $row=mysqli_fetch_assoc($q);

    if($row['password'] == $_POST['password'])
        echo 'success';
?>

一切都可以在浏览器中按预期工作,但是当我尝试使用Android Studio在Android模拟器中运行它时,提交按钮什么也没做。 但是,当我按“注册”时,它会切换页面。 因此,我得出结论,这一定是由于错误的Ajax实现或数据库设置所致。

PS是否需要将我的所有文件从www文件夹复制到platform / android / assets / www以查看应用程序上的更改?

PPS在我看来,这必须是数据库问题,因为仿真器无法访问本地主机。 如果我想使用我的应用程序的用户登录/注册来使用cordova应用程序,该如何设置我的后端? 我曾尝试从网络服务器运行这些php文件,但由于我的应用程序和数据库不在同一域上运行,因此遇到了Ajax跨域问题。

使用您的计算机的IP地址而不是localhost然后它将起作用。 无需复制这些文件。它将在您构建应用程序时自动复制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM