本文整理汇总了TypeScript中xmlhttprequest.XMLHttpRequest类的典型用法代码示例。如果您正苦于以下问题:TypeScript XMLHttpRequest类的具体用法?TypeScript XMLHttpRequest怎么用?TypeScript XMLHttpRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了XMLHttpRequest类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: dashboardServerReset
private dashboardServerReset(req: any, res: any) {
var sessionid = req.params.sessionid;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
res.send("Done.");
}
}
}
xhr.open("GET", "http://" + req.headers.host + "/api/reset/" + sessionid);
xhr.send();
}
示例2: return
return (req: any, res: any) => {
var sessionid = req.params.sessionid;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
res.send("Done.");
}
}
}
xhr.open("GET", "http://" + req.headers.host + this.baseURLConfig.baseURL + "/api/reset/" + sessionid);
xhr.send();
}
示例3: getInfo
getInfo(stopcode:number):string {
try {
var XMLHttpRequest:any = require("xmlhttprequest").XMLHttpRequest;
// request('http://www.google.com', function (error, response, body) {
// var request = require('google');
// if (!error && response.statusCode == 200) {
// console.log(body) // Show the HTML for the Google homepage.
// }
// });
var id = this.busStop.getID(stopcode);
var url:string = `https://api.tfl.gov.uk/StopPoint/${id}/Arrivals?app_id=&app_key=`;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", url, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
} catch(error) {
console.log(error);
return "Error: attempts to acquire information failed";
}
}
示例4: getNearestTwoBusStops
getNearestTwoBusStops(postcode:string):number[] {
var XMLHttpRequest:any = require("xmlhttprequest").XMLHttpRequest;
var url:string = "http://api.postcodes.io/postcodes/"+postcode;
try {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
var postcodeInfo:any = JSON.parse(xmlHttp.responseText);
var eastings:number = postcodeInfo.result.eastings;
var northings:number = postcodeInfo.result.northings;
var stopcodes:number[] = [NaN,NaN];
var bestDistance:number = NaN;
var secondBestDistance:number = NaN;
this.busStops.forEach(stop => {
var info:string[] = stop.split(",");
var stopEasting:number = parseInt(info[4]);
var stopNorthing:number = parseInt(info[5]);
var distanceSquared:number = (eastings - stopEasting)*(eastings-stopEasting) + (northings - stopNorthing)*(northings - stopNorthing);
if(!isNaN(distanceSquared) && (isNaN(bestDistance) || distanceSquared < bestDistance)) {
secondBestDistance = bestDistance;
bestDistance = distanceSquared;
stopcodes[1] = stopcodes[0];
stopcodes[0] = parseInt(info[1]);
} else {
if(isNaN(secondBestDistance) || distanceSquared < secondBestDistance) {
secondBestDistance = distanceSquared;
stopcodes[1] = parseInt(info[1]);
}
}
});
if(isNaN(stopcodes[0])|| isNaN(stopcodes[1]))return null;
return stopcodes;
} catch (error) {
return null;
}
}
示例5: Promise
return new Promise(function(resolve, reject) {
let request = new XMLHttpRequest();
let timer: any = null;
timer = setTimeout(() => {
if (timer == null) { return; }
timer = null;
reject(new Error('timeout'));
setTimeout(() => {
request.abort();
}, 0);
}, timeout);
let cancelTimeout = () => {
if (timer == null) { return; }
clearTimeout(timer);
timer = null;
}
if (json) {
request.open('POST', url, true);
headers['content-type'] = { key: 'Content-Type', value: 'application/json' };
} else {
request.open('GET', url, true);
}
Object.keys(headers).forEach((key) => {
let header = headers[key];
request.setRequestHeader(header.key, header.value);
});
request.onreadystatechange = function() {
if (request.readyState !== 4) { return; }
if (request.status != 200) {
cancelTimeout();
// @TODO: not any!
let error: any = new Error('invalid response - ' + request.status);
error.statusCode = request.status;
if (request.responseText) {
error.responseText = request.responseText;
}
reject(error);
return;
}
let result: any = null;
try {
result = JSON.parse(request.responseText);
} catch (error) {
cancelTimeout();
// @TODO: not any!
let jsonError: any = new Error('invalid json response');
jsonError.orginialError = error;
jsonError.responseText = request.responseText;
if (json != null) {
jsonError.requestBody = json;
}
jsonError.url = url;
reject(jsonError);
return;
}
if (processFunc) {
try {
result = processFunc(result);
} catch (error) {
cancelTimeout();
error.url = url;
error.body = json;
error.responseText = request.responseText;
reject(error);
return;
}
}
cancelTimeout();
resolve(result);
};
request.onerror = function(error) {
cancelTimeout();
reject(error);
}
try {
if (json != null) {
request.send(json);
} else {
request.send();
}
} catch (error) {
cancelTimeout();
// @TODO: not any!
let connectionError: any = new Error('connection error');
connectionError.error = error;
reject(connectionError);
}
//.........这里部分代码省略.........
示例6: setTimeout
setTimeout(() => {
request.abort();
}, 0);
示例7:
Object.keys(headers).forEach((key) => {
let header = headers[key];
request.setRequestHeader(header.key, header.value);
});