这里要注意一点,当服务端返回的数据是string类型而不是json时,dataType:json,ajax会去走error方法,返回值是:readysSatus:400,responseText:"aaaa",status“200”,statusText:"ok"
<body>
<div class="focus">
<ul class="clear">
<li>
<h3><span>1</span><em>在微信中点击放大镜</em></h3>
<img src="./images/step1.png" >
</li>
<li>
<h3><span>2</span><em>点击搜索框,长按粘贴“太易文化”,点击搜索</em></h3>
<img src="./images/step2.png" >
</li>
<li>
<h3><span>3</span><em>找到“pillar工作室”公众号,点击关注</em></h3>
<img src="./images/step3.png" >
</li>
</ul>
<a href="" id="toFocus">立即关注</a>
</div>
<div id="jinbiLayer">
<div class="jinbi-box">
<h3 class="jb-tit"><img src="./images/jb-tit.png"></h3>
<span class="jb-close" id="jb-close" ><img src="./images/close.png"></span>
<div class="jb-txt"><span id="gold-num"></span>金币</div>
<p>恭喜,已完成关注任务</p>
<a class="toDisk" href="com.calendar2345://task/
android">继续其他任务</a>
</div>
<div class="ad-pic"><a href="" id="ad-link"><img src="" id="ad-pic"></a></div>
</div>
<script src="http://www.77tianqi.com/js/jquery.min.js"></script>
<script>
$(function(){
//轮循30s每2s一次5次每4s一次5次
var weChatMethods={
toWeChatResult: function (){
var _this = this;
var $time = 0;
var timerOne = setInterval(function(){
if($time<5){
$time++;
_this.toWechatAjax();
if($time>=5){
clearInterval(timerOne);
var timerTwo = setInterval(function(){
$time++;
if($time>=10){
clearInterval(timerTwo);
}
_this.toWechatAjax();
},4000)
}
}
},2000)
},
//请求服务端获取是否关注成功
toWechatAjax:function (){
var postData = window.wechat.getWeChatFocusRequestEncryptString();
//alert(postData);
$.ajax({
type: "post",
url: "http://dan.oyinji.com/frame/api/task/queryFocusTask",
dataType: ‘text‘,
data:{"data":postData},
success: function (msg) {
var msg = window.wechat.decryptWeChatFocusString(msg);
var msg = JSON.parse(msg);
var data = msg.data;
if(msg.code==200 && data.taskFinish=="true"){
console.log("关注成功");
var jinbiNum = data.taskGold;
$("#jinbiLayer").show();
$("#gold-num").
html(jinbiNum);
$("#ad-link").attr("href",data.adverImage);
$("#ad-pic").attr("src",data.adverUrl);
}else{
alert("关注失败");
}
},
error: function (msg) {alert(JSON.stringify(msg))}
})
},
//点击事件
events:function(){
//点击立即去关注
$("#toFocus").click(function(){
//调起微信
window.wechat.launchWeChat();
//复制文案给客户端
window.wechat.pasteAccountName(‘太易文化‘);
})
//点击金币弹窗按钮
$("#closeBtn").click(function(){
//弹窗点击确认返回任务列表
window.wechat.onWeChatGuideGoToTaskPage();
})
$("#jb-close").click(function(){
$(‘#jinbiLayer‘).hide();
})
},
//判断是否有后台切入前台
onWebViewStateSwitched: function (isForeground){
if(isForeground==true){
toWeChatResult();
}
}
}
weChatMethods.events();
})
</script>
</body>