简体   繁体   中英

RESTfull web service display info on page, using visual studio 2015

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.

Testing shows this 在此输入图像描述

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. 在此输入图像描述

But I get nothing. 在此输入图像描述

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM