在向 ajax jQuery 添加方法时显示 PHP 警告消息



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

我测试了你的代码。它似乎正在起作用。

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

最新更新