Home > Backend Development > PHP Tutorial > Ajax request json data

Ajax request json data

WBOY
Release: 2016-08-08 09:28:24
Original
1117 people have browsed it

Request json data in the same domain

Client js code:

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

Server-side PHP code:

<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'] < <span>time</span>() && <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>
Copy after login
View Code

Request json data across domains

Client js code:

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

Server-side php code:

<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'] < <span>time</span>() && <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>
Copy after login
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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template