zoukankan      html  css  js  c++  java
  • 黄聪:使用$.getJSON解决ajax跨域访问 JQuery 的跨域方法(服务器端为wordpress程序)

    客户端:

    <input id="cat" name="cat" type="hidden" value="<? echo $category->slug; ?>"/>
    <input id="limit" name="limit" type="hidden" value="0"/>
    <input id="count" name="count" type="hidden" value="5"/>
    <input id="over" name="over" type="hidden" value="0"/>
    <input id="length" name="length" type="hidden" value="30"/>
    <input id="basturl" name="basturl" type="hidden" value="<? bloginfo('url'); ?>/?<? echo $category->slug; ?>="/>
    <div class="btn-more">点击加载5条资讯</div>
    <script>
    function get_post()
    {
        $.getJSON(
                        "http://www.xxx.com/ajax-wap-post?callback=?",
                        { cat: $("#cat").val(), limit:$("#limit").val(), count:$("#count").val(), length:$("#length").val() },
                        function(data)
                        {
                            if(data.result == 0)
                            {
                                $(".btn-more").html("没有更多了……");
                                $("#over").val("1");
                            }else
                            {
                                var html = "";
                                for(var i = 0; i<data.posts.length;i++)
                                {
                                    html = html + "<li><a href='" + $("#basturl").val() + data.posts[i].id+"'>"+data.posts[i].title+"</a><span>"+data.posts[i].date+"</span></li>";
                                }
                                $(".catlist ul").append(html);
                                $(".btn-more").html("点击加载5条资讯");
                                $("#limit").val(parseInt($("#limit").val()) + parseInt(data.result));
                            }
                        },
                          "json"
             );
    }
    get_post();
        $(".btn-more").click(function(){
            if($("#over").val() == "1"){return;}
            
            $(".btn-more").html("加载中……");
            
            get_post();
        });
    </script>

    服务器端:

    <?php
    
    global $wpdb;
    
    if(!$_GET)
    { exit; }
    
    $cat = htmlspecialchars(trim($_GET["cat"]));
    $limit = htmlspecialchars(trim($_GET["limit"]));
    $count = htmlspecialchars(trim($_GET["count"]));
    $length = htmlspecialchars(trim($_GET["length"]));
    
    
        $args = array('numberposts' => $count, 'offset'=> $limit, 'category' => get_category_by_slug($cat)->term_id,'post_status' => 'publish');   
        $posts = get_posts($args);
        $ary = array('result'=>count($posts),'posts'=>array());
        
        foreach( $posts as $post ) {
            array_push($ary["posts"],array('id'=>$post->ID,'title'=> mb_strimwidth($post->post_title, 0, $length, '…','utf-8') ,'date'=> date('Y-m-d',strtotime($post->post_date))));
        }
    
    
    echo $_GET['callback'].'('.json_encode($ary).')';
    exit;
    ?>
  • 相关阅读:
    Mysql(11)_Mysql权限与安全
    Mysql(10)_存储过程与流程控制
    Java(43)_AWT事件处理挂关闭生效
    6.实现合同测试用例
    6.测试库优化
    5.案例回顾及编写测试用例
    4.测试案例实现代码库与测试用例V2.0
    3.测试案例实现代码库与测试用例
    markdown语法学习
    1.faker批量随机造数据
  • 原文地址:https://www.cnblogs.com/huangcong/p/4072196.html
Copyright © 2011-2022 走看看