Показывать предупреждение PHP при добавлении методов в ajax jQuery - PullRequest
0 голосов
/ 29 августа 2018

Я новичок в ajax и jQuery, я только что сделал это для своего последнего проекта года У меня есть некоторые проблемы при добавлении методов в сценарий Java, это мой сценарий AJAX:

$(document).ready(function() {
    $('#campus_feed').html('<br><br><br><br><br><br><br><br><br><br><br><br> 
    <center><img src="loader.gif"></center>');
    fetch_data();
    setInterval(fetch_data, 5000);  // 5 seconds

    // FETCHING DATA
    function fetch_data()
    {
       var action = "fetch";
       $.ajax({
            url:"fetch_action.php",
            method:"POST",
            data:{action:action},
            success:function(data)
                    {
                         $('#campus_feed').html(data);
                    }
       })
   }

   /////////////////////////////////////////////////////
   $("#profile_nav").click(function profile_dflt() {
        $('#content').html('<br><br><br><br><br><br><br><br><br><br><br><br><img src="loader.gif">');
        $.ajax({
          url: "profile.php",
          method:"POST",
          success: function(result)
          {
            $("#profile_nav").css({"background-color":"#4CAF50"});
            $("#nofication_nav").removeAttr("style");
            $("#chat_nav").removeAttr("style");
            $("#forum_nav").removeAttr("style");
            $("#post_nav").removeAttr("style");
            $("#content").html(result);
        }
      });
    });
 ////////////////////////////////////////////////////////////
 $("#nofication_nav").click(function(){
        $.ajax({
          url: "nofication.php",
          method:"POST",
          success: function(result)
          {
            $("#nofication_nav").css({"background-color":"#CCCC00"});
            $("#profile_nav").removeAttr("style");
            $("#chat_nav").removeAttr("style");
            $("#forum_nav").removeAttr("style");
            $("#post_nav").removeAttr("style");
            $("#content").html(result);
        }
      });
    });
 ///////////////////////////////////////////////////////////
 $("#chat_nav").click(function(){
        $.ajax({
          url: "chat.php",
          method:"POST",
          success: function(result)
          {
            $("#chat_nav").css({"background-color":"#008CBA"});
            $("#nofication_nav").removeAttr("style");
            $("#profile_nav").removeAttr("style");
            $("#forum_nav").removeAttr("style");
            $("#post_nav").removeAttr("style");
            $("#content").html(result);
        }
      });
    });
 ///////////////////////////////////////////////////////
 $("#forum_nav").click(function(){
        $.ajax({
          url: "forum.php",
          method:"POST",
          success: function(result)
          {
            $("#forum_nav").css({"background-color":"#FF00FF"});
            $("#nofication_nav").removeAttr("style");
            $("#chat_nav").removeAttr("style");
            $("#profile_nav").removeAttr("style");
            $("#post_nav").removeAttr("style");
            $("#content").html(result);
        }
      });
    });
 ///////////////////////////////////////////////////////
 $("#post_nav").click(function(){
        $.ajax({
          url: "post.php",
          method:"POST",
          success: function(result)
          {
            $("#post_nav").css({"background-color":"#F44336"});
            $("#nofication_nav").removeAttr("style");
            $("#chat_nav").removeAttr("style");
            $("#forum_nav").removeAttr("style");
            $("#profile_nav").removeAttr("style");
            $("#content").html(result);
        }
      });
    });
});

Когда я добавляю метод SETINTERVAL() и другие дополнительные методы, он показывает мне предупреждающее сообщение, я не знаю, почему отображается это предупреждение, я думаю, что я допустил ошибку в объявлении синтаксиса, пожалуйста, помогите мне решить эту проблему

1 Ответ

0 голосов
/ 29 августа 2018

Я проверил твой код. И это похоже на работу.

Единственная найденная ошибка - это объявление .html () в две строки. У вас должен быть параметр htmlString в одной строке.

Пожалуйста, попробуйте это:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<div id ="campus_feed"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $('#campus_feed').html('<br><br><br><br><br><br><br><br><br><br><br><br><center><img src="loader.gif"></center>');
        setInterval(fetch_data, 5000);//5 seconds

        //FETCHING DATA
        function fetch_data()
        {

            var action = "fetch";
            $.ajax({
                url:"fetch_action.php",
                method:"POST",
                data:{action:action},
                success:function(data)
                {
                    $('#campus_feed').html(data);
                }

            })
        }
/////////////////////////////////////////////////////
        $("#profile_nav").click(function profile_dflt(){
            $('#content').html('<br><br><br><br><br><br><br><br><br><br><br><br><img src="loader.gif">');
            $.ajax({
                url: "profile.php",
                method:"POST",
                success: function(result)
                {
                    $("#profile_nav").css({"background-color":"#4CAF50"});
                    $("#nofication_nav").removeAttr("style");
                    $("#chat_nav").removeAttr("style");
                    $("#forum_nav").removeAttr("style");
                    $("#post_nav").removeAttr("style");
                    $("#content").html(result);
                }
            });
        });
        ////////////////////////////////////////////////////////////
        $("#nofication_nav").click(function(){
            $.ajax({
                url: "nofication.php",
                method:"POST",
                success: function(result)
                {
                    $("#nofication_nav").css({"background-color":"#CCCC00"});
                    $("#profile_nav").removeAttr("style");
                    $("#chat_nav").removeAttr("style");
                    $("#forum_nav").removeAttr("style");
                    $("#post_nav").removeAttr("style");
                    $("#content").html(result);
                }
            });
        });
        ///////////////////////////////////////////////////////////
        $("#chat_nav").click(function(){
            $.ajax({
                url: "chat.php",
                method:"POST",
                success: function(result)
                {
                    $("#chat_nav").css({"background-color":"#008CBA"});
                    $("#nofication_nav").removeAttr("style");
                    $("#profile_nav").removeAttr("style");
                    $("#forum_nav").removeAttr("style");
                    $("#post_nav").removeAttr("style");
                    $("#content").html(result);
                }
            });
        });
        ///////////////////////////////////////////////////////
        $("#forum_nav").click(function(){
            $.ajax({
                url: "forum.php",
                method:"POST",
                success: function(result)
                {
                    $("#forum_nav").css({"background-color":"#FF00FF"});
                    $("#nofication_nav").removeAttr("style");
                    $("#chat_nav").removeAttr("style");
                    $("#profile_nav").removeAttr("style");
                    $("#post_nav").removeAttr("style");
                    $("#content").html(result);
                }
            });
        });
        ///////////////////////////////////////////////////////
        $("#post_nav").click(function(){
            $.ajax({
                url: "post.php",
                method:"POST",
                success: function(result)
                {
                    $("#post_nav").css({"background-color":"#F44336"});
                    $("#nofication_nav").removeAttr("style");
                    $("#chat_nav").removeAttr("style");
                    $("#forum_nav").removeAttr("style");
                    $("#profile_nav").removeAttr("style");
                    $("#content").html(result);
                }
            });
        });
    });
</script>
</body>
</html>

Также я удалил ваш fetch_data() вызов, он уже вызывается в функции setInterval, нет необходимости вызывать его снова в вашей ready функции.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...