外部站点跨域调用迅睿CMS内容的写法??
网站A是dedecms做的
网站B是xunruicms做的.
需求:在网站A里面调用网站B的最新几条新闻。
在dedecms里面肯定不能使用poscms的标签了,只能通过ajax做站外请求,xunruicms支持ajaxp格式,解决了跨域问题,下面来分享我的经验。
1、在xunruicms 建一个模板 \template\pc\default\home\api\123.html,并写好调用标签
注意,这里是放在模板目录下的 api文件夹里,默认是不存在的,自己新建一个文件夹
当然这里的 123.html 是自定义的名字,你可以根据你自己的需要修改
{list action=module module=news num=10}
{$t.title}
{/list}2、在要调用的网站里写入如下代码:
<div id="xrcms_news"></div>
<script type="text/javascript">
$.ajax({
type: "GET",
url:"http://你的域名/index.php?s=api&c=api&m=template&name=123.html&format=jsonp",
dataType: "jsonp",
success: function(json){
$("#xrcms_news").html(json.msg);
},
error: function(){ }
});
</script>
$.get('http://你的域名/index.php?s=api&c=api&m=template&name=123.html&format=json&'+Math.random(),function(res){ if(res.code==1){ $('#xrcms_news').html(res.msg); } }, 'json');