forked from tclavier/jersey-skeleton
/
script.js
109 lines (99 loc) · 3.23 KB
/
script.js
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
$(document).ready(function() {
$('#get').click(function (event) {
console.log("get books")
$.ajax({
// The URL for the request
url: "/books",
// The data to send (will be converted to a query string)
/*data: {
id: 123
},
*/
// Whether this is a POST/GET/UPDATE/DELETE request
type: "GET",
// The type of data we expect back
dataType : "json",
// Code to run if the request succeeds;
// the response is passed to the function
success: function( json ) {
JSON.stringify(json);
var output = $("#output");
$("<h1>").text("Livres :").appendTo(output);
for(var i = 0 ; i < json.length ; i++){
$("<h4>").html("-["+json[i].id+"] "+json[i].title +" "+json[i].author).appendTo(output);
}
$( "<div class=\"content\">").html(json.html).appendTo( "body" );
},
// Code to run if the request fails; the raw request and
// status codes are passed to the function
error: function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
// Code to run regardless of success or failure
complete: function( xhr, status ) {
console.log( "The request is complete!" );
}
});
});
$('#create').click(function(event) {
$.ajax({
// The URL for the request
url:"books",
data: {
author : $('#insertTitre').val(),
title : $('#insertAuteur').val()
},
// Whether this is a POST/GET/UPDATE/DELETE request
type:"POST",
// The type of data we expect back
dataType:"json",
// Code to run if the request succeeds;
// the response is passed to the function
success: function(json) {
//OK
},
// Code to run if the request fails; the raw request and
// status codes are passed to the function
error: function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
// Code to run regardless of success or failure
complete: function( xhr, status ) {
console.log("complete");
}
});
});
$('#DELETE').click(function(event) {
var tmp;
tmp = $('#deleteId').val();
console.log(tmp);
$.ajax({
url:"books/"+tmp,
// Whether this is a POST/GET/UPDATE/DELETE request
type:"DELETE",
// The type of data we expect back
dataType:"json",
success: function(json) {
//OK
},
// Code to run if the request fails; the raw request and
// status codes are passed to the function
error: function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
// Code to run regardless of success or failure
complete: function( xhr, status ) {
console.log("complete");
}
});
});
}); // end document.ready