我试图从网址获取:http://redsox.tcs.auckland.ac.nz/BC/Open/Service.svc/booklist 然后将其解析为JSON,将JSON格式发送到表创建函数showTable()。
html文件:
<!DOCTYPE html>
<html>
<head>
<title>Online Shop</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script src="myscripts.js"></script>
</head>
<body>
<h1 id="mainheader">Boutique</h1>
<table id="menuTable" align="center">
<tr>
<td><button onclick="getResults()">Browse shop</button></td>
<td><button onclick="joinShop()">join the shop</button></td>
<td><button onclick="showComments()">Your comments</button></td>
</tr>
</table>
<hr></hr>
<div id="bodyDiv">
</div>
</body>
</html>
相关的javascript:
function getResults(){ //gotta change it to take appended shit after ".svc/"
var uri = "http://redsox.tcs.auckland.ac.nz/BC/Open/Service.svc/booklist";
var xhr = new XMLHttpRequest();
xhr.open('GET', uri, true);
xhr.onload = function (){
var resp = JSON.parse(xhr.responseText);
showTable(resp.value);
}
xhr.send(null);
}
function showTable(item) {
var tableContent = "<tr class='orderTitle'><td>book id</td><td>book title</td></tr>\n"//creating unique table heading column
for(var i = 0; i < item.length; ++i) {
var record = item[i];
if (i & 1 == 1){
tableContent += "<tr class='orderOdd'>";
}
else { //even row
tableContent += "<tr class='orderEven'>";
}
tableContent += "<td>" + record.Id + "</td><td>" + record.Title + "</td></tr>\n"
}
document.getElementById("bodyDiv").innerHTML = tableContent;
}