function Ajax() { var xhr =null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { xhr = new ActiveXObject("Microsoft.XMLHttp"); } this.get=function(url,success,fail){ //get请求 xhr.open("GET", "1.jsp",true); xhr.onreadystatechange=function(){ if(xhr.readyState==4) { alert(xhr.status); if(xhr.status==200) { var txt = xhr.responseText; txt = eval("("+txt+")"); var ch = txt.charAt(0); if(ch==" var xml = xhr.responseXML; success(eval("("+xml+")")); } else if(ch=="["||ch=="{") {//json类型 txt = eval("("+txt+")"); success(txt); } else {//不知道直接返回 success(txt); } } else { if(fail) { fail(xhr.status); } } } }; xhr.send(null); }; this.post = function (url,param,success,fail) {//post请求 xhr.open("POST", "1.jsp",true); xhr.onreadystatechange=function(){ if(xhr.readyState==4) { alert(xhr.status); if(xhr.status==200) { var txt = xhr.responseText; var ch = txt.charAt(0); if(ch==" var xml = xhr.responseXML; success(eval("("+xml+")")); } else if(ch=="["||ch=="{") {//json类型 txt = eval("("+txt+")"); success(txt); } else {//不知道直接返回 success(txt); } } else { if(fail) { fail(xhr.status); } } } }; xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(param); }; }
查看更多关于Ajax的小封装get,post请求的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did62142