首页 > 后端开发 > php教程 > Ajax请求json数据

Ajax请求json数据

WBOY
发布: 2016-08-08 09:28:24
原创
1113 人浏览过

同域请求json数据

客户端js代码:

<script><span>
    $.ajax({
        url: 'http://127.0.0.2/index.php'<span>,
        type: 'GET'<span>,
        dataType: 'json'<span>,
        data: {ac: 'xcajax',at: 'goodslist'<span>},
        success: <span>function<span>(json){
            $.each(json,<span>function<span>(i){
                   console.log(json[i].title);
            })
        }
    });
    
    
</script>
登录后复制
View Code

服务端端PHP代码:

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

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

            </span><span>if</span> (<span>$rsList</span>['istime'] == 1<span>) {
                </span><span>if</span> (<span>$rsList</span>['starttime'] time() && <span>$rsList</span>['endtime'] > <span>time</span><span>()) {
                    </span><span>$bann_array</span>[] = <span>$rsList</span><span>;
                }
            } </span><span>else</span><span> {
                </span><span>$bann_array</span>[] = <span>$rsList</span><span>;
            }
        }
        </span><span>$json</span> = json_encode(<span>$bann_array</span><span>);
        </span><span>//</span><span>$json = $bann_array;</span>
        <span>echo</span> <span>$callback</span>."(<span>$json</span>)"<span>;
    }</span>
登录后复制
View Code

跨域请求json数据

客户端js代码:

<script><span>
    $.ajax({
        type: 'GET'<span>,
        async: <span>false<span>,
        url: 'http://zbcn.cn/index.php'<span>,
        dataType: 'jsonp'<span>,
        data: {ac: 'xcajax',at: 'goodslist'<span>},
        jsonp: 'callback'<span>,
        success: <span>function<span>(json){
            $.each(json,<span>function<span>(i){
                   console.log(json[i].title);
            })
        }
    });
    
    
</script>
登录后复制
View Code

服务端php代码:

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

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

            </span><span>if</span> (<span>$rsList</span>['istime'] == 1<span>) {
                </span><span>if</span> (<span>$rsList</span>['starttime'] time() && <span>$rsList</span>['endtime'] > <span>time</span><span>()) {
                    </span><span>$bann_array</span>[] = <span>$rsList</span><span>;
                }
            } </span><span>else</span><span> {
                </span><span>$bann_array</span>[] = <span>$rsList</span><span>;
            }
        }
        </span><span>$json</span> = json_encode(<span>$bann_array</span><span>);
        </span><span>echo</span> <span>$callback</span>."(<span>$json</span>)"<span>;
    }</span>
登录后复制
View Code

以上就介绍了Ajax请求json数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板