黃聰:使用$.getJSON解決ajax跨域訪問 JQuery 的跨域方法(伺服器端為wordpress程式)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   for   

用戶端:

<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>

 

 

伺服器端:

<?phpglobal $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;?>

 

黃聰:使用$.getJSON解決ajax跨域訪問 JQuery 的跨域方法(伺服器端為wordpress程式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.