使用json格式输出

 /**
     * json输出
     *
     * @param unknown_type $info
     */
    public function json_out ($info)
    {
        header('Content-type: text/javascript;charset=utf-8');
        echo json_encode($info);
    }

 二、ajax 请求后台时,最后需要返回json格式的状态和信息

class Index{
    
    public function index(){
        if(empty($_REQUEST['name'])){
            $this->ajax_result(0,'name is null');
        }
    }
    
    /*
     *@purpose    :    ajax 请求后台时,最后需要返回json格式的状态和信息
     *@author     :    daicr
     *@params     :    $status    状态
     *            :   $msg    信息
     *@return     :    json 格式的结果,{"status":0,"msg":"name is null"}
    */
    function ajax_result($status,$msg){
        $res = array(
            'status'=>$status,
            'msg'=>$msg,
        );
        echo json_encode($res);        // 将数组编程 json 格式
        exit;
    }
}

$index = new Index();
$index -> index();

good

原文地址:https://www.cnblogs.com/chrdai/p/6090340.html