从action-CakePHP返回xml数据
我试图返回从xml文件检索到的一些数据,然后将其返回到视图页面 xml文件将在用户启动的某个操作期间更新,然后会弹出一个对话框,显示xml文件的内容。此操作期间将更新xml文件 在我看来,我想显示从读取xml文件的php操作返回的数据 下面是我获取更新的xml文件的操作。这将每秒调用一次以更新视图从action-CakePHP返回xml数据,xml,file,cakephp,get,Xml,File,Cakephp,Get,我试图返回从xml文件检索到的一些数据,然后将其返回到视图页面 xml文件将在用户启动的某个操作期间更新,然后会弹出一个对话框,显示xml文件的内容。此操作期间将更新xml文件 在我看来,我想显示从读取xml文件的php操作返回的数据 下面是我获取更新的xml文件的操作。这将每秒调用一次以更新视图 public function getUpdate() { $myFile = '/srv/www/xml-status/update_status.xml';
public function getUpdate()
{
$myFile = '/srv/www/xml-status/update_status.xml';
$file = fopen($myFile, 'r');
$data = file_get_contents($myFile);
//debug($data);
//print_r($data);
//echo json_encode($data);
//echo $data;
return json_encode($data);
}
xml文件的内容被检索并存储到$data
变量中,但我一直收到一个500内部服务器错误。我尝试了很多不同的方法,但我不知道如何正确返回数据
这是我的视图代码,用于显示信息
<script>
window.onload = timedRefresh(1000);
// Get the updated xml file every second
function timedRefresh(timeoutPeriod)
{
// Do a get request to get the updated contents of the xml file
$.get('/tools/getUpdate', function(data)
{
alert(data);
});
window.setTimeout('timeRefreshed(1000)', 1000);
}
</script>
<?php
if(false != $xml = simplexml_load_file($xmlFile))
{
foreach ($xml->LogEvent->sequence as $temp)
{
echo 'Step: ', $temp['step'], ' ', 'Step Name: ', $temp['stepName'], ' ', 'd1: ', $temp['d1'], ' ',
'd2: ', $temp['d2'], ' ', 'd3: ', $temp['d3'], ' ', 'Status: ', $temp['status'],'<br>';
}
}
else
{
echo '<br>Error: Update status can\'t be displayed<br>';
}
?>
window.onload=timedRefresh(1000);
//每秒获取更新的xml文件
函数timedRefresh(timeoutPeriod)
{
//执行get请求以获取xml文件的更新内容
$.get('/tools/getUpdate',函数(数据)
{
警报(数据);
});
setTimeout('timeRefreshed(1000)'1000);
}
日志事件->序列为$temp)
{
回显“步骤:”、$temp['Step']、“”、'Step Name:'、$temp['stepName']、“”、'd1:'、$temp['d1']、“”、,
“d2:”、$temp['d2']、“,'d3:”、$temp['d3']、“,'Status:”、$temp['Status']、“
”;
}
}
其他的
{
回显“
错误:无法显示更新状态”
;
}
?>
我不知道我做错了什么。如果我可以将数据变量返回到get请求,那么我就可以解析并显示信息,但是我在从php获取数据到javascript get请求时遇到了麻烦。
任何帮助都会很好
谢谢我想你的行动是这样的:
public function getUpdate()
{
$myFile = '/srv/www/xml-status/update_status.xml';
$file = fopen($myFile, 'r');
$data = file_get_contents($myFile);
//debug($data);
//print_r($data);
echo json_encode($data);
exit;
}
或
谢谢你的最新答案。我将用那个做实验。
public function getUpdate()
{
$this->autoRender = false;
$myFile = '/srv/www/xml-status/update_status.xml';
$file = fopen($myFile, 'r');
$data = file_get_contents($myFile);
//debug($data);
//print_r($data);
echo json_encode($data);
}