header(); $this->init(); } protected function header(){ header('Access-Control-Allow-Origin:*'); header('Content-type: application/json;chartset=uft-8'); if( $_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ exit; } } protected function init(){ } public function output($code,$msg,$data = [],$count = 0){ try { // 返回JSON数据格式到客户端 包含状态信息 $count = $count == 0?count($data):$count; $data = json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data,'count'=>$count], JSON_UNESCAPED_UNICODE); if ($data === false) { throw new \InvalidArgumentException(json_last_error_msg()); } header('Content-type: application/json'); echo $data;exit(); } catch (\Exception $e) { if ($e->getPrevious()) { throw $e->getPrevious(); } throw $e; } } }