forked from tdxmotc/SampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.js
More file actions
59 lines (51 loc) · 1.8 KB
/
Copy pathSample.js
File metadata and controls
59 lines (51 loc) · 1.8 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
$(function () {
GetAuthorizationHeader();
GetApiResponse();
});
function GetAuthorizationHeader() {
const parameter = {
grant_type:"client_credentials",
client_id: "XXXXXXXXXX-XXXXXXXX-XXXX-XXXX",
client_secret: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
};
let auth_url = "https://tdx.transportdata.tw/auth/realms/TDXConnect/protocol/openid-connect/token";
$.ajax({
type: "POST",
url: auth_url,
headers: {
"Accept-Encoding": "br,gzip",
},
crossDomain:true,
dataType:'JSON',
data: parameter,
async: false,
success: function(data){
$("#accesstoken").text(JSON.stringify(data));
},
error: function (xhr, textStatus, thrownError) {
}
});
}
function GetApiResponse(){
let accesstokenStr = $("#accesstoken").text();
let accesstoken = JSON.parse(accesstokenStr);
if(accesstoken !=undefined){
$.ajax({
type: 'GET',
url: 'https://tdx.transportdata.tw/api/basic/v2/Rail/TRA/LiveTrainDelay?$top=30&$format=JSON',
headers: {
"authorization": "Bearer " + accesstoken.access_token,
"Accept-Encoding": "br,gzip",
},
async: false,
success: function (Data) {
$('#apireponse').text(JSON.stringify(Data));
console.log('Data', Data);
},
error: function (xhr, textStatus, thrownError) {
console.log('errorStatus:',textStatus);
console.log('Error:',thrownError);
}
});
}
}