$this->ajaxReturn($data); //return json

    public function ajax_Return(){    
        //$data = 'ok';
        //"ok"
        //
        //["ok","good","byebye"]
        $data['a'] = 'ok';
        $data['b'] = 'good';
        $data['c'] = 'byebye';
        $this->ajaxReturn($data);
        //{"a":"ok","b":"good","c":"byebye"}
    }

 

$proj_developers=project_get_developer_rows($project_id) ;

$proj_developers=array_values($proj_developers);

$obj_array= json_encode($proj_developers);
header("Content-type: application/json;charset=utf-8");
header_remove("Content-Security-Policy"); 
echo $obj_array;

 D:\LearnWebDevelop\php\thinkphp_3.2.3_full\ThinkPHP\Mode\Api\Controller.class.php

    /**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type AJAX返回数据格式
     * @return void
     */
    protected function ajaxReturn($data,$type='') {
        if(empty($type)) $type  =   C('DEFAULT_AJAX_RETURN');
        switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
            case 'JSONP':
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                $handler  =   isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
                exit($handler.'('.json_encode($data).');');  
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($data);            
        }
    }

 

posted @ 2017-12-18 09:07  sky20080101  阅读(357)  评论(0)    收藏  举报