It is not possible to correctly read json encoded success / failure statuses sent by the handling of Cake PHP controller functions. Ajax call

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

I am working on a Cake PHP project. Consider the group_assocmodule submodule opstools. So there is a function group_assoc()inside opstools_controller.phpthat is called by an ajax call to update group associations.

My ajax post is as follows:

$.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 opstools_controller.phpI 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 terms of sending ajax), I get the line as {"success":false}.

. , Ajax- json_encode, Core PHP ( Cake PHP). ? ?


? ? , , . header("HTTP/1.1 200 OK"); , Ajax.

, $this->autoRender = false; .

+1
2

, API $.post():

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

, data. API, , , , , ( API), :

$.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

,

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

jQuery getJSON() JSON

+2

Source: https://habr.com/ru/post/1714459/


All Articles