thinkphp使用pjax

2023-04-13 14:58:21 阅读:3 编辑

thinkphp/library/think/response/Json.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think\response;

use think\Response;

class Json extends Response
{
    // 输出参数
    protected $options = [
        'json_encode_param' => JSON_UNESCAPED_UNICODE,
    ];

    protected $contentType = 'application/json';

    /**
     * 处理数据
     * @access protected
     * @param mixed $data 要处理的数据
     * @return mixed
     * @throws \Exception
     */
    protected function output($data)
    {
       /* return $data;
      // print_r($_SERVER);
      // print_r($_REQUEST);

        if(isset($_REQUEST["_pjax"])){
            $this->contentType("text/html", $this->charset);
            $header = [
                //"X-PJAX-URL"=>$_SERVER['REQUEST_URI'],
                //"X-PJAX-CONTAINER"=>''
            ];
            $this->header = array_merge($this->header, $header);
            //$this->contentType = "text/html";
            return $this->content;
        }*/

        try {
            /*
            if(isset($_REQUEST["_pjax"])){
                $contentType ="text/html";
                $charset = $this->charset;
                $header = [];
                $header['Content-Type'] = $contentType . '; charset=' . $charset;
              //  $header['x-pjax'] = "true";
              //  $header['x-pjax-container'] = "#pjax-container";
                $header['x-pjax-url'] =$_SERVER['REQUEST_URI'];
                //$header['x-pjax'] = "true";
                $this->header = $header;
                //$this->contentType("text/html", $this->charset);
                return  $this->data;
            }
            */
            // 返回JSON数据格式到客户端 包含状态信息
            $data = json_encode($data, $this->options['json_encode_param']);

            if ($data === false) {
                throw new \InvalidArgumentException(json_last_error_msg());
            }

            return $data;
        } catch (\Exception $e) {
            if ($e->getPrevious()) {
                throw $e->getPrevious();
            }
            throw $e;
        }
    }

}