Javascript 使用IONIC获取MySQL数据
我第一次尝试从MySQL表中获取一些数据 我正在使用ionic、PHP和AngularJS进行尝试,但我的查询没有返回任何结果。当我第一次开发它的时候,我正在关注它。尽管如此,当我运行代码时,我的文件只返回json文件之外的另一个文件的内容以及我想要的所有内容 基本上,我的应用程序控制器中有一个函数,我在一个按钮中调用它(用于测试)。此函数通过Angular向我的查询中的另一个文件发出$http请求。这个包含查询的文件调用另一个文件来打开数据库连接e通过Json返回数据 但我刚刚打开了第一个文件,作为该过程的返回。有人能帮我吗 app.jsJavascript 使用IONIC获取MySQL数据,javascript,php,angularjs,ionic,Javascript,Php,Angularjs,Ionic,我第一次尝试从MySQL表中获取一些数据 我正在使用ionic、PHP和AngularJS进行尝试,但我的查询没有返回任何结果。当我第一次开发它的时候,我正在关注它。尽管如此,当我运行代码时,我的文件只返回json文件之外的另一个文件的内容以及我想要的所有内容 基本上,我的应用程序控制器中有一个函数,我在一个按钮中调用它(用于测试)。此函数通过Angular向我的查询中的另一个文件发出$http请求。这个包含查询的文件调用另一个文件来打开数据库连接e通过Json返回数据 但我刚刚打开了第一个文件
// Ionic Starter App
// angular.module is a global place for creating, registering and
retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a
<body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var secompApp = angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory
bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
secompApp.controller("SeCompController", function($scope,
$cordovaBarcodeScanner, $http){
$scope.scanBarcode = function(){
$cordovaBarcodeScanner.scan().then(function(imageData){
//imageData = barcode content
//TRANSFORMAR ALERT POR FUNÇÃO QUE ENVIA imageData(DADO LIDO) PARA O BD
alert(imageData.text);
console.log("format: " + imageData.format);
}, function(error){
console.log("Ocorreu um erro, tente novamente. Erro:" + error);
});
}
$scope.getMinicursos = function(){
$http.get("ajax/getMinicursos.php").success(function(data){
$scope.minicursos = data;
alert(data);
});
}
});
//Ionic Starter应用程序
//angular.module是用于创建、注册和
检索角模
//“starter”是此角度模块示例的名称(也在
(index.html中的属性)
//第二个参数是“requires”的数组
var secompApp=angular.module('starter',['ionic','ngCordova']))
.run(函数($ionicPlatform){
$ionicPlatform.ready(函数(){
//默认情况下隐藏附件栏(将其移除以显示附件
键盘上方的横条
//表格输入)
if(window.cordova&&window.cordova.plugins.Keyboard){
插件键盘hideKeyboardAccessoryBar(真);
}
如果(窗口状态栏){
StatusBar.styleDefault();
}
});
})
secompApp.controller(“secompApp.controller”),功能($scope,
$cordovaBarcodeScanner,$http){
$scope.scanBarcode=函数(){
$cordovaBarcodeScanner.scan().then(函数(imageData){
//imageData=条形码内容
//TRANSFORMAR ALERT POR FUNÃO QUE ENVIA imageData(DADO LIDO)PARA O BD
警报(imageData.text);
日志(“格式:”+imageData.format);
},函数(错误){
控制台日志(“Ocorreu um erro,tente novatemente.erro:+错误);
});
}
$scope.getMinicursos=函数(){
$http.get(“ajax/getMinicursos.php”).success(函数(数据){
$scope.minicursos=数据;
警报(数据);
});
}
});
getMinicursos().php
<?php
require_once '../includes/config.php'; // The mysql database connection script
if(isset($_GET['name'])){
$task = $_GET['name'];
$status = "0";
$created = time();
$query="SELECT * from course";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$result = $mysqli->affected_rows;
echo $json_response = json_encode($result);
}
?>
<?php
$servername = "localhost";
$username = "abc";
$password = "123";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
config.php
<?php
require_once '../includes/config.php'; // The mysql database connection script
if(isset($_GET['name'])){
$task = $_GET['name'];
$status = "0";
$created = time();
$query="SELECT * from course";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$result = $mysqli->affected_rows;
echo $json_response = json_encode($result);
}
?>
<?php
$servername = "localhost";
$username = "abc";
$password = "123";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
我的错误是在Ionic localhost中使用PHP,我没有支付在安装了PHP的服务器中没有运行PHP的费用来正确“编译”我的文件并取回我正在查找的数据
因此,如果您要返回整个PHP文件,而不是由服务器处理,则应确保您的服务器中安装了PHP,即使是本地/本地主机。首先只需通过在浏览中键入url来检查php文件响应是否有效?请检查控制台,它是否会显示任何错误?谢谢你们的回答,但我可以解决我的问题。这只是处理CORS问题。请从标题和问题中删除“已解决”。你可以自己发布答案,甚至接受它。对于其他有类似问题的人来说,这是正确的指示。