我的问题
除了CORS,有人有解释吗,为什么Ionic不能将Post数据发送到php,php不能通过看到设置的数据
isset($_POST(u_name))
例如,我试图将一些登录信息发送到我的php文件"login_check.php"。
下面是我的控制器.js
.controller('LoginCtrl', function($scope, $http, $ionicPopup, $state) {
$scope.data = {};
$scope.login = function() {
console.log("Login pge :: getlogin");
var link = 'http://192.168.1.2:80/mySite/login_check.php';
var request = $http.post(link, { "u_name": $scope.data.username, "u_pass": $scope.data.password }, {'Content-Type': 'application/x-www-form-urlencoded'} );
request.success(function(response) {
console.log(response);
if(response.success === 1 ){
$state.go('tab.dash');
console.log(response);
}else{
//error handling
}
})
request.error(function(response) {
//error handling
});
}
})
我试着通过我的"login_check.php"阅读它,如下
<?php
include_once './database_conn.php'; //my DB configuration details
// array for JSON response
$response = array();
// check for required fields
if(isset($_POST('u_name')) && isset($_POST('u_pass'))){
$username = trim($_POST['u_name']);
$pass = trim($_POST['u_pass']);
// connecting to db
$db = new DbConnection();
// mysql checking the login
$result = mysql_query("SELECT tu_user, tu_pass FROM twp_users WHERE tu_user = '$username' AND tu_pass = '$pass'");
if (!empty($result)) {
// check if row selected or not
if (mysql_num_rows($result) > 0) {
// successfully retrieve data into database
$row = mysql_fetch_assoc($result);
$response["success"] = 1;
$response["message"] = "successful";
echo json_encode($response);
} else {
// failed to retrieve row
$response["success"] = 0;
$response["message"] = "Login failed. Either username or password is incorrect";
echo json_encode($response);
}
} else{
$response["success"] = 0;
$response["message"] = "No login found";
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is/are missing";
echo json_encode($response);
}
?>
每当我试图发送数据时,它总是说
"Required field(s) is/are missing"
注意:
此外,我的观点是,我正在尝试在几个客户端上使用相同的数据库和服务器端相同的脚本。到目前为止,JavaEE和Android正在从这个php返回数据。
如果我使用$_GET()并通过URL发送数据,那么这个php可以很好地与Cordova/Ionic配合使用。
请更改行:
if(isset($_POST(u_name)) && isset($_POST(u_pass)))
收件人:
if(isset($_POST['u_name']) && isset($_POST['u_pass']))
我希望它能有所帮助。
忘记引号和括号
if(isset($_POST['u_name']) && isset($_POST['u_pass']))
更改mysql查询如下:
$result = mysql_query("SELECT tu_user, tu_pass FROM twp_users WHERE tu_user = '{$username}' AND tu_pass = '{$pass}'");