3

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?

  • I am using $.post and keeping the ,"json" in the end like this - `$.post( url, function(data) { if(data) { alert("yes"+data.success); alert(data); } }, "json");` I get undefined in the first alert (alerting data.success) and get a string output containing all the echoes in the 2nd case (alerting data) – Sandeepan Nath Jun 22 '11 at 13:30

5 Answers5

3

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.

Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
  • I have a similar problem like OP, I am using $.ajax like I wrote in my comment to OP's question. If I just add this condition `if ( $this->RequestHandler->isAjax() ) { Configure::write ( 'debug', 0 ); }`, no echoes in my controller logic show anything anymore. So, putting an echo inside the if condition also does not echo. Any further ideas to solve my problem? Same as OP's - not able to read json data inside $.post's success handler using alert(data.success), but able to read all echoes using alert(data) – Sandeepan Nath Jun 22 '11 at 13:33
1

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.

Stirling
  • 4,308
  • 3
  • 23
  • 16
0

Check out the cakePHP book on Request Handling:

http://book.cakephp.org/view/174/Request-Handling

CakePHP has some built in functionality to handle controller methods that return json.

PetersenDidIt
  • 25,562
  • 3
  • 67
  • 72
0

You have to transform the data before use it.
See this answer:

jQuery ajax request with json response, how to?

you have to use the

var json = $.parseJSON(response);
alert(json.Numbers.uno);
Community
  • 1
  • 1
Guilherme
  • 1,980
  • 22
  • 23
0

I just use

die(json_encode(array('success' => true, 'result' => $result)));

at the end of the called controller method and it works for me.

Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127