5

Well, I am new to CakePHP. So a painful day to debug this. Here are 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);
               },
           })
       });
})

The 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 a error result, and I have no idea what's wrong with my code. Can anyone help me? Thanks in advance.

Everything is going well when I edit the JS file below. I don't know if this is a bug of CakePHP or if there is something else wrong with my code. I need a spider man!

$(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);
                   },
               })
           });
    })
3
  • HAve you checked the error result with Firebug? The content of the ajax response Commented Sep 1, 2011 at 14:16
  • I have make it successful. But with a funny code in js file. Check the edited post. Thank you! Commented Sep 2, 2011 at 1:31
  • You should probably json_encode you results. Commented Sep 2, 2011 at 2:05

4 Answers 4

5

The reason you get an error always is because you are never returning a response from the action you have to do an echo of a json for instance, you are simply setting the data and that's it.

You should also have some kind of validation in your controller method, what happens if the template with the provided ID does not exist? You will get an error and are not handling it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your advice. I will make a strong code in the controller and the jaso data type return. Cheers!
2

not sure about this string

$this->layout = false;

create new empty layout ajax.ctp like this

<?=$content_for_layout?>

and try to use it

$this->layout = 'ajax';

and.... you can try to use this way for ajax request

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

3 Comments

i'm added some example about ajax query, this is the way i do usualy, and by the way, did you use some debugger?
Sorry, I just use Firebug. In fact, i think there is something wrong in the ajax url. I even can not get param 'id' in PHP file.
Finally I make it success, but I have no idea why. Check the edited post. Thank you all the same!
0

1.$this->autoRender = false; or $this->viewBuilder->layout('ajax');(for Cakephp 3.0 & make a ajax.ctp inside layout folder) ajax.ctp should look like

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

You don't need to make the .cpt for ajax function because ajaxFunction could return some value not the html.

Then you can check it's ajax request or not .(It is not essential for best practices)

if ($this->request->is('ajax')) {
    // do your logic here
}
  1. you should put echo $result inside the reajax function.

Now your code should look like this

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

It will help you.

Comments

-1

first of all you should echo your result then exit your function or make it autoRender = false; for debuging you should use developers tool.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.