-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors-test.html
More file actions
114 lines (99 loc) · 3.56 KB
/
Copy pathcors-test.html
File metadata and controls
114 lines (99 loc) · 3.56 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CORS Test</title>
<style>
#payload {
width: 400px;
height: 200px;
}
#response-container {
width: 800px;
height: 400px;
}
#status-code {
font-weight: bold;
}
dl {
display: grid;
grid-template-columns: max-content auto;
row-gap: 10px;
}
dt {
grid-column-start: 1;
}
dd {
grid-column-start: 2;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
window.onload = function() {
init();
};
function init() {
prepareSubmitForm();
}
function prepareSubmitForm() {
$('#cors-form').submit(function(e) {
url = $('#endpoint').val();
payload = $('#payload').val();
$.ajax({
type: $('#method').val(),
beforeSend: function(request) {
request.setRequestHeader("Authorization", 'Bearer '+ $('#bearer_token').val());
},
url: url,
data: payload,
success: function(data, statusCode, xhr) {
returnSuccess(data, statusCode, xhr);
},
error: function(data, statusCode, xhr) {
console.log('we are here');
returnError(data);
},
dataType: 'json',
contentType: 'application/json'
});
e.preventDefault();
});
}
function returnSuccess(data, statusCode, xhr) {
var dataStr = JSON.stringify(data, undefined, 4);
$('#status-code').html(xhr.status);
$('#response-container').text(dataStr);
}
function returnError(response) {
var dataStr = JSON.stringify(response.responseJSON, undefined, 4);
$('#status-code').html(response.status);
$('#response-container').text(dataStr);
}
</script>
</head>
<body>
<form id="cors-form">
<dl>
<dt>Bearer token:</dt>
<dd><input type="text" id="bearer_token" style="width:500px" /></dd>
<dt>Method:</dt>
<dd><select id="method">
<option>GET</option>
<option selected="selected">POST</option>
<option>PATCH</option>
<option>DELETE</option>
</select>
</dd>
<dt>Endpoint:</dt>
<dd><input type="text" id="endpoint" value="http://localhost:8000/api/create-user" style="width:500px" /></dd>
</dl>
<textarea id="payload"></textarea>
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
<h3>Response</h3>
<p>Status code: <span id="status-code"></span></p>
<textarea id="response-container"></textarea>
</body>
</html>