aspnetcore/src/Microsoft.AspNetCore.Signal.../HttpClient.ts

35 lines
1014 B
TypeScript

class HttpClient {
get(url: string): Promise<string> {
return this.xhr("GET", url);
}
post(url: string, content: string): Promise<string> {
return this.xhr("POST", url);
}
private xhr(method: string, url: string, content?: string): Promise<string> {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.send(content);
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
}
else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = () => {
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
});
}
}