javascript - 通过使用 HTML、JS 和 PHP 的 PhoneGap 使用远程数据库
问题描述
我的 PhoneGap 应用程序遇到了一些困难。我想使用允许远程访问的外部 MySQL 数据库。它在我的浏览器中使用远程 IP 地址正常工作。使用PhoneGap编译和安装时,似乎连接不正常。我在前端使用 HTML 和 Angular,在后端使用 PHP 进行插入/读取。
这是我的 login.js:
angular.module('postLogin', [])
.controller('PostController', ['$scope', '$http', function($scope, $http) {
this.postForm = function() {
var encodedString = 'username=' +
encodeURIComponent(this.inputData.username) +
'&password=' +
encodeURIComponent(this.inputData.password);
$http({
method: 'POST',
url: 'http://78805.ict-lab.nl/keuze_eind/api/auth_login.php',
data: encodedString,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
//console.log(data);
if ( data.trim() === 'correct') {
window.location.href = 'home.php';
} else {
$scope.errorMsg = "Username and password do not match.";
}
})
}
}]);
auth_login.php:
<?php
require_once '../config.php';
global $dbc;
$stmt = $dbc->prepare("SELECT id,username,email,password from k_users WHERE
username='".$_POST['username']."' && password='". $_POST['password']."'");
$stmt->execute();
$row = $stmt->rowCount();
if ($row > 0){
echo 'correct';
} else{
echo 'wrong';
}
?>
配置.php:
<?php
header("Access-Control-Allow-Origin: *");
//connect to MySql database
try {
$dbc=new PDO("mysql:host=185.224.**.**;dbname=admin","admin_keuze","***")
or die("Kon geen verbinding maken met de database.");
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
?>
PhoneGap config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id. = "1.0.0" >
<name>KnipKappers</name>
<description>
De app voor voor KnipKappers.
</description>
<plugin name="cordova-plugin-device" spec="~2" source="npm" />
<plugin name="cordova-plugin-battery-status" spec="~2" source="npm" />
<plugin name="cordova-plugin-device-orientation" spec="~2" source="npm" />
<plugin name="cordova-plugin-camera" spec="~4" source="npm" />
<preference name="phonegap-version" value="cli-7.1.0" />
<gap:plugin name="cordova-plugin-whitelist" source="npm"/>
<access origin="*" subdomains="true" />
</widget>
任何帮助将不胜感激..提前致谢!
解决方案
推荐阅读
- ruby-on-rails - 通过 Ruby on Rails 中的 HTTP 标头自定义授权
- terminal - 如何在两个终端之间通话,一个使用 Ncat,一个使用 Nping?
- ios - 点击segmentedControl时SwiftUI关闭键盘
- javascript - 使用变量反应动态导入不起作用
- typescript - “错误:无法处理请求”谷歌云功能错误
- c - 有没有办法硬编码一个二维整数数组而不必在 C 中提及任何维度?
- python - 即使日期存在,SQLAlchemy 按日期过滤也不返回
- python - 如何在 Aegis Blade 中运行 Selenium
- google-bigquery - Big Query 中的双重命令
- java - Android Studio 错误器 OnCreate