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.