malong1992

Typescript 用接口模拟ajax请求

interface Config {
    type:string;
    url:string;
    data?:string;
    dataType:string
}

function ajax(config:Config){

var xhr = new XMLHttpRequest()
xhr.open(config.type,config.url,true)
xhr.send(config.data);
xhr.onreadystatechange =function(){
    if(xhr.readyState==4 && xhr.status==200){
        console.log(\'success\')
    }
    else{
        console.log(xhr.responseText)
    }
}

}
ajax({
    type:\'get\',
    url:\'www.baidu.com\',
    data:\'name:zhangsna\',
    dataType:\'json\'
})

 

 运行结果:

分类:

技术点:

相关文章: