-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
177 lines (164 loc) · 6.53 KB
/
index.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"
integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<title>Django with Ajax</title>
</head>
<body>
<section>
<div class="container m-5 p-5">
<div class="row bg-secondary m-3 p-2 text-light d-flex align-items-center">
<div class="col-lg-4 d-flex justify-content-center p-5">
<form class="row" id="post-form">
<h1 class="text-center">Form</h1>
<div class="col-md-12">
<label for="name" class="form-label">Title</label>
<input type="text" class="form-control" name="title" id="title">
</div>
<div class="col-md-12">
<label for="name" class="form-label">Code</label>
<input type="text" class="form-control" name="code" id="code">
</div>
<div class="col-md-12 mt-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="linenos" id="linenos">
<label class="form-check-label" for="linenos">
Default checkbox
</label>
</div>
</div>
<div class="col-12 text-center mt-3">
<input type="submit" value="SAVE" id="save" class="btn btn-lg btn-outline-warning">
</div>
</form>
</div>
<div class="col-lg-8 d-flex justify-content-center">
<div class="table-responsive text-light">
<table id="example" class="table text-light table-hover" style="width:100%">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Code</th>
<th>Linenos</th>
<th>Created</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</section>
</body>
<link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
<script>
$.ajax({
type: "GET",
url: 'http://127.0.0.1:8000/api/snippet/?format=json',
dataType: 'json',
success: function (obj, textstatus) {
$('#example').DataTable({
data: obj,
pageLength: 5,
columns: [
{ data: 'id' },
{ data: 'title' },
{ data: 'code' },
{ data: 'linenos' },
{ data: 'created' },
]
});
},
error: function (obj, textstatus) {
alert(obj.msg);
}
});
$(document).ready(function () {
$("#save").click(function () {
var data = new Object();
data.title = $('#title').val();
data.code = $('#code').val();
data.linenos = $('#linenos').prop('checked')
$.ajax({
url: 'http://127.0.0.1:8000/api/snippet/',
type: 'POST',
dataType: 'json',
data: data,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
});
</script>
<!--
//Get with bootstrap working
$(document).ready(function () {
$.ajax({
url: "http://127.0.0.1:8000/api/snippet/",
type: "GET",
dataType: "json",
success: function (data) {
console.log(data);
var html = "";
$.each(data, function (key, value) {
html += "<tr>";
html += "<td>" + value.id + "</td>";
html += "<td>" + value.title + "</td>";
html += "<td>" + value.code + "</td>";
html += "<td>" + value.linenos + "</td>";
html += "</tr>";
});
$("#tbody").html(html);
}
});
}); -->
<!--
$(document).ready(function () {
$("#post-form").submit(function (event) {
event.preventDefault();
$.ajax({
url: 'http://127.0.0.1:8000/api/emp/',
type: "POST",
data: $('#post-form').serialize(),
success: function (data) {
console.log(data);
},
error: function (data) {
console.log("error");
}
});
});
});
-->
<!--
<script>
$.ajax({
type: 'GET',
dataType: "json",
url: '../api/emp/?format=json',
success: function (data, status, xhr) {
console.log('data: ', data);
}
});
</script> -->
</html>