2016-09-19 12:16
浏览 83


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:

  <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/"></script>
  <link rel="stylesheet" href="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) {
            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);
              } else if (data == "error") {
        return false;

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


This is login.php

    include 'db.php';

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

    if(mysqli_num_rows($q) === 0)


    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条回答 默认 最新

相关推荐 更多相似问题