• 技术文章 >后端开发 >php教程

    Ajax请求json数据

    2016-08-08 09:28:24原创646
    同域请求json数据

    客户端js代码:

    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代码:

    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数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:rsList json gt array callback

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 你知道如何用PHP实现多进程吗• PHP与MySQL连接的方法总结• 求解:phpcms模板怎样转码?该怎么解决• php 之 cookie 跟 session 简单解读(笔记)• php 网页截取快讯代码
    1/1

    PHP中文网