通过 phonegap 应用程序从同一网络中的移动设备与本地服务器通信

Communicate with local server from a mobile in the same network through a phonegap app

我正在尝试创建一个将与服务器通信以存储数据的应用程序。 我正在尝试的配置如下。 局域网上的 Apache 服务器 IP:192.168.1.9:80 随机局域网 IP 上的移动设备:192.168.1.3(示例)

该应用程序是使用 phonegap 制作的。 我正在向服务器发送 Ajax 调用以执行一个 .php 文件,该文件将与 MySQL 数据库通信并存储一些用户数据,但无法实现。另一方面,当我从 Chrome(在移动设备上)尝试相同的应用程序时,通信已建立并且数据存储到后端数据库。

现在,我想我知道问题所在,但我无法解决它。 我认为的问题是,由于安全限制,phonegap 不允许在服务器端执行代码(并且有一个很明显的原因)。我知道我必须在 phonegap 的 config.xml 上插入一些 <meta> 语句才能允许这种通信,但我尝试的一切似乎都不起作用。

部分代码: Ajax 通话:

 $.ajax({
        type: "POST",
        url: "192.168.1.9/mysite/register.php",
        data:{uname:u_user, pass:u_pass,uid:u_uid, email:u_email, fullname:u_fullname, address:u_address, telephone:u_telephone}, 
        crossDomain: true,
        cache: false,
        success: function(d){
        if (d == 'This email is already being used') {
         alert ("The email is already being used. New account with existing email cannot be created.")
         return true;
        }
        alert("Thank you for registering!");
        },
        error: function(e) {
        alert (e)
        alert("An error has occurred. Please contact reseller.")
  }
   });

当我在开发者模式下打开 chrome 以查找错误时,我得到:

file:///android_asset/www/192.168.1.9/mysite/register.php net::ERR_FILE_NOT_FOUND

这表明请求不是在服务器上发出的,而是应用正在寻找移动设备中的本地文件。

config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<widget 
xmlns = "https://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.nsbasic.{id}"
versionCode = "{phoneGapBuildCounter}"
version = "{version}">

<name>{title}</name>
<description>{description}</description>
<preference name="phonegap-version" value="{phoneGapVersion}" />

<icon src='{icon}' />
<preference name='SplashScreenDelay' value='2000' />
<preference name='AutoHideSplashScreen' value='true' />
<plugin name='cordova-plugin-splashscreen' source='npm' />

<preference name="permissions" value="none"/>
<!-- sample preference specifications -->
<!-- <preference name="autorotate" value="false" readonly="true"/> -->
<!-- <preference name="orientation" value="default" /> -->
<!-- <preference name="fullscreen" value="true" /> -->

<!-- Platforms: Customize as needed. -->
<gap:platforms>
   <gap:platform name="android" />
   <gap:platform name="ios" />
</gap:platforms>

<plugin name="cordova-plugin-statusbar" source="npm" />
  <preference name="StatusBarOverlaysWebView" value="{phoneGapStatusBarOverlay}" />
  <preference name="StatusBarBackgroundColor" value="{phoneGapStatusBarColor}" />
  <preference name="StatusBarStyle" value="{phoneGapStatusBarStyle}" />

<plugin name="cordova-plugin-whitelist" source="npm" />
  <allow-navigation href="*" />
  <access origin="*" />
  <allow-intent href="*" />
</widget>

我还为内容安全策略添加了以下内容:

default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline'; media-src *; img-src * data:;

知道为什么这不起作用吗?

此致。

您的 url 缺少协议前缀 - http://.

$.ajax({
    type: "POST",
    url: "http://192.168.1.9/mysite/register.php",
    etc