【问题标题】:Show PHP warning message when adding methods to ajax jQuery向 ajax jQuery 添加方法时显示 PHP 警告消息
【发布时间】:2018-08-29 09:55:09
【问题描述】:

我是 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()方法和其他额外方法时,它显示警告消息,我不知道为什么显示此警告,我认为我在语法声明中犯了错误,请帮我解决这个

【问题讨论】:

    标签: php jquery html ajax debugging


    【解决方案1】:

    我测试了您的代码。它似乎正在工作。

    我发现的唯一错误是两行中的 .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 函数上再次调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      相关资源
      最近更新 更多