简体   繁体   中英

Ajax not loading posts on live server but working on localhost

I'm trying to load posts of users from my database to the website but the ajax part isn't loading for some reason. It was working on localhost but not working on the live server. Is there some problem in the ajax code or the code written in the index page?

Here in the index page the posts_area div part isn't loading

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);
}
?>
<?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);
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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