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

时间:2022-03-14 12:13:18

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:

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

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

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

json是从我的IndexController的这个方法发送的:

IndexController.php

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:

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?

我究竟做错了什么?

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

This method is called from:

从以下方法调用此方法:

Edit I:

编辑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:

编辑II:

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

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

Headers request:

标题要求:

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

Headers response:

标题回复:

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

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.

我不知道为什么我在响应中收到Content-Type“text / html”,因为在我的模块配置中的ZF3中我定义了ViewJsonStrategy。

module.config.php

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 个解决方案

#1


0  

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

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

#1


0  

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

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