首页 > 解决方案 > 通过使用 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>

任何帮助将不胜感激..提前致谢!

标签: javascriptdatabaseangularphonegap

解决方案


推荐阅读