1

My problem is somewhat similar to cakephp, jquery, .ajax(), dataType: json, but my observations are little different.

I am working on a Cake PHP project. Consider a group_assoc submodule of opstools module. So, there is this function group_assoc() inside opstools_controller.php which is invoked by an ajax call to update group associations.

My ajax post is like this -

$.post( url,
function(data) 
{
   if(data)
   {

      alert(data.success);  //alerts -> undefined
      alert(data);          //alerts -> {"success":true}  or {"success":false}

      if(data.success)
      {
         //does not work
      }
  }

}, "json");

And inside the opstools_controller.php I have -

function group_assoc()
{
     ...
     ...
     //some code
     ...
     ...
     $success //contains true or false - depending on previous logic

     echo json_encode(array("success" => $success));
}

So, inside the Ajax response handler function (in the ajax posting part), I am getting a string like {"success":false}.

How do I fix this problem. I remember using similar Ajax posting and responding using json_encode, which worked perfectly fine in a previous project with Core PHP (no Cake PHP). What could be the problem here? Any pointers?

Update
Do I need to explicitly set any header? Why would that be needed? Where to check if it is set that we are returning text. I tried putting header("HTTP/1.1 200 OK"); before the echoing part, as is done in the existing code - similar Ajax handler functions.

Also, I have set $this->autoRender = false; in my module.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • Looks like you're returning text. Have you tried setting the proper content header? – JohnP Jun 24 '11 at 07:07
  • @SandeepanNath nope, I meant the content type header. Have a look at this question : http://stackoverflow.com/questions/267546/correct-http-header-for-json-file. Have that before you echo it. – JohnP Jun 24 '11 at 07:32
  • @JohnP - added `header('Content-type: application/json');` ... still same results – Sandeepan Nath Jun 24 '11 at 08:17
  • @SandeepanNath try using the [getJSON()](http://api.jquery.com/jQuery.getJSON/) method. Somehow your json seems to be getting treated as text rather than json. Can be easily solved by calling `eval` on it, but I'd rather avoid it. – JohnP Jun 24 '11 at 08:34
  • @JohnP, it worked! you can put this in the answer... – Sandeepan Nath Jun 24 '11 at 10:18
  • @SandeepanNath cool, posted :) – JohnP Jun 24 '11 at 10:33
  • Please see http://stackoverflow.com/questions/4975196/is-this-an-acceptable-ajax-action-for-a-cakephp-auto-complete , it contains an answer tha tells you have to setup a .json extension for views. – Dunhamzzz Jun 24 '11 at 11:17

2 Answers2

2

Make sure you're sending the correct header type

header('Content-type: application/json');

Alternatively you can use jQuery's getJSON() method to handle JSON

JohnP
  • 49,507
  • 13
  • 108
  • 140
1

It looks to me like you might be missing an argument to the $.post() API:

.post( url, [data,] [success(data, textStatus, jqXHR),] [dataType] )

Specifically, the data argument. The API's example seems to indicate that it's smart enough to figure out when it's missing (the argument doesn't appear in the API example), but it may be worth adding it explicitly:

$.post( url, null,
  function(data) 
  {
    if(data)
    {

       alert(data.success);  //alerts -> undefined
       alert(data);          //alerts -> {"success":true}  or {"success":false}

    if(data.success)
    {
      //does not work
    }
  }

}, "json");
Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192