使用$.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;
?>
复制代码



本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/p/4072196.html,如需转载请自行联系原作者
相关文章
|
7天前
|
JavaScript
jQuery 遍历 方法
jQuery 遍历 方法
21 5
|
1天前
|
前端开发 JavaScript
jQuery - AJAX load() 方法
jQuery load() 方法是简单但强大的 AJAX 方法。
22 6
|
12天前
|
JavaScript
jQuery parentsUntil() 方法
jQuery parentsUntil() 方法
23 10
|
6天前
|
JavaScript 前端开发
jQuery 杂项方法
jQuery 杂项方法
12 2
|
7天前
|
JavaScript
jQuery 效果 方法
jQuery 效果 方法
9 3
|
9天前
|
JavaScript 前端开发
jQuery - noConflict() 方法
jQuery - noConflict() 方法
14 5
|
6天前
|
JSON 前端开发 JavaScript
jQuery AJAX 方法
jQuery AJAX 方法
11 1
|
7天前
|
XML 前端开发 JavaScript
jQuery HTML / CSS 方法
jQuery HTML / CSS 方法
9 2
|
7天前
|
人工智能 网络协议 Shell
内网穿透实现公网访问自己搭建的Ollma架构的AI服务器
内网穿透实现公网访问自己搭建的Ollma架构的AI服务器
21 1
|
12天前
|
JavaScript
jQuery parents() 方法
jQuery parents() 方法
15 7
下一篇
无影云桌面