我正在尝试返回普通的旧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标记并使响应无法解析。
有什么想法吗?
答案 0 :(得分:1)
使用此代码获取Json响应:
public function respond($controller) {
$controller->autoRender = false;
$this->response->type('json');
$controller->response->statusCode($this->statusCode);
$controller->response->body(json_encode($this->messages));
}