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