AngularJS:无法发布到服务器PHP文件(获取404)


AngularJS: cannot POST to server PHP file (getting 404)

大家好,谢谢你的时间!我是 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服务器代码!谢谢!

使用 "..URL 中的/" 是一个很好的指标,表明您做错了什么。

你不能"走出"网络服务器的根目录,所以你需要把它放在"app"目录中,以便它可以在网络上访问。