CakePHP JQuery.ajax ()

Well, I'm new to CakePHP. So a painful day to debug it. Here is my code:

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 file

$(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);
               },
           })
       });
})

Click file

    <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>

Every time I get an error result, and I have no idea what is wrong with my code. Can someone help me? Thanks in advance.

Everything goes well when I edit the JS file below. I don't know if this is CakePHP error or if something is wrong with my code. I need a spiderman!

$(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);
                   },
               })
           });
    })
+5
source share
4 answers

, , , , json, , .

- , , ID ? .

+5

$this->layout = false;

ajax.ctp,

<?=$content_for_layout?>

$this->layout = 'ajax';

.... ajax

$.get('/controller/action/'+Math.random(),{},function(data){
  $('#result').html(data);
});
+2

1. $this->autoRender = false; $this->viewBuilder->layout('ajax'); ( Cakephp 3.0 ajax.ctp ) ajax.ctp

<?php
 echo $this->fetch('content') ;
 ?>

.cpt ajax, ajaxFunction , html.

ajax . ( )

if ($this->request->is('ajax')) {
    // do your logic here
}
  1. echo $ reajax.

:

function reajax($id = NULL) {       
        $this->autoRender= false;
        $result = "Some value";
        echo $result;`enter code here`
    }

.

0
source

First of all, you should repeat your result, then exit your function or make it autoRender = false; for debugging you should use the developer tool.

-1
source

All Articles