-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
79 lines (68 loc) · 1.9 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
<!DOCTYPE html>
<html>
<head>
<title>Tom Brennan Redbrain Assignment</title>
</head>
<body>
<h1>Travel Itinerary API</h2>
<form id="form">
<textarea name="title" rows="40" cols="60" >
[
{
"source":"Stockholm","destination":"New York JFK",
"type":"Flight",
"seat":"7B",
"vehicleName":"SK22",
"boardingGate":"22"
},
{
"source":"Barcelona",
"destination":"Gerona Airport",
"type":"Airport Bus"
},
{
"source":"Madrid",
"destination":"Barcelona",
"type":"Train",
"seat":"45B",
"vehicleName":"78A"
},
{
"source":"Gerona Airport",
"destination":"Stockholm",
"type":"Flight",
"seat":"3A",
"vehicleName":"SK455",
"boardingGate":"45B",
"baggageDropCounter":"344"
}
]</textarea>
</form>
<br>
<button type="button" onclick="postAndUpdate()">Get itinerary from JSON</button>
<br>
<br>
<p id="output"></p>
<script>
function postAjax(url, data, success) {
var params = typeof data == 'string' ? data : Object.keys(data).map(
function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }
).join('&');
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.open('POST', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) { success(xhr.responseText); }
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(params);
return xhr;
}
function postAndUpdate(){
var json = document.forms["form"].elements[0].value;
postAjax('http://transportapi-env.984bzp88yp.us-east-2.elasticbeanstalk.com/itinerary',json, function(data){ console.log(data); document.getElementById("output").innerHTML = data;});
}
postAndUpdate();
</script>
</body>
</html>