DOCTYPE html html head meta charset utf-8 title Library title head bod

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Library</title>
</head>
<body>
<div>
<h2>All UserCards</h2>
<ul id="userCards"></ul>
</div>
<div>
<h2>Search by ID</h2>
<input type="text" id="userCardId" size="5" />
<input type="button" value="Search" onclick="find();" />
<p id="userCard" />
</div>
<div>
<h2>Search by First Name</h2>
<input type="text" id="userCardFirstName" size="10" />
<input type="button" value="Search by name" onclick="findByFirstName();"/>
<p id="userCard" />
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
var uri = 'api/UserCards';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#userCards'));
});
});
});
function formatItem(item) {
return item.Id + ' ' + item.FirstName + ' ' + item.LastName;
}
function find() {
var id = $('#userCardId').val();
$.getJSON('api/UserCard/' + id)
.done(function (data) {
$('#userCard').text(formatItem(data));
})
.fail(function (jqXHR, textStatus, err) {
$('#userCard').text('Error: ' + err);
});
}
function findByFirstName() {
var fName = $('#userCardFirstName').val();
$.getJSON('api/UserCard/ByName/' + fName)
.done(function (data) {
$('#userCard').text(formatItem(data));
})
.fail(function (jqXHR, textStatus, err) {
$('#userCard').text('Error: ' + err);
});
}
</script>
</body>
</html>