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