[英]RESTfull web service display info on page, using visual studio 2015
我在構建Web服務時遇到麻煩,可以從txt文件中讀取NBA玩家的信息,然后將其顯示在網頁上。
首先,我在Models文件夾中構建了一個類。
namespace zuoye4.Models
{
public class Players
{
public string Registration_ID { get; set; }
public string Player_name { get; set; }
public string Team_name { get; set; }
public DateTime Date_of_birth { get; set; }
}
}
其次,我創建一個Controller來讀取文件並將所有玩家添加到列表中。 我還定義了一個GetAllPlayers方法來返回列表。
然后我創建一個html頁面來顯示列表。 這是我的代碼。
<!DOCTYPE html>
<html>
<head>
<title>PLALYERS</title>
<meta charset="utf-8" />
</head>
<body>
<div>
<h2>All Players</h2>
<ul id="players" />
</div>
<div>
<h2>Search by ID</h2>
<input type="text" id="prodId" size="5" />
<input type="button" value="Search" onclick="find();" />
<p id="product" />
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
var uri = 'api/Players';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of players.
$.each(data, function (key, item) {
// Add a list item for the player.
$('<li>', { text: formatItem(item) }).appendTo($('#playerList'));
});
});
});
function formatItem(item) {
return item.Registration_ID + ': $' + item.Player_name;
}
</script>
</body>
</html>
我做錯了什么???
這是教程如下。 https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
您似乎將播放器列表項附加到ID為“playerList”的元素:
$('<li>', { text: formatItem(item) }).appendTo($('#playerList'));
問題是,“playerList”似乎不存在,我看到你有一個“玩家”而不是?
<ul id="players" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.