大家好,感谢您的宝贵时间!我是 AngularJS 的新手,目前正在使用服务器端部分处理我的第一个表单。我在 VirtualBox 上运行,使用 Yeoman 进行设置。
我的 HTML 有 2 个字段:用户名和密码,它们依次传递给 js 文件:
function authUsers($scope, $http) {
$scope.url = '../api/authUsersService.php'; // The url of our search
// The function that will be executed on button click (ng-click="search()")
$scope.loginAttempt = function() {
// Create the http post request
// the data holds the keywords
// The request is a JSON request.
alert($scope.session.username);alert($scope.session.password);
$http.post($scope.url, { "username" : $scope.session.username, "password" : $scope.session.password}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data; // Show result from server in our <pre></pre> element
alert(data);
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
alert(data);
alert(status);
});
};
}
我收到 2 个警报(用户名、密码)。该文件和 HTML 本身位于 Angular 的 APP 文件夹下。在文件夹之外,在同一个包含文件夹中:我创建了“API”文件夹。这是文件 api/authUsersService.php:
<?php
$data = file_get_contents("php://input");
$objData = json_decode($data);
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "select userID from authUsers where username = " . $objData->username . " and password = " . $objData->password);
if ($result->num_rows > 0) {
$row = $result->fetch_array(MYSQLI_ASSOC);
echo $row["userID"];
} else {
echo "";
}
?>
提交 HTML 表单时,我会从控制器(js 文件)中获取所有警报,包括“.error”警报。我在错误中得到的数据:“无法发布到/api/authUsersService.php”,状态为“404”。
我找不到任何解决方案。在 var\www\http 文件夹中尝试了 .htaccess,但没有帮助。请帮我成功获取PHP服务器代码!谢谢!