CakePHP Jquery.ajax()

Rui*_*ant 5 php ajax jquery cakephp

好吧,我是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']);
    }
Run Code Online (Sandbox Code Playgroud)

reajax.ctp

echo $result;
Run Code Online (Sandbox Code Playgroud)

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);
               },
           })
       });
})
Run Code Online (Sandbox Code Playgroud)

点击文件

    <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>
Run Code Online (Sandbox Code Playgroud)

每次我收到错误结果,我都不知道我的代码有什么问题.谁能帮我?提前致谢.

当我编辑下面的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);
                   },
               })
           });
    })
Run Code Online (Sandbox Code Playgroud)

8vi*_*ius 5

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

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