function ajax(opt) {
var xhr = null;
var param = Change(opt);
// 兼容
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject('Micosoft.XMLHttp');
}
// 连接判断 get--post
if (opt.type == "GET") {
xhr.open(GET, opt.url + '?' + param, opt.async);
xhr.send(null);
} else if (opt.type == "POST") {
xhr.open(POST, opt.url, opt.async);
xhr.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
xhr.send(param);
}
// 状态判断 并发送
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
opt.success(xhr.responseText);
}
}
// 数据处理
function Change(data) {
var arr = [];
for (var p in data) {
arr.push(p+ "=" + data[p]);
}
return arr.join("&");
}
}