一、定义Ajax请求数据的接口,ts封装ajax
// 1.定义接口约束interface Config { type: string; url: string; data?: string; // 可传可不传 dataType: string;}// 2.原生js封装的ajax 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) { if (config.dataType == 'json') { console.log(JSON.parse(xhr.responseText)); } else { console.log(xhr.responseText) } } }}ajax({ type: 'get', data: 'name=zhangsan', url: 'http://a.itying.com/api/productlist', dataType: 'json'})