使用jQuery的$.getJSON獲取數據出錯解決辦法

时间:12-08-16 栏目:网站开发 作者:admin 评论:0 点击: 9,479 次

下面這段代碼,在服務器出錯,或者網絡中斷等原因下,引造成錯誤:

//更新最后一期开奖信息
function updateLast() {
clearTimeout(L1);
var url ="?action=xml&operate=last&a=";
$.getJSON(url,function(msg,status){

if (msg == "-99") {
alert(S4[0]);
top.location.href = "?action=logout&sid=" + SID;
return;
}
L1 = setTimeout('updateLast();',5000);
if(typeof(msg[0])!='undefined'){
//$('#UserResult').html("¥"+msg[0]+"");
$('#UserResult').html(""+msg[0]+"");
UP_LIDtext = ($('#UP_LID').text());
UP_GAMEIDtext = ($('#UP_GAMEID').text());
if(UP_LIDtext!=msg[1][0] && UP_GAMEIDtext==msg[3]){//新的开奖[彩类相同,但]

//console.log("新的开奖:"+UP_LIDtext+" msg[1][0]:"+msg[1][0]);
top.topFrame.PlaySound('sound_03');
update_betcount();
setTimeout("top.leftFrame.location.href=\"?action=account\";",5000);
}
$('#UP_LID').text(msg[1][0]);
$('#UP_GAMEID').text(msg[3]);
if($('#classid').val() && $('#classid').val() != msg[3]){
top.topFrame.SelectType(msg[3]-0);
return ;
}
if(top._class_type != msg[3]){
top.topFrame.SelectType(msg[3]-0);
return ;
}

for(var i in msg[1][1]){//color
switch(msg[1][1][i]){
case 'R':cclass='ballred';break;
case 'G':cclass='ballgreen';break;
case 'B':cclass='ballblue';break;
default:cclass='';
}

var n = '<span>'+(msg[1][2][i]!==null&&typeof(msg[1][2][i])!='undefined'?msg[1][2][i]:"-")+'</span>';

$('#BaLL_n'+(i-0+1)).html(n);
}

if(top.g_timestap<=0 && msg[2]==true){//刚开盘
// console.log("刚开盘,播放声音");
//top.topFrame.PlaySound();

setTimeout("update_betcount();",1000);
setTimeout("window.location = window.location;",1000);
setTimeout("top.leftFrame.location.href=\"?action=account&sid=\";",5000);
top.g_status =  msg[2];
}
}

});
}

修改後,可以在出現錯誤後,繼續運行:

function updateLast(c) {
	clearTimeout(L1);

	$.ajax({
		url: "?action=xml&operate=last&c="+c,
		type: 'GET',
		dataType: 'json',
		timeout: 3000,
		error: function(){
			L1 = setTimeout('updateLast('+((c>0?0:c)-1)+');',5000);
			//alert('Error loading JSON document');
		},
		success: function(msg,status){
			if(status=='success'){
				if (msg == "-99") {
					alert(S4[0]);
					top.location.href = "?action=logout&sid=" + SID;
					return;
				}
				L1 = setTimeout('updateLast('+((c<0?0:c)+1)+');',5000);
				if(typeof(msg[0])!='undefined'){
					//$('#UserResult').html("¥"+msg[0]+"");
					$('#UserResult').html(""+msg[0]+"");
					UP_LIDtext = ($('#UP_LID').text());
					UP_GAMEIDtext = ($('#UP_GAMEID').text());
					if(UP_LIDtext!=msg[1][0] && UP_GAMEIDtext==msg[3]){//新的开奖[彩类相同,但]

						//console.log("新的开奖:"+UP_LIDtext+" msg[1][0]:"+msg[1][0]);
						top.topFrame.PlaySound('sound_03');
						update_betcount();
						setTimeout("top.leftFrame.location.href=\"?action=account\";",5000);
					}
					$('#UP_LID').text(msg[1][0]);
					$('#UP_GAMEID').text(msg[3]);
					if($('#classid').val() && $('#classid').val() != msg[3]){
						top.topFrame.SelectType(msg[3]-0);
						return ;
					}
					if(top._class_type != msg[3]){
						top.topFrame.SelectType(msg[3]-0);
						return ;
					}

					for(var i in msg[1][1]){//color
						switch(msg[1][1][i]){
							case 'R':cclass='ballred';break;
							case 'G':cclass='ballgreen';break;
							case 'B':cclass='ballblue';break;
							default:cclass='';
						}

						var n = '<span class="'+cclass+'">'+(msg[1][2][i]!==null&&typeof(msg[1][2][i])!='undefined'?msg[1][2][i]:"-")+'</span>';

						$('#BaLL_n'+(i-0+1)).html(n);
					}

					if(top.g_timestap<=0 && msg[2]==true){//刚开盘
					   // console.log("刚开盘,播放声音");
						//top.topFrame.PlaySound();

						setTimeout("update_betcount();",1000);
						setTimeout("window.location = window.location;",1000);
						setTimeout("top.leftFrame.location.href=\"?action=account&sid=\";",5000);
						top.g_status =  msg[2];
					}
				}

			}else{
				L1 = setTimeout('updateLast('+(c-1)+');',5000);
			}
		}
	});
}

參考:

http://www.linuxidc.com/Linux/2012-02/53754.htm
http://www.ibm.com/developerworks/cn/xml/x-ajaxjquery.html
http://topic.csdn.net/u/20100528/12/e8013ff6-c511-4411-a575-fee3b4556149.html

声明: 本文由( admin )原创编译,转载请保留链接: 使用jQuery的$.getJSON獲取數據出錯解決辦法

使用jQuery的$.getJSON獲取數據出錯解決辦法:等您坐沙发呢!

发表评论


------====== 本站公告 ======------
联系信息:
电话:19970108113(微信同号) QQ:3142401606
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。

读者排行