CakePhp将返回HTML和BODY标签以及json


CakePhp is returning HTML and BODY tags along with the json?

我试图返回普通的旧JSON,但由于某种原因,它被返回如下:

<html>
   <body>
       {
          "token":"MTEyLSQyeSQxMCRHVS9nS2t2QVRVcGpJWjJGVERldXouWWJFTzgyZ0lCTURBZFIvdWs2RldGNm1IeWxxNGpTUw==",
          "user":{
              "id":112,
              "username":"admin",
              "firstName":"admin",
              "lastName":"admin",
           },
           "userType":{
              "id":1,
              "name":"admin"
           }
       }
   </body>
</html>

我正在使用CakePHP发送响应:

/**
 * @param $controller 'App'Controller'AppController
 */
public function respond($controller) {
    $controller->response->header('Content-Type: application/json');
    $controller->response->statusCode($this->statusCode);
    $controller->response->body(json_encode($this->messages));
}

但我也试过使用普通PHP:

echo json_encode($this->messages);
die();

HTML标签不是我前端的问题,它们似乎被javascript忽略了。但是由于某些原因,TestNG正在获取HTML标签并使响应不可解析。

任何想法?

使用以下代码获取Json响应:

public function respond($controller) {
    $controller->autoRender = false;
    $this->response->type('json');
    $controller->response->statusCode($this->statusCode);
    $controller->response->body(json_encode($this->messages));
}

Ref。为CakePHP发送正确的JSON内容类型