Ajax封装六步骤
1.有浏览器 支持 没有浏览器请求发送不出去 2.创建Ajax对象 3.Ajax.open(method,url,true0); 设置请求 4.ajax.send(); 发送数据 5.onreadystatechage 监听数据返回事件 6.status 数据状态
详情看代码`
function ajaxFun(method,url,data,callback,flag){ //传请求方式 url 数据 回调函数 同步false/异步true
var xhr = null;
if(window.XMLHttpRequest){
xhr = new.XMLHttpRequest(); //创建Ajax对象
}else{
xhr = new.ActiveXObject('Microsoft.XMLHttp'); //兼容ie
}
if(method = 'GET'){ //兼容‘GET’ 和‘post’
xhr.open(method,url + '?' + data,flag) ; // ( 数据传输方式 ,url地址 ,异步true/同步false) GET请求参数拼接在url后面
xhr.send(); //把数据发送
}else if(method = 'POST'){
xhr.oprn(method,url,flag);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded&
|