首页 > Web开发 > 详细

Ajax请求json数据

时间:2015-03-13 14:01:39      阅读:327      评论:0      收藏:0      [点我收藏+]

同域请求json数据

客户端js代码:

技术分享
<script>
    $.ajax({
        url: ‘http://127.0.0.2/index.php‘,
        type: ‘GET‘,
        dataType: ‘json‘,
        data: {ac: ‘xcajax‘,at: ‘goodslist‘},
        success: function(json){
            $.each(json,function(i){
                   console.log(json[i].title);
            })
        }
    });
    
    
</script>
View Code

服务端端PHP代码:

技术分享
function in_goodslist() {
        $callback = $this->fun->accept(‘callback‘, ‘G‘);
        $db_table = db_prefix . ‘advert‘;
        $db_where = ‘ WHERE isclass=1 AND atid=6‘;
        $bann_array = array();
        $sql = "SELECT * FROM $db_table $db_where ORDER BY pid,adid DESC LIMIT 0,10";
        $rs = $this->db->query($sql);
        while ($rsList = $this->db->fetch_assoc($rs)) {

            if ($rsList[‘islink‘] == 2 && $rsList[‘gotoid‘] > 0) {
                $docread = $this->get_documentview($rsList[‘gotoid‘]);
                if ($docread[‘did‘] > 0) $rsList[‘url‘] = $this->get_link(‘doc‘, $docread, $lngpack);
            }

            if ($rsList[‘istime‘] == 1) {
                if ($rsList[‘starttime‘] < time() && $rsList[‘endtime‘] > time()) {
                    $bann_array[] = $rsList;
                }
            } else {
                $bann_array[] = $rsList;
            }
        }
        $json = json_encode($bann_array);
        //$json = $bann_array;
        echo $callback."($json)";
    }
View Code

跨域请求json数据

客户端js代码:

技术分享
<script>
    $.ajax({
        type: ‘GET‘,
        async: false,
        url: ‘http://zbcn.cn/index.php‘,
        dataType: ‘jsonp‘,
        data: {ac: ‘xcajax‘,at: ‘goodslist‘},
        jsonp: ‘callback‘,
        success: function(json){
            $.each(json,function(i){
                   console.log(json[i].title);
            })
        }
    });
    
    
</script>
View Code

服务端php代码:

技术分享
function in_goodslist() {
        $callback = $this->fun->accept(‘callback‘, ‘G‘);//GET 封装
        $db_table = db_prefix . ‘advert‘;
        $db_where = ‘ WHERE isclass=1 AND atid=6‘;
        $bann_array = array();
        $sql = "SELECT * FROM $db_table $db_where ORDER BY pid,adid DESC LIMIT 0,10";
        $rs = $this->db->query($sql);
        while ($rsList = $this->db->fetch_assoc($rs)) {

            if ($rsList[‘islink‘] == 2 && $rsList[‘gotoid‘] > 0) {
                $docread = $this->get_documentview($rsList[‘gotoid‘]);
                if ($docread[‘did‘] > 0) $rsList[‘url‘] = $this->get_link(‘doc‘, $docread, $lngpack);
            }

            if ($rsList[‘istime‘] == 1) {
                if ($rsList[‘starttime‘] < time() && $rsList[‘endtime‘] > time()) {
                    $bann_array[] = $rsList;
                }
            } else {
                $bann_array[] = $rsList;
            }
        }
        $json = json_encode($bann_array);
        echo $callback."($json)";
    }
View Code

 

Ajax请求json数据

原文:http://www.cnblogs.com/ahhg/p/4272182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!