Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp, jquery, .ajax(), dataType: json

is it possible to be able to do without a view for a cakephp controller function? i am trying to have my server return a datatype that is not a string - but an array

my controller function :


    function test() {
      $this->layout = 'plain';
      $task['Numbers']['uno'] = 'mooo';
      $task['Numbers']['dos'] = 'says the cow';
      $result = json_encode($task);
      $this->set('result', $result);
    }

my view file test.ctp



    echo $result;


my jquery:



    $('#test').live('click', test);

    function test() {

        var data = $('#form').serialize();

        $.ajax({
        type: "post",
        url: "/controller/test",
        data: data,
        dataType: 'json',
        success: function(response){            
        alert(response.Numbers.uno);
        }
        });

    }

clicking on the html element marked test doesn't give me anything. however if i take out

dataType: 'json', 

and change
alert(response.Numbers.uno);
to
alert(response);

in my jquery - i get an alert: the json encoded data but as a string (
alert(typeof response);
)

does anyone have any idea what might be happening?


2 Answers

First, to answer your question, yes you can do without a view (and this is a good case for doing so). In your controller action (test, in this case), just set $this->autoRender = false;. That's what I usually do and just echo out the encoded JSON string in the controller itself. Less clutter that way.

The next thing that I'd look at are your debug settings in config.php. If not set to 0, there's a very good chance that this is your problem. Your echo statement may be echoing the JSON-formatted string, but appending debug output to it. In the method, just include Configure::write ( 'debug', 0 ). This will disable debug only for the current request. I usually do this in my AppController actually:

if ( $this->RequestHandler->isAjax() ) {
   Configure::write ( 'debug', 0 );
}

I'd engage a tool like Firebug and see what's happening to your ajax request. It will monitor those requests and provide the request and response headers for you to inspect. That may help. If the latter is the problem, then Firebug would have shown you that in the response headers.

like image 110
Rob Wilkerson Avatar answered Mar 21 '26 15:03

Rob Wilkerson


I agree with Rob, except with setting your debug in the app controller. It gets a little annoying to me because you have to set the debug level in the app controller every time you want to debug something for a specific function. I write my handler out like this in the controller i am working in.

Configure::write('debug', 0);
$this->autoRender = false;

Also I agree with the use of firebug. With firebug all you have to do is open firebug, click on console, and then run your test. This will tell you what you are doing with your jquery and give you the results.

like image 23
Stirling Avatar answered Mar 21 '26 14:03

Stirling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!