Home > Article > Backend Development > Ajax request json data
Request json data in the same domain
Client js code:
View Code
Server-side PHP code:
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
Request json data across domains
Client js code:
View Code
Server-side php code:
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
The above introduces the Ajax request for json data, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.