js封装 ajax
/**
* Created by Administrator on 2017/3/28.
* method 提交方法
* url 地址
* param 请求传过去的参数
* callback 回调函数
* async 是否为同步异步
*
*/
/*
* 1
var ajax=function(inValue){
var xhr;
var type=(inValue.type==null||inValue.type==""||typeof (inValue.type)=="undefined") ? "get":inValue.type;
var url="/"+inValue.url;
var async=(inValue.async==null||inValue.async==""||typeof (inValue.async)=="undefined") ? "true":inValue.async;
if(window.XMLHttpRequest){//DOM
xhr=new XMLHttpRequest();
}else {//IE
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}
console.log(type);
var str="";
for(var key in inValue.data){
str+=key +"="+inValue.data[key]+"&";
}
str=str.slice(0,-1);
if(type=="get"){
console.log(str);
xhr.open(type,url+"?"+str,async);
xhr.send(null);
}else if(type=="post"){
xhr.open(type,url,async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(str);
}
xhr.onreadystatechange=function (){
if(xhr.readyState==4 && xhr.status==200){
inValue.callback(xhr.responseText);
}
}
};
*/
/*
* 2
(function(){
function createXhr(){
var xhr;
if(window.XMLHttpRequest){//DOM
xhr=new XMLHttpRequest();
}else {//IE
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}
return xhr;
}
function ajax(inValue){
var xhr=createXhr();
var type=(inValue.type==null||inValue.type==""||typeof (inValue.type)=="undefined") ? "get":inValue.type;
var url="/"+inValue.url;
var async=(inValue.async==null||inValue.async==""||typeof (inValue.async)=="undefined") ? "true":inValue.async;
var str="";
for(var key in inValue.data){
str+=key +"="+inValue.data[key]+"&";
}
str=str.slice(0,-1);
if(type=="get"){
console.log(str);
xhr.open(type,url+"?"+str,async);
xhr.send(null);
}else if(type=="post"){
xhr.open(type,url,async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(str);
}
xhr.onreadystatechange=function (){
if(xhr.readyState==4 && xhr.status==200){
inValue.callback(xhr.responseText);
}
}
}
window.ajax=ajax;
})();
*/
//* 3
(function (){
var $={
createXhr:function(){
var xhr;
if(window.XMLHttpRequest){//DOM
xhr=new XMLHttpRequest();
}else {//IE
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}
return xhr;
},
ajax:function(inValue){
var xhr=$.createXhr();
var type=(inValue.type==null||inValue.type==""||typeof (inValue.type)=="undefined") ? "get":inValue.type;
var url="/"+inValue.url;
var async=(inValue.async==null||inValue.async==""||typeof (inValue.async)=="undefined") ? "true":inValue.async;
var str="";
for(var key in inValue.data){
str+=key +"="+inValue.data[key]+"&";
}
str=str.slice(0,-1);
if(type=="get"){
console.log(str);
xhr.open(type,url+"?"+str,async);
xhr.send(null);
}else if(type=="post"){
xhr.open(type,url,async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(str);
}
xhr.onreadystatechange=function (){
if(xhr.readyState==4 && xhr.status==200){
inValue.callback(xhr.responseText);
}
}
}
}
window.$=$;
})();
/*
* 4
var $=function(){
function createXhr(){
var xhr;
if(window.XMLHttpRequest){//DOM
xhr=new XMLHttpRequest();
}else {//IE
xhr=new ActiveXObject('Microsoft.XMLHTTP');
}
return xhr;
}
function ajax(inValue){
var xhr=createXhr();
var type=(inValue.type==null||inValue.type==""||typeof (inValue.type)=="undefined") ? "get":inValue.type;
var url="/"+inValue.url;
var async=(inValue.async==null||inValue.async==""||typeof (inValue.async)=="undefined") ? "true":inValue.async;
var str="";
for(var key in inValue.data){
str+=key +"="+inValue.data[key]+"&";
}
str=str.slice(0,-1);
if(type=="get"){
console.log(str);
xhr.open(type,url+"?"+str,async);
xhr.send(null);
}else if(type=="post"){
xhr.open(type,url,async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(str);
}
xhr.onreadystatechange=function (){
if(xhr.readyState==4 && xhr.status==200){
inValue.callback(xhr.responseText);
}
}
}
return {ajax:ajax};
}();
*/
浙公网安备 33010602011771号