0

I'm developing with ZF3 and Angular JS version 1.6.7 and I've got a problem. If I call the url directly, I can see the response correctly like you can see in the next image:

enter image description here

The json is send it from this method of my IndexController:

IndexController.php

public function getCompetitionAction(){
    $request = $this->getRequest();        
    $log = new \File\LogWriter();

    $params = json_decode(file_get_contents('php://input'),true);
    if ($params != null){
        $competition = new Competition($params["idLeague"], $params["idCompetition"]);                
        return new JsonModel([
            "result"    => IndexController::RESULT_OK,
            "name"      =>  $competition->getName()
        ]);                                                        
    }else{
        $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": No se han recibido parámetros");
        return new JsonModel([
            "result"    => IndexController::PARAMS_NOT_EXIST,
        ]);                                                        
    }          

}

The call to method controller is done it from:

lf1Controller.js:

self.getDataCompetition = function(idLeague, idCompetition){
    var data = {
        idLeague        :  idLeague,
        idCompetition   :  idCompetition
    };
    console.log("Vamos a llamar a statServices.getData");
    statServices.getData(FactoryURL.url[0], data).then(function(response){
        console.log("resultado: " + response.result);
        switch(response.result){
            case 0: //Resultado OK
                console.log("Resultado: " + response.result);
                break;
            case 3: //Error de sistemas
                console.log("lf1Controller::getDataCompetition(): Error de sistemas");
                break;
        }
    });          
};

And the service which I call is:

app.service("statServices", ["$http", "$q", function($http, $q){

    var self = this;

    self.getData = function(url, data){
        var promise = $q.defer();
        $http.post(url, data)
        .success(function(response, status, headers, config){
            console.log("length data: " + response.length);
            promise.resolve(response);
        }).error(function(data){
            //Servidor caído
            console.log("Error en getData: " + data);
        });

        return promise.promise;     
    };
}]);

And the message that I've got is "This request has no response data available"

What am I doing wrong?

enter image description here

This method is called from:

Edit I:

If I consult the status and the resutl of the post request, I've got the next values:

    self.getData = function(url, data){
        var promise = $q.defer();
        $http.post(url, data)
        .then(function(response, status, headers, config){
            console.log("resultado status: " + response.status); => Returns: 200 OK
            console.log("response.result: " + response.result); => Returns: response.result: undefined
            promise.resolve(response);
        },function(data){
            //Servidor caído
            console.log("Error en getData: " + data);
        });

        return promise.promise;     
    };

Edit II:

Is it possible that the problem is that the Content-Type in my response is "text/html" and not "application/json"?

Headers request:

enter image description here

Headers response:

enter image description here

I don't know why I'm receiving in my response like Content-Type "text/html" because in ZF3 in my module configuration I have defined ViewJsonStrategy.

module.config.php

'view_manager' => [
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => [
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'stats/index/index'       => __DIR__ . '/../view/stats/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
    /*
     * Con este array de parámetros permitimos enviar datos y no mostrar vista
     */
    'strategies' => [
        'ViewJsonStrategy',
    ],           
],
halfer
  • 18,701
  • 13
  • 79
  • 158
José Carlos
  • 2,170
  • 7
  • 43
  • 78

1 Answers1

0

You're doing a POST request from the JS opposed to a GET one via a browser.

Te Ko
  • 758
  • 3
  • 13
  • I think it doesn't matter, is an example to show that the server code works fine. When I make the post request this code works fine but I don't know why angular doesn't receive anything :( – José Carlos Sep 22 '17 at 15:38
  • It definitely could matter. You'll have to find out what methods the web api supports. – JC Ford Sep 22 '17 at 15:41
  • The web api supports post requests. The server doce works fine when I make a post request. I write on a log the result and this is correct. Then the unique code to execute is "return new JsonModel(["result" => 1, "name" => "nombre competicion"]) – José Carlos Sep 22 '17 at 15:46
  • Can You log out the whole result object? – Te Ko Sep 22 '17 at 18:03