【发布时间】:2011-11-08 05:31:35
【问题描述】:
嗯,我是 CakePHP 的新手。所以调试这个是痛苦的一天。这是我的代码:
templates_controller.php
function reajax($id = NULL) {
$this->layout = false;
$this->Template->id = $id;
$template = $this->Template->read();
$this->set('result', $template['Template']['content']);
}
reajax.ctp
echo $result;
js 文件
$(document).ready(function() {
$(".abcd").click(function(event){
event.preventDefault();
var id = this.id;
$.ajax({
type:"GET",
url:"/templates/reajax/" + id,
success : function(data) {
alert('success');
$("textarea").text(data);
},
error : function() {
alert(id);
},
})
});
})
点击文件
<ul class="content-box-tabs">
<?php echo $html->link($html->image('thumbnails/'.$template['Template']['thumbnail'], array('alt' => 'test', 'height' => '120', 'width' => '110')), array('controller' => 'templates', 'action' => 'reajax'), array('class' => 'abcd', 'id' => $template['Template']['id'], 'escape' => false))?>
</ul>
每次我得到一个错误结果,我都不知道我的代码有什么问题。谁能帮我?提前致谢。
当我编辑下面的 JS 文件时,一切都很顺利。我不知道这是 CakePHP 的错误还是我的代码有其他问题。我需要一个蜘蛛侠!
$(document).ready(function() {
$(".abcd").click(function(event){
event.preventDefault();
var id = this.id;
$.ajax({
type:"GET",
url:"/cakephp/templates/reajax/" + id,
//url: "/templates/reajax/" + id,
success : function(data) {
alert('success');
$("textarea").text(data);
},
error : function() {
alert(id);
},
})
});
})
【问题讨论】:
-
您是否使用 Firebug 检查过错误结果? ajax 响应的内容
-
我已经成功了。但是在 js 文件中有一个有趣的代码。检查编辑的帖子。谢谢!
-
您可能应该对结果进行 json_encode。