【问题标题】:Why ajax calls respond with permission denied为什么ajax调用响应权限被拒绝
【发布时间】:2020-11-29 18:34:11
【问题描述】:

我一直在使用 Amazon Web Services (AWS) 来开发我的网络应用程序。我使用 EC2、S3 和 RDS。自从我开始以来已经一个月了,突然 ajax 调用停止工作并以 Permission Denied 进行响应。

我已尝试允许来自任何来源的所有流量并更改 ajax 代码中的 url 以包含绝对路径。

我的所有代码都在免费层 EC2 实例(虚拟机)上。这是我的 ajax 代码的一部分,用于在注册时验证新用户名:

$.ajax({
    type: 'GET',
    url: 'validateUsername.php',
    data: { usernameInput: usernameInputText },
    success : function(response) {
            //if sql query doesn't return a match
            //then username is valid and not taken
//                              alert("response: " + response);
            if(response == "username taken"){
                    usernameValid = false;
                    var popup = document.getElementById("usernameInvalidPopUp");
                    if(popup.innerHTML == "Please enter a username."){
                            popup.innerHTML = "Username taken";
                    }
                    else if(popup.innerHTML == ""){
                            popup.innerHTML = "Username taken";
                            popup.classList.toggle("show");
                    }
                    else{
                    }
            }
            if(response == "username available"){
                    var popup = document.getElementById("usernameInvalidPopUp");
                    if(popup.innerHTML == "Username taken" || popup.innerHTML == "Please enter a username."){
                            popup.classList.toggle("show");
                            popup.innerHTML = "";
                    }
                    usernameValid = true;

            }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("username Status: " + textStatus);
            alert("uError: " + errorThrown);
    }
});

这是服务器端脚本:

<?php
    //do not display error when there is no get or post values
    //ini_set('display_errors', 0);
    //error_reporting(E_ERROR | E_WARNING | E_PARSE);

    $servername = "x";
    $username = "x";
    $password = "x";
    $dbname="x";
    // Create connection
    // https://www.youtube.com/watch?v=5xnXHLHR3AE
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $username = $_GET["usernameInput"];

    //procedural style: https://www.php.net/manual/en/mysqli.real-escape-string.php

    $usernameString = mysqli_real_escape_string($conn, $username);

    // Prepare our SQL, preparing the SQL statement will prevent SQL injection.
    if ($stmt = $conn->prepare("SELECT * FROM user WHERE username = ?")) {
            // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
            $stmt->bind_param('s', $usernameString);
            $stmt->execute();
            // Store the result so we can check if the account exists in the database.
            $stmt->store_result();

            if ($stmt->num_rows > 0) {
                    //$stmt->bind_result($id, $password);
                    //$stmt->fetch();
                    echo "username taken";
            }
            else{
                    echo  "username available";
            }
            $stmt->close();
    }
    //$sql = "SELECT * FROM user WHERE username='".$usernameString."';";

    //$result = $conn->query($sql);

    $conn->close();
?>

【问题讨论】:

  • 可以直接使用 Postman 调用 PHP 脚本。如果失败了,你的错误很可能是服务器端的,如果没问题,这意味着你在 JS 中有一个错误。如果错误出现在服务器上,请确保您的文件权限设置正确。可能是您的 Web 服务器无权执行该 PHP 文件。
  • 问题是我启用了 SELinux。我通过编辑 /etc/sysconfig/selinux 禁用了它。 ``` 纳米 /etc/sysconfig/selinux ```

标签: php ajax amazon-s3 amazon-ec2 amazon-rds


【解决方案1】:

问题是我启用了 SELinux。我通过编辑 /etc/sysconfig/selinux 禁用了它。

nano /etc/sysconfig/selinux

然后设置 SELINUX=disabled

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多