繁体   English   中英

PHP响应未由Ajax发送回

[英]PHP response is not being sent back by ajax

当窗口加载事件发生时,我试图使用AJAX从数据库获取值。 当我访问PHP脚本时,我在屏幕上获得了正确的值,但是当我尝试通过AJAX在应有的其他页面上获得它时,我仅获得HTML而不是PHP值。 请帮我。 从昨天开始,我很沮丧地试图解决这个问题!

这是我的ajax脚本:

    <script type="text/javascript">
    function getReplies() {
        var commentDiv = document.getElementById("reply_comment");
        var xhr = new XMLHttpRequest();
        xhr.open('POST', "widgets/board_reply_fetch.php?comment_id=<?php echo $board_comment_id_number;?>", true);
        // when we make POST request, we must add this first line
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;
                //result2 = result.split('/');
                console.log('Result: ' + result);
                commentDiv.innerHTML = result;
            }
        };
        xhr.send("comment_id=<?php echo $board_comment_id_number;?>");
    }

    window.addEventListener("load", getReplies);
    </script>

这是我的PHP脚本:

<?php 
require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';


$comment_id = isset($_GET['comment_id']) ? (int)$_GET['comment_id'] : '';

$reply_data = find_board_replies_by_comment_id($comment_id);

$reply_assoc = mysqli_fetch_assoc($reply_data);
$reply_comment_id = $reply_assoc['comment_id'];
$reply_board_id = $reply_assoc['board_id'];
$reply_user_id = $reply_assoc['user_id'];
$reply_text = $reply_assoc['reply'];
$reply_timestamp = $reply_assoc['reply_timestamp'];

$reply_user_data = find_user_data_by_id($reply_user_id);


$profile_image = $reply_user_data['profile_picture']; 
$profile_image_thumb = "uploaded_pictures/profile/$reply_user_id/" . $reply_user_id . "small.png";

if ($profile_image == "") {
    if ($reply_user_data['gender'] == "Male"){
        $user_profile_picture = "images/ProfilePicMale.png";
    } else {
        $user_profile_picture = "images/ProfilePicFemale.png";
    }
} else {
    $user_profile_picture = $profile_image_thumb;
}

$full_name = ucfirst(htmlentities($reply_user_data['first_name'])) . " " . ucfirst(htmlentities($reply_user_data['last_name']));
$time_of_post = time_of_post($reply_timestamp);
$the_reply_text = nl2br($reply_text);

?>  

<div class="reply_comment_div">
<a href="profile.php?user_id=<?php echo $reply_user_id;?>" class="board_comments_div_picture">
<img src="<?php echo $user_profile_picture;?>" width="50px" height="50px" /></a>
<a href="profile.php?user_id=<?php echo $reply_user_id;?>" class="board_comments_reply_link"><?php echo $full_name;?></a>

    <a href="edit_comment_board.php?comment_id=<?php echo $reply_comment_id;?>" class="edit_comment_button_board">Edit</a>
    <a href="widgets/delete_board_comment.php?board_id=<?php echo $reply_board_id;?>&comment_id=<?php echo$reply_comment_id;?>" onclick="return confirm('Are you sure you want to delete this admin?')" class="delete_button_status">Delete</a>

<div class="board_comment_submited_on">submitted <?php echo $time_of_post;?></div>
<span class="comment_content_span"><?php echo $the_reply_text;?></span>
</div>

我可以在这里看到几个问题:

  • 使用POST请求,您不必传递查询字符串参数。 您应该改为在请求正文中传递它们。 因此,删除xhr.open中的查询字符串
  • 发送POST请求后,您尝试通过$_GET访问有效负载,该负载用于获取GET请求中的查询字符串参数。 您应该改用$_POST

修改这两个应该可以使工作正常。

话虽如此,我建议您使用GET请求。 由于数据库状态未更改(不执行创建或更新操作),因此GET请求更为合理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM