José Carlos
José Carlos

Reputation: 2922

How to read json request in a ZF2 Controller?

I have made a development with Angular JS in front-end and in back-end with Zend Framework 2. I want to send a json from front-end to back-end using Angular JS and read this json in Zend Framework 2.

Angular JS

    self.updateUser = function(user){
        var  data = JSON.stringify(
            {
                id: $scope.user.id
            }
        );

        var config = {
            headers: {
                 'Content-Type': 'application/json'
            }
        }

        $http.post("/privado/usuario/updateuser", data, config)
         .success(function(data, status, headers, config){
            switch (data.result){
                case 0: //OK
                    console.log("Result OK!!!");
                    break;
                case 1: //No tiene acceso
                    console.log("Resultado NO ACCESS!!!");
                    break;
                case 3: //Error de sistemas
                    console.log("Resultado KO!!!");
                    break;                  
             }
        })
        .error(function(data){
            console.log("Problems with the server!!!");
        });         
    }

On the other side, back-end, in my controller I have the next code:

Controller

   public function updateUserAction(){
        $log = $this->getServiceLocator()->get("Zend/Log");

        $request = $this->getRequest();

        if ($request->isPost()){    

            $post_data = $request->getPost();       
            if ($post_data != null){
                try{
                    $json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
                    return new JsonModel(array(
                            "result"    =>  UsuarioController::RESULT_OK
                    ));
                }catch(\Exception $e){
                    //Error de acceso a BBDD
                    $log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
                    $log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
                    return new JsonModel(array(
                            "result"    =>  UsuarioController::RESULT_SYSTEM_ERROR
                    ));
                }
            }else{
                $log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
                //Error al recibir los datos
                return new JsonModel(array(
                        "result"    =>  UsuarioController::RESULT_SYSTEM_ERROR
                ));
            }
        }
    }   

If I remove from back-end this line of code:

$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);

The code works fine. That is, the front-end makes the call and receive like result "0" writing in the console "Result OK!!!". But If I don't remove that line of code, in front-end I don't receive any result because something in back-end don't work fine.

How can I read the data received from front-end in my controller?

UPDATED 1

I have added more info to send from front-end to back-end:

Angular JS

    self.updateUser = function(user){
    var data = $.param({
        json: JSON.stringify({
            id: $scope.user.id,
            profiles: 2,
            user: "John",
            color: "blue"               
        })          
    });

    var config = {
        headers: {
             'Content-Type': 'application/json'
        }
    }

    $http.post("/privado/usuario/updateuser", data, config)
     .success(function(data, status, headers, config){
        switch (data.result){
            case 0: //OK
                console.log("Result OK!!!");
                break;
            case 1: //No tiene acceso
                console.log("Resultado NO ACCESS!!!");
                break;
            case 3: //Error de sistemas
                console.log("Resultado KO!!!");
                break;                  
         }
    })
    .error(function(data){
        console.log("Problems with the server!!!");
    });         
}

And I have modified the Controller too ...

Controller

   public function updateUserAction(){
    $log = $this->getServiceLocator()->get("Zend/Log");

    $request = $this->getRequest();

    if ($request->isPost()){   

        $post_data = $request->getPost();     
        if ($post_data != null){
            try{
                    $json = json_decode($post_data["data"]);
                    $log->info("Datos recibidos2: " . sizeof($json));
                    $log->info("id: " . $json["id"]);
                return new JsonModel(array(
                        "result"    =>  UsuarioController::RESULT_OK
                ));
            }catch(\Exception $e){
                //Error de acceso a BBDD
                $log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
                $log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
                return new JsonModel(array(
                        "result"    =>  UsuarioController::RESULT_SYSTEM_ERROR
                ));
            }
        }else{
            $log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
            //Error al recibir los datos
            return new JsonModel(array(
                    "result"    =>  UsuarioController::RESULT_SYSTEM_ERROR
            ));
        }
    }
}   

These lines of code:

$log->info("Datos recibidos2: " . sizeof($json));
$log->info("id: " . $json["id"]);

Write me in a file the next result:

2015-11-19T18:23:30+01:00 INFO (6): Datos recibidos2: 0
2015-11-19T18:30:49+01:00 INFO (6): id:

It seems like I don't receive anything ... How can I get the data received?

UPDATED 2

I have modified my var data in front-end and remove config from calling to the back-end:

Angular JS

    self.updateUser = function(user){
    var data = $.param({
            id: $scope.user.id,
            profiles: 2,
            user: "John",
            color: "blue"               
    });

    $http.post("/privado/usuario/updateuser", data)
     .success(function(data, status, headers, config){
        switch (data.result){
            case 0: //OK
                console.log("Result OK!!!");
                break;
            case 1: //No tiene acceso
                console.log("Resultado NO ACCESS!!!");
                break;
            case 3: //Error de sistemas
                console.log("Resultado KO!!!");
                break;                  
         }
    })
    .error(function(data){
        console.log("Problems with the server!!!");
    });         
}

And I have modified the Controller too ...

Controller

   public function updateUserAction(){
    $log = $this->getServiceLocator()->get("Zend/Log");

    $request = $this->getRequest();

    if ($request->isPost()){   

        $post_data = $request->getPost();     
        if ($post_data != null){
            try{
                $log->info("id: " . $_POST["id"]);
                return new JsonModel(array(
                        "result"    =>  UsuarioController::RESULT_OK
                ));
            }catch(\Exception $e){
                //Error de acceso a BBDD
                $log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
                $log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
                return new JsonModel(array(
                        "result"    =>  UsuarioController::RESULT_SYSTEM_ERROR
                ));
            }
        }else{
            $log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
            //Error al recibir los datos
            return new JsonModel(array(
                    "result"    =>  UsuarioController::RESULT_SYSTEM_ERROR
            ));
        }
    }
}   

Sadly, this line of code doesn't return anything.

$log->info("id: " . $_POST["id"]);
2015-11-19T19:24:29+01:00 INFO (6): id:

And I've got this error in the console.log:

enter image description here

And, without changing anything in front-end, if I change this line of code in my controller:

$log->info("id: " . $_POST["id"]);

By this other:

$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);

And add this other line to write the value of "id":

$log->info("id: " . $json["id"]);

It doesnt' work, because I've got an error in the line:

$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);

Because in my file I don't write anything.

UPDATED 3 From my last version of code, if I remove $.param var data would be:

    var data = {
        id: $scope.user.id,
        profiles: 2,
        user: "John",
        color: "blue"               
    };

It doesn't work and I've got the same error in the console than before. I haven't made any change in the controller.

Upvotes: 1

Views: 1956

Answers (1)

Szymon
Szymon

Reputation: 106

You don't need to use:

headers: {
             'Content-Type': 'application/json'
        }

and JSON decoding:

$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);

You can just send data using JSON as you did (or change JSON.stringify function to string in format: {data1:value1, data2:value2 ...}) and get it using normal $_POST (without json_decode).

So in JavaScript you can write:

var data = {
            id: $scope.user.id,
            profiles: 2,
            user: "John",
            color: "blue"                             
    };

And in PHP receive:

$_POST['id'], $_POST['profiles'], $_POST['user'] ....

Upvotes: 1

Related Questions