2016-09-01 7 views
1

나는 평범한 구식 JSON을 반환하려고하지만 어떤 이유로이 같은 반환됩니다 : 나는 현재 응답을 보내 CakePHP를 사용하고CakePhp는 json과 함께 HTML 태그와 BODY 태그를 반환하고 있습니까?

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

가 :

/** 
* @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

'echo json_encode ($ this-> messages)의 결과는 무엇입니까? die();'? – Sherif

+0

똑같은 것. @Sherif – jrquick

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

참조하십시오. Sending correct JSON content type for CakePHP