43 lines
848 B
TypeScript
43 lines
848 B
TypeScript
export class Request
|
|
{
|
|
static post<I,O>( url:string, input:I ):Promise<O>
|
|
{
|
|
let promise = new Promise<O>
|
|
(
|
|
( resolve, reject ) =>
|
|
{
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
console.log( "post", url, ">>", input);
|
|
xhr.open( "POST", url, true );
|
|
|
|
xhr.responseType = "text";
|
|
|
|
xhr.onload=
|
|
() =>
|
|
{
|
|
console.log( xhr.responseURL, xhr.responseText );
|
|
|
|
if ( xhr.status !== 200 || xhr.responseText.startsWith( "ERROR:" ) )
|
|
{
|
|
reject( xhr.responseText )
|
|
}
|
|
else
|
|
{
|
|
resolve( JSON.parse( xhr.responseText ) as O );
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror=(e)=>
|
|
{
|
|
reject( e );
|
|
}
|
|
|
|
xhr.send( JSON.stringify( input ) );
|
|
}
|
|
);
|
|
|
|
return promise;
|
|
}
|
|
} |