Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
45 righe
1.8 KiB
45 righe
1.8 KiB
String.prototype.ununderscore = function () { return this.split('').map(char => char == '_' ? ' ' : char).join(''); }; |
|
|
|
var makeGetRequest = (url, options) => { |
|
const headers = typeof options == 'object' ? options.headers : undefined; |
|
const identifier = typeof options == 'object' ? options.identifier : undefined; |
|
const request = new XMLHttpRequest(); |
|
return new Promise((resolve, reject) => { |
|
request.onreadystatechange = () => { |
|
if (request.readyState !== 4) return; |
|
if (request.status == 200) { |
|
let response = request.responseText; |
|
if (request.getResponseHeader('content-type').split('json').length == 2) { |
|
try { |
|
response = JSON.parse(request.responseText); |
|
} |
|
catch (error) { |
|
reject({ |
|
url: url, |
|
status: 400, |
|
statusText: error.message, |
|
raw_response: request.responseText, |
|
}); |
|
} |
|
} |
|
identifier == undefined || (response = {identifier: identifier, response: response}); |
|
resolve(response); |
|
} else { |
|
reject({ |
|
url: url, |
|
status: request.status, |
|
statusText: request.statusText |
|
}); |
|
} |
|
}; |
|
request.open('GET', url); |
|
if (typeof headers == 'object') { |
|
for (const header in headers) { |
|
if (headers.hasOwnProperty(header)) { |
|
request.setRequestHeader(header, headers[header]); |
|
} |
|
} |
|
} |
|
request.send(); |
|
}); |
|
};
|
|
|