简体   繁体   English

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

[英]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.我正在尝试将用户的帖子从我的数据库加载到网站,但由于某种原因没有加载 ajax 部分。 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? ajax代码或索引页面中编写的代码是否有问题?

Here in the index page the posts_area div part isn't loading在索引页面中,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);
}
?>
<?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);
    }
?>

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

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