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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题