我最近开始使用Ionic开发应用程序。由于我对MySQL的后端解决方案比较熟悉,我想做的是:从表单中提取凭据并将它们发送到MAMP服务器上的php文件。此文件将检查帐户是否存在并作出响应。
以下是表格:
<form name="loginForm" novalidate> <!-- ng-submit="LogIn(loginForm)" -->
<div class="section-big">
<div id="logo_container">
<img src="img/Logo.png" id="logo">
</div>
<div id="login_container">
<input class="login_item" name="email" type="email" ng-model="userdata.email" placeholder="email" ng-minlength="5" required>
<input class="login_item" name="password" type="password" ng-model="userdata.password" placeholder="password" ng-minlength="5" required>
</div>
</div>
<span> {{responseMessage}} </span>
<div class="section-small">
<button type="button" class="button button-full button-outline button-light login-inactive" ng-show="loginForm.$invalid">LOGIN</button>
</div>
</form>
离子控制器中的功能:
$scope.logIn = function(userdata) {
$scope.responseMessage = "in treatement";
console.log($scope.userdata.email)
var request = $http({
method: 'post',
url: 'http://localhost:8888/login.php',
crossDomain: true,
headers: {'Content-Type' : 'application/x-www-form-urlencoded'},
data: {
email: $scope.userdata.email,
password: $scope.userdata.password
},
});
console.log("passed");
request.success(function(data) {
if(data == "1"){
$scope.responseMessage = "Login Successful";
}
else {
$scope.responseMessage = "Wrong credentialsemai";
}
});
console.log("end reached");
}
login.php文件(基于我对StackSkills的调查结果):
<?php
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;
$password = $request->password;
include_once 'dbconnect.php';
$request = "SELECT * FROM User WHERE email ='$email' AND password = '$password'";
$result = $conn->query($request);
if($result){
echo "1";
}
else {
echo "0";
}
?>
事态:
目前,触发了logIn功能。由于{{ responseMessage }}
没有改变,我认为信息没有发送/不会从`login.php返回。
有没有人有想法要么修复这个或者仍然使用Ionic到php for MySQL?
修改:
正在检索数据,但由于以下错误,我仍无法发送数据:
SyntaxError: Unexpected token N
谢谢!
答案 0 :(得分:1)
此错误是因为在php中您的第一行是header("Content-Type: application/json; charset=UTF-8");
这意味着它尝试在json中对响应进行编码,但是您只返回字符串1
或0
在php中尝试以下内容。
if($result){
echo json_encode(array("status"=>1));
}
else {
echo json_encode(array("status"=>0));
}