微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CakePHP Jquery.ajax()

好吧,我是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);
                   },
               })
           });
    })

解决方法:

你得到错误的原因总是因为你永远不会从你必须做一个json回声的动作返回一个响应,例如,你只是设置数据就是这样.

您还应该在控制器方法中进行某种验证,如果具有提供的ID的模板不存在会发生什么?您将收到错误并且没有处理它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐