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.

7
  • Looks like you're returning text. Have you tried setting the proper content header? Commented Jun 24, 2011 at 7:07
  • @SandeepanNath nope, I meant the content type header. Have a look at this question : stackoverflow.com/questions/267546/…. Have that before you echo it. Commented Jun 24, 2011 at 7:32
  • @JohnP - added header('Content-type: application/json'); ... still same results Commented Jun 24, 2011 at 8:17
  • @SandeepanNath try using the 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. Commented Jun 24, 2011 at 8:34
  • @JohnP, it worked! you can put this in the answer... Commented Jun 24, 2011 at 10:18

2 Answers 2

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

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

Comments

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");

1 Comment

yes... mentioning null for data param explicitly solves the problem. Thanks @Rob.

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.