0

こんにちは。お時間をいただきありがとうございます。私は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サーバーコードを正常に取得するのを手伝ってください! ありがとう!

4

1 に答える 1