Javascript 如何检查服务器是否返回未定义,然后忽略它

Javascript 如何检查服务器是否返回未定义,然后忽略它,javascript,php,jquery,Javascript,Php,Jquery,我从一个页面收到许多动态post id,一个php服务器端页面(server.php)使用这些id进行查询,以查找mysql中新添加的数据 如果在mysql中没有发现任何新添加的数据,它将返回一个未定义的值。因此,根据我的脚本,它会以一个时间间隔一个接一个地追加一个未定义的 那个么,若php查询在sql中找不到任何内容,那个么如何检查,然后退出,不返回任何内容 我在我的phpif(mysqli_num_rows($res)){//do something}中尝试了这一点,但它也显示为未定义 我的

我从一个页面收到许多动态post id,一个php服务器端页面(server.php)使用这些id进行查询,以查找mysql中新添加的数据

如果在mysql中没有发现任何新添加的数据,它将返回一个
未定义的值。因此,根据我的脚本,它会以一个时间间隔一个接一个地追加一个
未定义的

那个么,若php查询在sql中找不到任何内容,那个么如何检查,然后退出,不返回任何内容

我在我的php
if(mysqli_num_rows($res)){//do something}
中尝试了这一点,但它也显示为未定义

我的javascript:

var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});

function addrep(type, msg){
CID.forEach(function(id){
    $("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
});
}

function waitForRep(){
    $.ajax({
        type: "GET",
        url: "server.php",
        cache: false,
        data: {CID : CID},
        timeout:15000, 
        success: function(data){ 
            addrep("postreply", data);
            setTimeout(waitForRep, 15000 );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(waitForRep, 15000); }
    });
}

$(document).ready(function(){
    waitForRep();
});
我在我的php
if(mysqli_num_rows($res)){//do something}
中尝试了这个方法,但它也显示
未定义的

var timer; // declare the timer here
var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i) {
  CID[i] = $(this).data('post-id');
});

function addrep(type, msg) {
  CID.forEach(function(id) {
    if(msg.id === undefined || msg.details === undefined){
        clearTimeout(timer); // cleartimeout timer
    }else{
        $("#newreply" + id).append("<div class='" + type + "" + msg.id + "'><ul><div class='cdomment_text'>" + msg.detail + "</ul></div>");
    }
  });
}

function waitForRep() {
  $.ajax({
    type: "GET",
    url: "server.php",
    cache: false,
    data: {
      CID: CID
    },
    timeout: 15000,
    success: function(data) {
      addrep("postreply", data);
      timer = setTimeout(waitForRep, 15000); // assign the settimeout to timer
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      setTimeout(waitForRep, 15000);
    }
  });
}

$(document).ready(function() {
  waitForRep();
});
我想这应该是因为您每15000
ms通过ajax使用
setTimeout
调用一次php代码

var timer; // declare the timer here
var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i) {
  CID[i] = $(this).data('post-id');
});

function addrep(type, msg) {
  CID.forEach(function(id) {
    if(msg.id === undefined || msg.details === undefined){
        clearTimeout(timer); // cleartimeout timer
    }else{
        $("#newreply" + id).append("<div class='" + type + "" + msg.id + "'><ul><div class='cdomment_text'>" + msg.detail + "</ul></div>");
    }
  });
}

function waitForRep() {
  $.ajax({
    type: "GET",
    url: "server.php",
    cache: false,
    data: {
      CID: CID
    },
    timeout: 15000,
    success: function(data) {
      addrep("postreply", data);
      timer = setTimeout(waitForRep, 15000); // assign the settimeout to timer
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      setTimeout(waitForRep, 15000);
    }
  });
}

$(document).ready(function() {
  waitForRep();
});
因此,您可以使用
addrep()
函数中的js代码忽略它,而不是停止它

    function addrep(type, msg) {
      CID.forEach(function(id) {
        if (msg.id !== undefined && msg.detail !== undefined) { // <--check undefined here
          $("#newreply" + id).append("<div class='" + type + "" + msg.id + "'>"+
                                     "<ul><div class='cdomment_text'>" + msg.detail +
                                     "</ul></div>");
        }
      });
    }

谢谢你,先生。第一个一站未定义值,但它也只能获取第一个CID的实际值。第二个我正在尝试。