I am using cakephp to develop a webpage that dynamically append data retrieved from server. The appended data can be turned in jquery accordions (not shown).
我正在使用cakephp开发一个动态附加从服务器检索的数据的网页。附加的数据可以用jquery手风琴转动(未示出)。
I've tried some suggestions as seen in other topics, but still nothing shows up on the webpage.
我已尝试过其他主题中的一些建议,但网页上仍然没有显示任何内容。
The javascript is as such:
javascript是这样的:
<script type="text/javascript">
$.getJSON('viewmjcatjson.ctp', function(data){
var content = '<div id="majorcat">\n';
$.each(data, function(index,cat){
content += '<h2><a href="#">' + cat.category + '</a></h2><div>\n';
content += cat.description + '</div>\n';
});
$("#majorcat").append(content);
});
</script>
The 'viewmjcatjson.ctp' is as such:
'viewmjcatjson.ctp'是这样的:
<?php
Configure::write('debug', 0);
echo json_encode($majorCategories);
?>
The json object returned is valid (checked with jsonlint)
返回的json对象有效(使用jsonlint检查)
[{
"MajorCategories":{
"category":"Corporate Governance",
"description":"description on Corporate Governance"
}
},
{
"MajorCategories":{
"category":"Earnings Report",
"description":"description on Earnings Report"
}
},
{
"MajorCategories":{
"category":"Equity",
"description":"Relating to stock"
}
},
{
"MajorCategories":{
"category":"Financial Issue",
"description":"The area of finance dealing with monetary decisions that business enterprises make and the tools and analysis used to make these decisions"
}
}
]
I am suspecting something is wrong in the each loop, but I am not sure.
我怀疑每个循环中有什么问题,但我不确定。
EDIT The entire php file containing the javascript.
编辑包含javascript的整个php文件。
<?php echo $html->script('jquery-1.5.1.min.js'); ?>
<?php echo $html->script('jquery-ui-1.8.13.custom.min.js'); ?>
<?php echo $html->css('ui-lightness/jquery-ui-1.8.13.custom.css'); ?>
<script type="text/javascript" src="development-bundle/jquery-1.3.2.js"></script>
<script type="text/javascript" src="development-bundle/ui/ui.core.js"></script>
<script type="text/javascript" src="development-bundle/ui/ui.accordion.js"></script>
<div class="users form">
<h1><?php echo $article['Article']['title']?></h1>
<p><small>Author: <?php echo $article['Article']['author_all']?></small></p>
<p><?php echo $article['Article']['full_text']?></p>
<br>
<hr>
<br>
<table>
<tr>
<td width="60%">
<div id="majorcat">
</div>
</td>
<td width="40%">
<div id="minorcat">
</div>
</td>
</tr>
</table>
</div>
<div class="actions">
<h3><?php __('Menu'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('Logout', true),
array('controller' => 'users', 'action' => 'logout'));?></li>
</ul>
</div>
<script type="text/javascript">
$(function(){
$.getJSON('viewmjcatjson.ctp', function(data){
var content = '<div id="majorcat">\n';
$.each(data, function(index,cat){
content += '<h2><a href="#">' + cat.category + '</a></h2><div>\n';
content += cat.description + '</div>\n';
});
$("#majorcat").append(content);
});
})
</script>
2 个解决方案
#1
1
<script type="text/javascript">
$(function(){
$.getJSON('viewmjcatjson.ctp', function(data){
var content = '<div>';
$.each(data, function(index,cat){
content += '<h2><a href="#">' + cat.MajorCategories.category + '</a></h2> <div>';
content += cat.MajorCategories.description + '</div>';
});
$("#majorcat").append(content);
});
})
</script>
You're missing the document.ready
你错过了document.ready
#2
0
I noticed Firebug console showing that 'viewmjcatjson' is responding not with json-encoded data but with a HTML file. The contents is actually exactly the same as the PHP file.
我注意到Firebug控制台显示“viewmjcatjson”不响应json编码的数据,而是使用HTML文件。内容实际上与PHP文件完全相同。
So I deduced it could be a routing problem. Changing to...
所以我推断它可能是一个路由问题。改为......
$.getJSON('../viewmjcatjson', function(data){
...solves the problem.
......解决了这个问题。
Thanks Interstellar_Coder for the fixed code though.
感谢Interstellar_Coder的固定代码。
#1
1
<script type="text/javascript">
$(function(){
$.getJSON('viewmjcatjson.ctp', function(data){
var content = '<div>';
$.each(data, function(index,cat){
content += '<h2><a href="#">' + cat.MajorCategories.category + '</a></h2> <div>';
content += cat.MajorCategories.description + '</div>';
});
$("#majorcat").append(content);
});
})
</script>
You're missing the document.ready
你错过了document.ready
#2
0
I noticed Firebug console showing that 'viewmjcatjson' is responding not with json-encoded data but with a HTML file. The contents is actually exactly the same as the PHP file.
我注意到Firebug控制台显示“viewmjcatjson”不响应json编码的数据,而是使用HTML文件。内容实际上与PHP文件完全相同。
So I deduced it could be a routing problem. Changing to...
所以我推断它可能是一个路由问题。改为......
$.getJSON('../viewmjcatjson', function(data){
...solves the problem.
......解决了这个问题。
Thanks Interstellar_Coder for the fixed code though.
感谢Interstellar_Coder的固定代码。