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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵