法一:系统配置

    'default_return_type'=>'json'

法二:输出设置

    namespace app\index\controller;
    class Index 
    {
        public function index()
        {
            $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
            // 指定json数据输出
            return json(['data'=>$data,'code'=>1,'message'=>'操作完成']);
        }
    }

或者指定输出XML类型数据:

    namespace app\index\controller;
    class Index 
    {
        public function index()
        {
            $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
            // 指定xml数据输出
            return xml(['data'=>$data,'code'=>1,'message'=>'操作完成']);
        }
    }

核心支持的数据类型包括view、xml、json和jsonp,其他类型的需要自己扩展。

 

相关文章:

  • 2022-12-23
  • 2021-07-04
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-02
  • 2022-12-23
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案