Ajax不在实时服务器上加载帖子,但在本地主机上工作

时间:2019-11-29 09:16:13

标签: php jquery mysql ajax

我正尝试将用户的帖子从数据库加载到网站,但是由于某种原因ajax部分未加载。它在localhost上运行,但在实时服务器上不运行。 ajax代码或索引页面中编写的代码是否存在问题?

在索引页面中,posts_area div部分未加载

index.php
<?php 
include("includes/header.php");
if(isset($_POST['post'])){
$post = new Post($con, $userLoggedIn);
$post->submitPost($_POST['post_text'], 'none');
}
?>

<div class="main_column column">
    <form class="post_form" action="index.php" method="POST">
        <textarea name="post_text" id="post_text" placeholder="Got something to say?"></textarea>
        <input type="submit" name="post" id="post_button" value="Post">
        <hr>
    </form>
    <div class="posts_area"></div>
    <img id="loading" src="assets/images/icons/loading.gif">
</div>

<div class="user_details column">
    <h4>Popular</h4>
    <div class="trends">
        <?php 
        $query = mysqli_query($con, "SELECT * FROM trends ORDER BY hits DESC LIMIT 9");
        foreach ($query as $row) {          
            $word = $row['title'];
            $word_dot = strlen($word) >= 14 ? "..." : "";
            $trimmed_word = str_split($word, 14);
            $trimmed_word = $trimmed_word[0];
            echo "<div style'padding: 1px'>";
            echo $trimmed_word . $word_dot;
            echo "<br></div><br>";
        }
        ?>
    </div>
</div>

<script>
var userLoggedIn = '<?php echo $userLoggedIn; ?>';

$(document).ready(function() {
    $('#loading').show();
    //Original ajax request for loading first posts 
    $.ajax({
        url: "https://bestconnect.000webhostapp.com/includes/handlers/ajax_load_posts.php",
        type: "POST",
        data: "page=1&userLoggedIn=" + userLoggedIn,
        cache:false,
        success: function(data) {
            $('#loading').hide();
            $('.posts_area').html(data);
        }
    });
    $(window).scroll(function() {
        var height = $('.posts_area').height(); //Div containing posts
        var scroll_top = $(this).scrollTop();
        var page = $('.posts_area').find('.nextPage').val();
        var noMorePosts = $('.posts_area').find('.noMorePosts').val();
        if ((document.body.scrollHeight == document.body.scrollTop + window.innerHeight) && noMorePosts == 'false') {
            $('#loading').show();
            var ajaxReq = $.ajax({
                url: "includes/handlers/ajax_load_posts.php",
                type: "POST",
                data: "page=" + page + "&userLoggedIn=" + userLoggedIn,
                cache:false,
                success: function(response) {
                    $('.posts_area').find('.nextPage').remove(); //Removes current .nextpage 
                    $('.posts_area').find('.noMorePosts').remove(); //Removes current .nextpage 
                    $('#loading').hide();
                    $('.posts_area').append(response);
                }
            });
        } //End if 
        return false;
    }); //End (window).scroll(function())
});
</script>
</div>

ajax_load_posts.php
<?php
include("../../config/config.php");
include("../classes/User.php");
include("../classes/Post.php");
$limit = 10; //Number of posts to be loaded per call
if (isset($_GET['posts'])) {
$posts = new Post($con, $_REQUEST['userLoggedIn']);
$posts->loadPostsFriends($_REQUEST, $limit);
}
?>

1 个答案:

答案 0 :(得分:0)

<?php
header("Access-Control-Allow-Origin: *");
include("../../config/config.php");
include("../classes/User.php");
include("../classes/Post.php");
$limit = 10; //Number of posts to be loaded per call
if (isset($_GET['userLoggedIn'])) 
    {
        $posts = new Post($con, $_REQUEST['userLoggedIn']);
        $posts->loadPostsFriends($_REQUEST, $limit);
    }
?>