TypeScript 패키지 원생 Ajax

TypeScript 패키지 원생 Ajax, CRUD 대응 post, delete, put, get 실제 열 거 는 다음 과 같다.
//1.   ajax           
interface IAjaxConfig { 
    type: string;
    url: string;
    data?: string;
    dataType: string;
} 

//2.   CRUD        
//1.   ajax           
interface IAjaxConfig { 
    type: string;
    url: string;
    data?: string;
    dataType: string;
} 
 
//2.   CRUD        
export abstract class TsAjax { 
    abstract _post(url: string, data?:string): any;
    abstract _put(url: string, data?:string): any;
    abstract _delete(url: string, data?:string): any;
    abstract _get(url: string, data?:string): any;
}
 
//3.               ,     ajax [CRUD]
export class Ajax extends TsAjax{
    //  js   ajax 
    private doAjax(config: IAjaxConfig):any {
        let result: any = "";
        let xhr = new XMLHttpRequest();
 
        if (xhr == null) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");  /*      Internet Explorer (IE5   IE6)   ActiveX   : */
        } 
 
        if (xhr != null) {
            xhr.open(config.type, config.url, true);
            xhr.send(config.data);
            xhr.onreadystatechange = function () {
                console.log(xhr.statusText);
                let data = xhr.responseText;
                if (xhr.readyState == 4 && xhr.status == 200) {
                    if (config.dataType == 'json') {
                        result = JSON.parse(data);
                    } else {
                        result = data;
                    }
                } else { 
                    result = data; //error
                }
            }
        } else { 
            result = "Your browser does not support XMLHTTP.";
        }
        console.log(result);
        return result;
    }
 
    _post(url: string, data?:string): any { 
        return this.doAjax({
            type:'post',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }
 
    _delete(url: string, data?:string): any { 
        return this.doAjax({
            type:'delete',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }
 
    _put(url: string, data?:string): any { 
        return this.doAjax({
            type:'put',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }
 
    _get(url: string, data?:string): any { 
        return this.doAjax({
            type:'get',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }
}
 
//4.  
let myAjax = new Ajax(); 
let d = myAjax._get("http://localhost:62902/api/values");abstract class TsAjax { 
    abstract _post(url: string, data?:string): any;
    abstract _put(url: string, data?:string): any;
    abstract _delete(url: string, data?:string): any;
    abstract _get(url: string, data?:string): any;
}

//3.               ,     ajax [CRUD]
export class Ajax extends TsAjax{
    //  js   ajax 
    private doAjax(config: IAjaxConfig):any {
        let result: any = "";
        let xhr = new XMLHttpRequest();

        if (xhr == null) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");  /*      Internet Explorer (IE5   IE6)   ActiveX   : */
        } 

        if (xhr != null) {
            xhr.open(config.type, config.url, true);
            xhr.send(config.data);
            xhr.onreadystatechange = function () {
                console.log(xhr.statusText);
                let data = xhr.responseText;
                if (xhr.readyState == 4 && xhr.status == 200) {
                    if (config.dataType == 'json') {
                        result = JSON.parse(data);
                    } else {
                        result = data;
                    }
                } else { 
                    result = data; //error
                }
            }
        } else { 
            result = "Your browser does not support XMLHTTP.";
        }
        console.log(result);
        return result;
    }

    _post(url: string, data?:string): any { 
        return this.doAjax({
            type:'post',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }

    _delete(url: string, data?:string): any { 
        return this.doAjax({
            type:'delete',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }

    _put(url: string, data?:string): any { 
        return this.doAjax({
            type:'put',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }

    _get(url: string, data?:string): any { 
        return this.doAjax({
            type:'get',
            data:data,
            url:url, //api
            dataType:'json'
        });
    }
}

//4.  
let myAjax = new Ajax(); 
let d = myAjax._get("http://localhost:62902/api/values");

좋은 웹페이지 즐겨찾기