Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用php echo显示长轮询结果_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用php echo显示长轮询结果

Javascript 如何使用php echo显示长轮询结果,javascript,jquery,Javascript,Jquery,我正在使用一个很长的轮询脚本,如果我在测试中添加任何文本,该脚本将运行良好。现在我想显示user.php从data update.php收集的长轮询结果。我在user.php页面使用了轮询脚本 在这里,如果我像下面那样使用测试轮询,那么它工作得很好 $("#updatepost").append("test polling"); 但是我想在这里附加我的update.php文件的echo 我的完整代码 在my user.php中 <script> function addms

我正在使用一个很长的轮询脚本,如果我在测试中添加任何文本,该脚本将运行良好。现在我想显示user.php从data update.php收集的长轮询结果。我在user.php页面使用了轮询脚本

在这里,如果我像下面那样使用测试轮询,那么它工作得很好

    $("#updatepost").append("test polling");
但是我想在这里附加我的update.php文件的echo

我的完整代码 在my user.php中

<script>
function addmsg(type, msg){
    $("#updatepost").append("test polling");
}

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: "update.php",

        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            addmsg(".upbox1", data); /* Add response to a .msg div (with the "new" class)*/
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, /* Try again after.. */
                15000); /* milliseconds (15seconds) */
        }
    });
}
$(document).ready(function () {
    waitForMsg(); /* Start the inital request */
});
</script>

</head>
<body>
<div id="updatepost">
</div>
在my update.php中

    $u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' AND `created` > '".$timestamp."' ORDER BY created DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($u)) {
$parent_id = $row['parent_id'];
$to_id = $row['to_id'];
$sub = $row['sub'];
$detail = $row['detail'];
$time = $row['created'];

// I want to show this result at my polling
echo '<div class="upbox1" id="'.$parent_id.'"><div class="upbox2">'.$from_id.' '.$sub.' '.$to_pname.'</div>
<div class="upbox3">';
if ($detail=="") {echo '';}
else echo''.$detail.;
echo'</div></a><div class="upbox4">'.$time.'</div>
</div>';
}
如果将addmsg函数更改为

function addmsg(type, msg){
    $("#updatepost").append(msg);
}

它会将ajax调用的结果附加到updatepost元素中。

传递给addmsg函数的.upbox1参数是什么?这个函数的两个参数应该是什么意思?对不起,我只是在尝试这个。在这里工作很好:ajax调用和结果的演示url。因此,请检查您的ajax是否返回了您期望的结果..谢谢您,先生,但无法在我的页面中工作。如果我显示php结果,我会在update.php页面中添加:$result['id']=$row['id']$结果['detail']=$row['detail'];标题'Content-Type:application/json';echo json_encode$result;出口如何通过JS显示它?我正在尝试:$updatepost.html''+data.detail+'';