Beispiel #1
0
 /**
  * 返回数据到客户端
  * @access protected
  * @param mixed $data 要返回的数据
  * @param String $type 返回数据格式
  * @param bool $exit 是否终止执行
  * @return void
  */
 public static function returnData($data, $type = '', $exit = true)
 {
     $headers = ['json' => 'application/json', 'xml' => 'text/xml', 'html' => 'text/html', 'jsonp' => 'application/javascript', 'script' => 'application/javascript', 'text' => 'text/plain'];
     $type = strtolower($type);
     if (isset($headers[$type])) {
         header('Content-Type:' . $headers[$type] . '; charset=utf-8');
     }
     switch ($type) {
         case 'json':
             // 返回JSON数据格式到客户端 包含状态信息
             $data = json_encode($data, JSON_UNESCAPED_UNICODE);
             break;
         case 'xml':
             // 返回xml格式数据
             $data = \org\Transform::xmlEncode($data);
             break;
         case 'jsonp':
             // 返回JSON数据格式到客户端 包含状态信息
             $handler = isset($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler');
             $data = $handler . '(' . Transform::jsonEncode($data) . ');';
             break;
     }
     if ($exit) {
         exit($data);
     } else {
         echo $data;
     }
 }