const formsParams = (data) => {
var arr = []
for(var prop in data){
arr.push(prop + "=" + data[prop])
}
return arr.join("&")
}
const ajax = async(options) => {
let xhr = null;
let { type, url, async = true, data } = options
type = type.toUpperCase()
let params = formsParams(data);
//创建对象
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest()
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP")
}
// 连接
if(type == "GET"){
xhr.open(type, url + "?" + params, async)
xhr.send(null)
} else if(type == "POST"){
xhr.open(type, url, async)
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
xhr.send(params)
}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
options.success(xhr.responseText)
}
}
}
// https://www.maitanbang.com/
// ajax({
// url : "https://www.maitanbang.com/api/hldh/index",
// type : "GET",
// data : {
// key: 'KnDYcFP6qMoUBC19W4sdPqYkYZ',
// coin: 'USD',
// tocoin: 'CNY',
// money: '10000'
// },
// success : function(data){
// console.log(data)
// }
// })
console