http Post request with Typescript

前端 未结 3 624
旧巷少年郎
旧巷少年郎 2021-02-19 02:15

I am trying to find an example of HTTP post request in Typescript but can only find examples that use Angular. Could someone point me in the right direction to find this or post

3条回答
  •  天命终不由人
    2021-02-19 03:01

    Here is my very simple example to call GET or POST with Typescript only.

    //-------------------------------------------------
    // Simple function to GET or POST
    function httpCall(method: string, url:string, data:any, callback:(result:any)=>any) {
        var xhr = new XMLHttpRequest();
        xhr.open(method, url, true);
        if (callback) xhr.onload = function() { callback(JSON.parse(this['responseText'])); };
        if (data != null) {
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify(data));
        }
        else xhr.send();
    }
    

    Optional input data (the post body) and callback. The data and result are both assumed to be JSON.

提交回复
热议问题