Ajax请求json数据

2025-09-07

同域请求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);            })        }    });        
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);            })        }    });        
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教程有兴趣的朋友有所帮助。

标签: ajax请求json数据

本文地址:https://www.lifejia.cn/news/213607.html

免责声明:本站内容仅用于学习参考,信息和图片素材来源于互联网,如内容侵权与违规,请联系我们进行删除,我们将在三个工作日内处理。联系邮箱:cloudinto#qq.com(把#换成@)