$data
$data : mixed
原始数据
Json Response
$data : mixed
原始数据
$contentType : string
当前contentType
$charset : string
字符集
$code : int
状态码
$allowCache : bool
是否允许请求缓存
$options : array
输出参数
$header : array
header参数
$content : string
输出内容
$cookie : \think\Cookie
Cookie对象
$session : \think\Session
Session对象
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\response;
use think\Cookie;
use think\Response;
/**
* Json Response
*/
class Json extends Response
{
// 输出参数
protected $options = [
'json_encode_param' => JSON_UNESCAPED_UNICODE,
];
protected $contentType = 'application/json';
public function __construct(Cookie $cookie, $data = '', int $code = 200)
{
$this->init($data, $code);
$this->cookie = $cookie;
}
/**
* 处理数据
* @access protected
* @param mixed $data 要处理的数据
* @return string
* @throws \Exception
*/
protected function output($data): string
{
try {
// 返回JSON数据格式到客户端 包含状态信息
$data = json_encode($data, $this->options['json_encode_param']);
if (false === $data) {
throw new \InvalidArgumentException(json_last_error_msg());
}
return $data;
} catch (\Exception $e) {
if ($e->getPrevious()) {
throw $e->getPrevious();
}
throw $e;
}
}
}