thinkphp处理前端跨域请求

2018-07-04  本文已影响179人  jnxc1888

1、前端js正常ajax请求

<script>
    function testGet(){
        $.ajax({
            url: "http://xxxx.com/home/signin/getxxx?session_id=xxxx",
            type: "get",
            dataType: "json",
            success: function (data) {
                console.log(data);
            },
            error: function(data){
                console.log(data);
            }
        });
    }

    function testPost(){
        $.ajax({
            url: "http://xxx.com/home/signin/getxxx",
            type: "post",
            dataType: "json",
            data: {
                session_id: 'xxxxx'
            },
            success: function (data) {
                console.log(data);
            },
            error: function(data){
                console.log(data);
            }
        });
    }
</script>

后端php返回

jsonResponse($respone);

//jsonResponse方法:
//function jsonResponse($data)
//{
//    exit(json_encode($data, JSON_UNESCAPED_SLASHES));
//}

2、前端js用ajax跨域请求

<script>
    function testGet(){
        $.ajax({
            url: "http://xxxx.com/home/signin/getxxx?session_id=xxxx",
            type: "get",
            dataType: "jsonp",
            success: function (data) {
                console.log(data);
            },
            error: function(data){
                console.log(data);
            }
        });
    }

    function testPost(){
        $.ajax({
            url: "http://xxx.com/home/signin/getxxx",
            type: "post",
            dataType: "jsonp",
            data: {
                session_id: 'xxxxx'
            },
            success: function (data) {
                console.log(data);
            },
            error: function(data){
                console.log(data);
            }
        });
    }
</script>

thinkphp返回

$this->ajaxReturn($res,JSONP);

ajaxReturn方法:

/**
     * 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);            
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读