Javascript 如何从另一个php文件中获取php文件的结果
我正在使用一个长轮询脚本,其中我希望将结果显示到user.php页面updateapst div中,该div使用Ajax从update.php页面echo获得结果 请查看我的代码以了解我的问题 在这里,如果我使用下面的任何文本进行测试,它的效果很好Javascript 如何从另一个php文件中获取php文件的结果,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用一个长轮询脚本,其中我希望将结果显示到user.php页面updateapst div中,该div使用Ajax从update.php页面echo获得结果 请查看我的代码以了解我的问题 在这里,如果我使用下面的任何文本进行测试,它的效果很好 success: function(data){ $("#updatetime").append("Test working"); 但我想在这里附加我的update.php页面echo“” 我在user.php页面上使用的脚本 f
success: function(data){
$("#updatetime").append("Test working");
但我想在这里附加我的update.php页面echo“”
我在user.php页面上使用的脚本
function waitForMsg(){
$.ajax({
type: "GET",
url: "update.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
/* Here above I want to append update.php page echo */
$("#updatepost").append(data.responseText);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
}
$(document).ready(function () {
waitForMsg();
});
要显示结果的My user.php页面
<div id="updatepost"> result will goes here from update.php page echo''; </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)) {
$from_id = $row['from_id'];
$parent_id = $row['parent_id'];
$to_id = $row['to_id'];
$sub = $row['sub'];
$detail = $row['detail'];
echo '<div class="upbox" id="'.$parent_id.'">
// All result of above query
</div>';
}
若要从服务器追加数据,必须返回当前结果。在您的示例中,您返回文本,但在js中,您尝试使用此文本-这是您的问题。这是一个例子: user.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)) {
$result['from_id'] = $row['from_id'];
$result['parent_id'] = $row['parent_id'];
......
....
header('Content-Type: application/json');
echo json_encode($result);
exit;
}
你的成功方法呢
success: function(data){
if(data) {
//create your html structs as you access data : data.from_id or data.parent_id
}
},
查看success函数的数据参数。这包含来自服务器的答案。请帮助我访问我不清楚的数据。如果我使用$updatetime.append.html'+data.sub+,它是否像$updatetime.append.html'data.sub,data.detail,data.from_id,data.parent_id';它的显示函数sub{[本机代码]}先生,请帮我解决它。无法在此处使用JS htmlAre u访问数据。