doudie2693 2016-09-19 12:16
浏览 84
已采纳

AJAX请求不在cordova中工作?

From what I've gathered every piece of communication with the database should go through ajax requests in cordova application. So i've set up a localhost, made a database, created my php files somewhere there and started setting up cordova. I've developed it in browser first and managed to get it going but now every ajax request is not ran. For example this is my login.html file:

<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>

This is 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';
?>

Everything works as expected in the browser but when I try to run it in Android Emulator using Android Studio the submit button does nothing. However when i press Register it switches pages like it should. So I concluded that it has to be because of the wrong ajax implementation or database setup.

P.S. Is it rly needed to copy all my files from www folder to platforms/android/assets/www to see the changes on the app?

P.P.S In my opinion it has to be the database problem since emulator can't access localhost. How to setup my backend if I'd like to have cordova app using login/registration of users using my application? I've tried running those php files from webserver but ran into cross-domain problems with ajax since my app and my database don't run on same domain.

  • 写回答

1条回答 默认 最新

  • dqayok7935 2016-09-19 13:44
    关注

    Use IP address of your computer instead of localhost.Then it will work. No need to copy those files.It will automatically copy when you build your application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧