[英]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。
該應用程序是使用 phonegap 制作的。 我正在向服務器發送 Ajax 調用以執行 a.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:;
任何想法為什么這不起作用?
此致。
您的 url 缺少協議前綴 - http://。
$.ajax({
type: "POST",
url: "http://192.168.1.9/mysite/register.php",
etc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.