jquery(jsonp) 跨域请求

时间:2022-11-21 21:21:02
 
测试jquery跨域记录:

前台:

<script type="text/javascript" src="<{$Think.const.WWW_URL}>js/jquery-1.4.min.js"></script>
<script type="text/javascript">
$(function(){
    $('.test').click(function(){
        var cd=$('input[name="conditon"]').val();
        var url='http://xxx.xxx.com/xxx/xxx/xxx';
        var data={'condition':cd,'is_ajax':'1'};
        $.ajax({
            url:url,
            type:'GET',//跨域只能Get方式
            dataType:'jsonp',
            jsonp:'jsonpCallback',
            data:data,
            success:function (msg) {
                console.log(msg);
            },
            error:function (e) {
                
            }
        });

    });
});
</script>

后台php

    public function selectYj()
    {
         $result="test success...";
        $jsonpCallback=$_GET['jsonpCallback'];
        echo $jsonpCallback.'('.json_encode($result).')';exit();
        
    }