原生js xmlhttprequest.txt function http_post(url, data, callback) { var sp = typeof Spinner == 'function' ? new Spinner({color:'#000', lines: 12}).spin(document.body) : null; var request = new XMLHttpRequest(); //调用XMLHttpRequest对象的
function http_post(url, data, callback) { var sp = typeof Spinner == 'function' ? new Spinner({color:'#000', lines: 12}).spin(document.body) : null; var request = new XMLHttpRequest(); //调用XMLHttpRequest对象的open方法 request.open('POST', url); //调用XMLHttpRequest对象的send方法 var fd = new FormData(); for(var attr in data) { fd.append(attr,data[attr]); } request.send(fd); //为XMLHttpRequest对象添加onreadystatechange 响应函数 request.onreadystatechange = function() { //判断响应是否完成:XMLHttpRequest 对象的readystate的属性值为4的时候 if (request.readyState == 4) { sp && sp.stop(); //在判断响应是否可用:XMLHttpRequest 对象status 属性值为200 if (request.status == 200) { //响应结果 callback(request.responseText); } } } }