사용자가 적절한 자격 증명없이 REST API에 액세스하려고하면 내 yii 응용 프로그램에서 내 controller.php 파일에 다음과 같은 401 오류가 발생합니다.
throw new UnauthorizedHttpException('Access unavailable without access_token.', 401);
다음 형식으로 오류를 반환합니다.
<?xml version="1.0" encoding="UTF-8"?>
<response>
<name>Unauthorized</name><message>Access unavailable.</message>
<code>401</code>
<status>401</status>
<type>yii\web\UnauthorizedHttpException</type>
</response>
이 오류를 JSON 형식으로 반환하는 방법은 무엇입니까?
{
"name": "Access unavailable.",
"message": "You are not authorized.",
"code": 0,
"status": 401
}
이 리소스에 액세스하는 내 모바일 앱에는 JSON 개체가 필요합니다.
yii2 나머지 컨트롤러는 "수락"HTTP 헤더에 반응합니다. 이 헤더에 "application / json"만있는 경우 나머지 컨트롤러의 응답은 JSON이됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다