http.post请求在ZF3和Angular.js中没有可用的响应数据

时间:2017-09-22 15:28:56

标签: php angularjs zend-framework3

我正在使用ZF3和Angular JS版本1.6.7进行开发,我遇到了问题。如果我直接调用网址,我可以正确看到响应,就像您在下一张图片中看到的那样:

enter image description here

json是从我的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,
        ]);                                                        
    }          

}

对方法控制器的调用是通过以下方式完成的:

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

我打电话的服务是:

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;     
    };
}]);

我收到的消息是“此请求没有可用的响应数据”

我做错了什么?

enter image description here

此方法从:

调用

编辑I:

如果我查阅帖子请求的状态和结果,我会得到下一个值:

    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;     
    };

编辑II:

问题可能是我的回复中的Content-Type是“text / html”而不是“application / json”?

标头请求:

enter image description here

标题回复:

enter image description here

我不知道为什么我会在Content-Type“text / html”中收到我的回复,因为在模块配置的ZF3中我定义了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',
    ],           
],

1 个答案:

答案 0 :(得分:0)

你正在通过浏览器向JS做一个POST请求,而不是GET。