Typescript 用接口模拟ajax请求


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'
})

 

 运行结果:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM