I have trouble with building a web service can read information of NBA players from a txt file and then display it on the web page.
Firstly, I build a class in the Models folder.
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; }
}
}
Secondly I create a Controller to read file and add all players to an list. I also define a GetAllPlayers method to return the list.
Then I create a html page to display the list. Here is my code.
<!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>
It should shows something like this.
What I've done wrong???
Here is the tutorial follow. https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
You seem to be appending the players list item to an element with id "playerList":
$('<li>', { text: formatItem(item) }).appendTo($('#playerList'));
Problem is, "playerList" doesn't seem to exist, I see you have a "players" instead?
<ul id="players" />
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments