3 个回答

报错信息呢?还有你这个ThinkPHP的版本是3还是5?

这里我假如你是TP5,还有如果你是来充当api调用返回数据的话,你很有可能没有指定 $code = 1, 下面是调用的正确方式(参数要按照顺序):

$this->result($data, $code = 0, $msg = '', $type = '', array $header = [])

下面是TP5的result源码,你可以对比看一下:

    /**
     * 返回封装后的API数据到客户端
     * @access protected
     * @param mixed     $data 要返回的数据
     * @param integer   $code 返回的code
     * @param mixed     $msg 提示信息
     * @param string    $type 返回数据格式
     * @param array     $header 发送的Header信息
     * @return void
     */
    protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => $_SERVER['REQUEST_TIME'],
            'data' => $data,
        ];
        $type     = $type ?: $this->getResponseType();
        $response = Response::create($result, $type)->header($header);
        throw new HttpResponseException($response);
    }
新手上路,请多包涵

不管怎么传参,都是报错,类型错误,array 和 字符串都不行

新手上路,请多包涵

默认返回类型不是json的话,要自己传第四个参数

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进