zoukankan      html  css  js  c++  java
  • Ajax请求json数据

    同域请求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
  • 相关阅读:
    win11 千呼万唤 原生安卓体验及安装方法
    (转)pytorch和torch框架对比(区别 联系)
    AI深度学习部分框架了解
    有趣的USB接口和颜色分类
    后疫情时代读《浪潮之巅》第四版 读书笔记
    关于win11 VBS(基于虚拟化的安全性) 相关研究中
    Windows IPsec IP安全策略
    Element ui复杂表格(多级表头、尾行求合、单元格合并)前端导出excel
    es的常用字段类型和查询
    oom常见的解决方式
  • 原文地址:https://www.cnblogs.com/ahhg/p/4272182.html
Copyright © 2011-2022 走看看