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

问题描述 投票:0回答:1

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

[该应用程序是使用phonegap制作的。我正在向服务器发送Ajax调用以执行一个.php文件,该文件将与MySQL DB对话并存储一些用户数据,但无法实现。另一方面,当我尝试从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:;

任何想法为什么这不起作用?

最好的问候。

javascript php cordova mobile phonegap
1个回答
0
投票

您的网址缺少协议前缀-http://。

$.ajax({
    type: "POST",
    url: "http://192.168.1.9/mysite/register.php",
    etc
© www.soinside.com 2019 - 2024. All rights reserved.