AJAX访问JSON

{
	"topic": "MotoGP Racing",
	"subject": "Top 10 Rider Statistics",
	"source": "Wikipedia",
	"sourceUrl": "https://en.wikipedia.org/wiki/List_of_500cc/MotoGP_Motorcycle_World_Champions#Multiple_champions",
	"riderStats": [
		{
			"name": "Giacomo Agostini",
			"rank": "1",
			"wins": 122,
			"starts": 223,
			"link": "https://en.wikipedia.org/wiki/Giacomo_Agostini"
		}, {
			"name": "Valentino Rossi",
			"rank": "2",
			"wins": 89,
			"starts": 369,
			"link": "https://en.wikipedia.org/wiki/Valentino_Rossi"
		}
	]
}
<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Insert title here</title>
		<style>
			*{
				margin: 0 auto;
				padding: 0;
				text-align: center;
			}
			
			table,tr,th,td{
				border: 1px solid #808080;
				border-collapse: collapse;
			}
			.app{
				margin: 0 auto;
				
			}
			#hotel{
				margin: 0 auto;
			}
			#hotelData{
				margin: 0 auto;
			}
			.myBtn{
				width: 100px;/*a width value of 100px*/
				padding: 10px;
				background-color: #ecf2f4;
				margin-top: 15px;
			}
			.myCaption{
				font-size: 21px;
				font-weight: bold;
				padding-top: 30px;
				padding-bottom: 10px;
			}
			th{
				background-color: #a8a8a8;
			}
			th,td{
				padding: 10px;
				font-size: 20px;
			}
			/*nth-child() pseudo-class must be applied to shade even table rows (see “Design Resources”)*/
			 table tr:nth-child(even){
		        background-color: #e0e0e0;
		    }
		    #leftAlign{
		    	text-align: left;
		    }
		</style>
		<script>
			function loadJSONDoc(url) {

				var xmlhttp;
				var txt, x, xx, i;
				if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				} else { // code for IE6, IE5
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}

				xmlhttp.open("GET", url, true);
				xmlhttp.send();
				xmlhttp.onreadystatechange = function() {
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						txt = "<table border='1'><caption class='myCaption'>MotoGP Racing:Top 10 Rider Statistics</caption ><tr><th>Rank</th><th>Rider Name</th><th>Total Starts</th><th>Total Wins</th><th>Total Losses</th><th>Win Percentage</th></tr>";
						x = JSON.parse(xmlhttp.responseText);  ;
						xx = x.riderStats;
						console.log(xx);
						for(i = 0; i < xx.length; i++) {
							
							try {
								
								txt = txt + "<td>" + xx[i].rank + "</td>";
							} catch(er) {
								txt = txt + "<td> </td>";
							}							
							try {
								txt = txt + "<td id='leftAlign'><a href='#' target='_blank'>" + xx[i].name + "</a></td>";
							} catch(er) {
								txt = txt + "<td> </td>";
							}
							try {
								txt = txt + "<td>" + xx[i].starts + "</td>";
							} catch(er) {
								txt = txt + "<td> </td>";
							}
							try {
								txt = txt + "<td>" + xx[i].wins + "</td>";
							} catch(er) {
								txt = txt + "<td> </td>";
							}
							try {
								txt = txt + "<td>" + (parseInt(xx[i].starts)-parseInt(xx[i].wins)) + "</td>";
							} catch(er) {
								txt = txt + "<td> </td>";
							}
							try {
								let s = ((parseFloat(xx[i].wins)/parseFloat(xx[i].starts))*100).toFixed(2);
								if(s < 30){
									txt = txt + "<td style='background:#ff9999'>" + s + "%</td>";
								}else if(s < 50){
									txt = txt + "<td  style='background:#ffdb99'>" + s + "%</td>";
								}else{
									txt = txt + "<td  style='background:#99cc99'>" + s + "%</td>";
								}
								
							} catch(er) {
								txt = txt + "<td> </td>";
							}
							
							txt = txt + "</tr>";
							
							
						}
						txt = txt + "</table><h3 style='margin-top:10px'>The raw data used for caculating these results has been obtained from <a href=\"#\" target='_blank'>Wikipedia</a></h3>";
						document.getElementById('hotelData').innerHTML = txt;
					}

				}
			}

		</script>

	</head>

	<body>
	<div class="app">
		<div id="hotel">
			<h2 style="margin-top: 10px;">Task4:AJAX&JSON</h2>
			<button class="myBtn" onclick="loadJSONDoc('A5_TASK3.json')">Start</button>		
		</div>
		<div id="hotelData">
			
		</div>
	
	</div>
	</body>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值