简体   繁体   English

如何使 Phonegap 连接到本地主机

[英]How to make Phonegap connect to localhost

I am working with phonegap for the first time to build hybrid mobile app with back-end(php, mysql).我第一次使用 phonegap 构建带有后端(php、mysql)的混合移动应用程序。 So i am doing a test on how phonegap can connect to php on localhost to send and retrieve data.所以我正在测试phonegap如何连接到本地主机上的php来发送和检索数据。 But no data was retrieved, I have reduced my codes to the this and i see no errors in both ajax call and php code.但是没有检索到数据,我已将我的代码减少到此,并且我在 ajax 调用和 php 代码中都没有发现错误。 So i guess it should be the way phonegap connects to backend that i am getting wrong, please help.所以我想这应该是phonegap连接到后端的方式我弄错了,请帮忙。

html form and ajax call: html 形式和 ajax 调用:

        <form id="form1">
       <input  type="text" id="email"  />
         <input  type="password" id="password"  />
         <input type="submit" name="login" id="login" value="Login">  
        </form>

      <script type="text/javascript">
      $("form").submit(function(){
         var data= ("#form1").serialize();
       $.post("http://localhost/securityapp/login.php",data,function(response){
     alert(response);
       });

        });

      </script>

php file: php 文件:

          <?php
           include 'db.php';
      session_start();
     if ($_POST ) {
     echo $_POST;

        }


           ?>

Basically it is meant to alert to values sent to php script as the response but it is not doing so, network tab says 200 for status.基本上它是为了提醒发送到 php 脚本的值作为响应,但它没有这样做,网络选项卡显示状态为 200。 what am i doing wrong?我究竟做错了什么? I feel phonegap isn't connecting to the url defined我觉得 phonegap 没有连接到定义的 url

This is how I solved this issue:这就是我解决这个问题的方法:

  1. created a table on a database that holds the current URL/IP of the server在包含服务器当前 URL/IP 的数据库上创建了一个表

  2. Created a check-url.php file, this file runs a query on the database to see IP/URL of the server创建了一个 check-url.php 文件,该文件在数据库上运行查询以查看服务器的 IP/URL

  3. I created a connection.js file, this file basically makes a request to a check-url.php to get current IP of the server to use in Cordova app我创建了一个 connection.js 文件,该文件基本上向 check-url.php 发出请求,以获取服务器的当前 IP 以在 Cordova 应用程序中使用

check-url.php检查-url.php


 header("Access-Control-Allow-Origin: *"); //to allow phonegap access it because of cross origin and domain restrictions
 require 'db.php';
 $query= "SELECT url  FROM settings";
 $query= mysqli_query($conn,$query);
 $row = mysqli_fetch_assoc($query);
 $row= $row['url'];
 echo $row; //current IP exmaple:http://127.0.0.1:80/

connection.js连接.js

  //check current server ip from database
export function check_url(callback) {
  var httpRequest = new XMLHttpRequest();
  httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState === 4) { // request is done
          if (httpRequest.status === 200) { // successfully
              callback(httpRequest.responseText); // we're calling our method
          }
      }
  };
  httpRequest.open('GET', "http://127.0.0.1:80/projectname/check-url.php");
  httpRequest.send();
}

So any time I want to connect to backend by Cordova I import the function check-url() from connection.js into my front-end ajax request url like this: So any time I want to connect to backend by Cordova I import the function check-url() from connection.js into my front-end ajax request url like this:

<script type="module">
   import {check-url} from './connection.js';

check-url(function(result) {
var currentIP= result;
$.ajax({
type:"POST",
url: currentIP + "projectapp/login.php",
data:data,
success: function(data){
//do something
}
});
});
</script>  

Note: The URL/IP in connection.js, database, and server IP should be the same, as this helps you not to repeat IP always and also to test when using PhoneGap mobile and also when you switch to live, change it to the current IP address of your project.注意:connection.js、数据库和服务器 IP 中的 URL/IP 应该相同,因为这可以帮助您不要总是重复 IP 并且还可以测试使用 PhoneGap 移动设备时以及切换到 live 时,将其更改为您项目的当前 IP 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM