黄聪:使用$.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;
?>
黄聪:使用$.getJSON解决ajax跨域访问 JQuery 的跨域方法(服务器端为wordpress程序)的更多相关文章
- SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域
SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域 >>>>>>>>>>>> ...
- SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域[转]
SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域 原文地址:https://www.cnblogs.com/fanshuyao/p/716847 ...
- 黄聪:PHP解决textarea内容换行存入数据库,如何解析取出不能自动换行
解决办法: <textarea rows="5" style="height: auto;" ><?php //按行分割,然后每行输出后带上一 ...
- 黄聪:wkhtmltopdf解决分页问题
wkhtmltopdf 有个很好的方法,就是在那个div的样式后添加一个:page-break-inside:avoid;就ok了. <!DOCTYPE html PUBLIC "-/ ...
- 黄聪:路由器WIFI连接无法正常访问个别网站及发送图片
打开路由,路由默认MTU是1500,改成1472 就解决了
- Ajax跨域访问解决方案
No 'Access-Control-Allow-Origin' header is present on the requested resource. 当使用ajax访问远程服务器时,请求失败,浏 ...
- Geoserver通过ajax跨域访问服务数据的方法(含用户名密码认证的配置方式)
Goeserver数据有两种,一种需进行用户密码的权限认证,一种无须用户密码.对于网上跨域访问Geoserver数据的种种方法,对这2种数据并非通用. 笔者将Geoserver官方下载的Geoserv ...
- Android WebView存在跨域访问漏洞(CNVD-2017-36682)介绍及解决
Android WebView存在跨域访问漏洞(CNVD-2017-36682).攻击者利用该漏洞,可远程获取用户隐私数据(包括手机应用数据.照片.文档等敏感信息),还可窃取用户登录凭证,在受害者毫无 ...
- jQuery jsonp跨域请求
跨域的安全限制都是对浏览器端来说的,服务器端是不存在跨域安全限制的. 浏览器的同源策略限制从一个源加载的文档或脚本与来自另一个源的资源进行交互. 如果协议,端口和主机对于两个页面是相同的,则两个页面具 ...
随机推荐
- 推荐一些android开发学习的资料
网址: 1:http://v.youku.com/v_show/id_XMTgwMTQ1MTgw.html 2:http://mars.apkbus.com/ 3:http://wenku.baidu ...
- JS初学之-循环生成坐标
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- 铺地毯 2011年NOIP全国联赛提高组
题目描述 Description 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从 ...
- URAL 1291 Gear-wheels(BFS)
Gear-wheels Time limit: 1.0 secondMemory limit: 64 MB - Arny! What happened with coordinator? Bad wo ...
- Linux查找文件
which 可以查找可执行文件的位置 evilxr@IdeaPad:~$ which ping /bin/ping whereis whereis -m 可查询到命令的帮助文档在什么地方 evilxr ...
- 使用CURL下载远程文件保存到服务器
比如微信公众平台开发,下载用户的头像到服务器上: /** * 使用CURL获取远程文件保存到服务器 *@param $image=$oJSON->headimgurl; 获取到的微信返回的头像U ...
- kuangbin_UnionFind C (HDU 1213)
过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...
- C++@类对象和类指针的区别(转)
原文地址不详 如下程序: #include <iostream> #include <string> using namespace std; class Student { ...
- windows API
Process and Thread Functions https://msdn.microsoft.com/en-us/library/windows/desktop/ms684847(v=vs. ...
- dede留言板链接变成localhost的解决办法
去前缀arctype表中找到在线留言栏 把根目录换成{cmspath}/ 例如{cmspath}/plus/guestbook.php 或者在后台的栏目管理里改---但是预览不能使用了