52,797
社区成员
发帖
与我相关
我的任务
分享
/*$ajaxFun封装
* @Function sDomain 同域ajax $ajaxFun封装.sDomain({...})
* @Function cDomain 跨域ajax $ajaxFun封装.cDomain({...}) url返回json格式的js对象:jsonp
* @param {Object} options 参数集
* @param {String} url 链接
* @param {String} type 传参方式 'POST' or 'GET'(默认)
* @param {Bool} async 是否异步 true异步(默认) false同步
* @param {String} dataType 返回数据类型 'html'(默认) 'xml' 'json'
* @param {Function} beforeSend 发送请求前调用函数
* @param {Function} success 请求成功后回调函数
* @param {Function} complete 请求完成后回调函数(不管成功与否)
* @param {Bool} cache 读取缓存 true(默认) or false
*/
var $ajaxFun = function(win){
var ajaxops={
url:'',
type:'GET',
async:true,
dataType:'html',
data:null,
beforeSend:null,
success:function(){},
error:function(){},
complete:null,
cache:true
}
var isFF = navigator.userAgent.indexOf('Firefox')>0,
//isIE = /*@cc_on!@*/!1,
ie678 = !-[1,],
opera = win.opera,
doc = win.document,
head = doc.getElementsByTagName('head')[0];
var createXMLHttpRequest = function(){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}catch (e){
try{
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}catch (e){
alert('您的浏览器不支持AJAX!');
return false;
}
}
}
return xmlHttp;
}
var request = function(xmlHttp,sops){
if(( xmlHttp.status >= 200 && xmlHttp.status < 300 ) || xmlHttp.status === 304 || xmlHttp.status === 1223 || xmlHttp.status === 0){
var msg;
switch(sops.dataType){
case 'html': /*返回html*/
msg=xmlHttp.responseText;
break;
case 'xml': /*返回xml*/
msg=xmlHttp.responseXML;
break;
case 'json': /*返回json格式字符串,通过new Function()转换为json对象*/
msg=xmlHttp.responseText;
msg=(new Function('return '+msg))();
break;
default: /*其余情况视返回html*/
msg=xmlHttp.responseText;
break;
}
sops.success(msg);
}else{
sops.error();
}
}
/*同域ajax,通过使用XMLHttpRequest*/
var sAjax = function(options){
var sops=extendJson({},ajaxops,options);
if(sops.url){
var xmlHttp = createXMLHttpRequest(),
requestDone = false;
if(!xmlHttp){return false;}
var hasPara = /\?/.test(sops.url);
if(sops.type.toUpperCase() == 'GET' && sops.data){
sops.url += hasPara ? "&" : "?" + sops.data;
sops.data = null;
}
if(!sops.cache){sops.url += /\?/.test(sops.url) ? "&ts=" : "?ts=" + (new Date).getTime();}
if(!sops.async && isFF){
/*同步ajax,FF不支持onreadystatechange,使用onload来解决*/
xmlHttp.onload = function(){
request(xmlHttp,sops);
if(sops.complete && !requestDone){
sops.complete();
requestDone=true;
}
}
}else{
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState === 4){
request(xmlHttp,sops);
if(sops.complete && !requestDone){
sops.complete();
requestDone = true;
}
}
}
}
if(sops.beforeSend){
sops.beforeSend();
}
xmlHttp.open(sops.type,sops.url,sops.async);
if(sops.type.toUpperCase() == 'POST'){
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;');
}
xmlHttp.send(sops.data);
}
}
/*跨域ajax,通过使用script,返回一个json格式的js对象jsonp*/
var cAjax = function(options){
var cops = extendJson({},ajaxops,options),
hasPara = /\?/.test(cops.url),
done = false;
if(cops.type.toUpperCase() == 'GET' && cops.data){
cops.url += hasPara ? "&" : "?" + cops.data;
}
if(!cops.cache){cops.url += hasPara ? "&ts=" : "?ts=" + (new Date).getTime();}
var script = doc.createElement('script');
var callback = function(isSucc){
if(isSucc){
if(typeof jsonp != 'undefined'){
done = true;
cops.success(jsonp);
delete jsonp;
}else{
cops.error();
}
}else{
cops.error();
}
// Handle memory leak in IE
script.onload = script.onerror = script.onreadystatechange = null;
if( head && script.parentNode ){
head.removeChild(script);
}
}
if(ie678){
script.onreadystatechange = function(){
var readyState = this.readyState;
if(!done && (readyState == 'loaded' || readyState == 'complete')){
callback(true);
}
}
}else{
script.onload = function(){
callback(true);
}
script.onerror = function(){
callback();
}
if(opera){
setTimeout(function(){
if(!done){
callback();
}
}, 5000);
}
}
script.src = cops.url;
head.insertBefore(script, head.firstChild);
}
return {sDomain:sAjax,cDomain:cAjax};
}(this)