-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
91 lines (68 loc) · 2.48 KB
/
index.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
jQuery(document).ready(function($) {
showMeYourCookies('When index document is ready');
$.ajax({
type: 'GET',
url: '/rest/hello'
}).done(function (data, textStatus, jqXHR) {
showMeYourJqXHR('When GET /rest/hello is done', jqXHR);
showMeYourCookies('When GET /rest/hello is done');
var csrfToken = jqXHR.getResponseHeader('X-CSRF-TOKEN');
if (csrfToken) {
var cookie = JSON.parse($.cookie('helloween'));
cookie.csrf = csrfToken;
$.cookie('helloween', JSON.stringify(cookie));
}
$('#helloweenMessage').html(data.message);
}).fail(function (jqXHR, textStatus, errorThrown) {
showMeYourJqXHR('When GET /rest/hello fails', jqXHR);
showMeYourCookies('When GET /rest/hello fails');
if (jqXHR.status === 401) { // HTTP Status 401: Unauthorized
var cookie = JSON.stringify({method: 'GET', url: '/', csrf: jqXHR.getResponseHeader('X-CSRF-TOKEN')});
$.cookie('helloween', cookie);
window.location = '/login.html';
} else {
console.error('Houston, we have a problem...');
}
});
$('#postButton').on('click', function () {
event.preventDefault();
showMeYourCookies('When postButton is clicked');
var cookie = JSON.parse($.cookie('helloween'));
$.ajax({
data: {},
headers: {'X-CSRF-TOKEN': cookie.csrf},
timeout: 1000,
type: 'POST',
url: '/rest/hellopost'
}).done(function(data, textStatus, jqXHR) {
showMeYourJqXHR('When POST /rest/hellopost is done', jqXHR);
showMeYourCookies('When POST /rest/hellopost is done');
console.info("POST succeeded!!!");
}).fail(function(jqXHR, textStatus, errorThrown) {
showMeYourJqXHR('When POST /rest/hellopost fails', jqXHR);
showMeYourCookies('When POST /rest/hellopost fails');
console.error('Problems when posting...');
});
});
$('#logoutButton').on('click', function (event) {
event.preventDefault();
showMeYourCookies('When logoutButton is clicked');
var cookie = JSON.parse($.cookie('helloween'));
$.ajax({
data: {},
headers: {'X-CSRF-TOKEN': cookie.csrf},
timeout: 1000,
type: 'POST',
url: '/logout'
}).done(function(data, textStatus, jqXHR) {
showMeYourJqXHR('When POST /logout is done', jqXHR);
showMeYourCookies('When POST /logout is done');
console.info('congratulations, you have logged out!');
window.location = '/';
}).fail(function(jqXHR, textStatus, errorThrown) {
showMeYourJqXHR('When POST /logout fails', jqXHR);
showMeYourCookies('When POST /logout fails');
console.error('Help! I cannot get out of here!');
});
});
});