Cordova不适用于Android应用程序,但适用于浏览器

时间:2018-02-10 19:26:42

标签: cordova

我正在尝试使用Ajax和php将数据插入到MYSQL数据库中。当我在Web浏览器上访问代码时,它能够成功地将数据发送到数据库。但是,当我上传到Android设备时,它无法发送数据 这是HTML代码:

<script type="text/javascript">
      $(document).ready(function() {
          $("#submit").click(function() {
               var team,color,auto,defense,scale,climb,speed,score,cards;
               //no wifi connection, save as local storage
               team=$("#team").val();
               color=$("#color").val();
               auto=$("#auto").val();
               defense=$("#defense").val();
               scale=$("#scale").val();
               climb=$("#climb").val();
               speed=$("#speed").val();
               score=$("#score").val();
               cards=$("#cards").val();
              var dataString = "team=" + team + "&color=" + color + "&auto=" + auto + "&defense="+defense+"&scale="+scale+"&climb="+climb+"&speed="+speed+"&score="+score+"&cards="+cards;
                  $.ajax({
                      type: "POST",
                      url: "https://nerdyscout2018.000webhostapp.com/main.php",
                      // data: dataString,
                      data:{team:team,color:color,auto:auto,defense:defense,scale:scale,climb:climb,speed:speed,score:score,cards:cards},
                      // crossDomain: true,
                      // cache: false,
                      beforeSend: function() {
                          $("#submit").val('Connecting...');
                          alert("about to send!");
                      },
                      success: function(data) {
                          // if (data == "success") {
                          //     alert("inserted");
                          //     $("#submit").val('submit');
                          // } else if (data == "error") {
                          //     alert("error");
                          // }
                          $("#submit").val('Submit');
                          alert(data);
                      }
                  });
              return false;
          });
      });
      </script>

这是php代码:

<?php
     header("Access-Control-Allow-Origin: *");
     $con = mysqli_connect("104.197.101.9","root","nerdherd687","db");
     if(!$con){
       die('Error:'.mysqli_connect_error());
     }
      $team = $_POST['team'];
      $color = $_POST['color'];
      $auto = $_POST['auto'];
      $defense = $_POST['defense'];
      $scale = $_POST['scale'];
      $climb = $_POST['climb'];
      $speed = $_POST['speed'];
      $score = $_POST['score'];
      $cards = $_POST['cards'];
      $result = mysqli_query($con, "DESCRIBE entries");
      while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        echo "{$row['Field']} - {$row['Type']}";
      }
      $q = mysqli_query($con, "INSERT INTO entries (teamNumber,color,autoRating,defRating,scale,climb,speed,endScore,cards) VALUES ('$team','$color','$auto','$defense','$scale','$climb','$speed','$score','$cards')");
      if($q) {
        echo "Records inserted successfully.";
      }
      else {
        echo "ERROR: Could not execute $q" .mysqli_error($con);
      }
?>

我认为问题是应用程序没有运行php代码,但我不确定。有没有人可能有答案?

1 个答案:

答案 0 :(得分:0)

Cordova应用无法运行PHP代码。但是PHP代码在你的服务器中,对吗?

您的问题可能是缺少白名单插件,或者配置不正确,或者内容安全策略配置错误。