搜索
系统检测到您的用户名不符合规范:

复杂的ajax 原生js实现

浏览:4306 发布日期:2013年07月19日 分类:功能实现 关键字: javascript js
复杂的ajax实现
复杂的原生js ajax实现,加入了时间超时,超时处理模仿百度的js写的/**
* 复杂的ajax封装
* @version 1.0
*
* 用法
* var xmlhttp = new YAjax();
* xmlhttp.request({
* url : "./demo.php", // get请求时 可以这样写 "./demo.php?name=zhangsan"
* method : "POST",
* data : "name=李四", // 支持json传值 {"name":"zhangsan"} get时不用该参数
* receiveType : "html", // json html or xml
* timeout : 3000, // 3秒
* success : function(d) {alert(d);},
* error : function(xmlhttp){alert('timeout');}
* });
*
*/
function YAjax() {
this._self = this;
this.xmlhttp = this.init();
}

YAjax.prototype = {
constructor : YAjax,

// 初始化xmlhttpRequest
init : function() {
var xmlhttp = null;

// 针对不同浏览器建立这个对象的不同方式写不同代码
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
//针对某些特定版本的Mozillar浏览器的BUG进行修正
if(xmlhttp.overrideMimeType) {
xmlhttp.overrideMimeType("text/xml");
}

} else if (window.ActiveXObject) {
var activexName = ['MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
for (var i=0; i<activexName.length; i++) {
try {
xmlhttp = new ActiveXObject(activexName[i]);
break;
} catch(e) {}
}
}

return xmlhttp;
},

extend : function(destination, source, override) {
if(undefined == override) override = true;
if(typeof destination != "object" && typeof destination != "function") {
if(!override)
return destination;
else
destination = {};
}
var property = '';
for(property in source) {
if(override || !(property in destination)) {
destination[property] = source[property];
}
}

return destination;
},

// json to string {name: 'lisi', age: 10} --> name=lisi&age=10
json2String : function(jsonData) {
var strArr = [];
for(var k in jsonData) {
strArr.push(k + "=" + jsonData[k]);
}

return strArr.join("&");
},

// 发送http 请求
request : function(opt) {
var _self = this,
isTimeout = false,
timeFlag = 0,
options = {
url : "", // string
data : "", // json or string
method : "POST",
receiveType : "html", // html json or xml
timeout : 7000,
async : true,
success : function(){alert("define your success function");},
error : function(xmlhttp){}
};
if("data" in opt) {
if(typeof opt.data == "string"){} else {opt.data = this.json2String(opt.data); }
}
options = this.extend(options, opt);

this.xmlhttp.onreadystatechange = function(){
if(_self.xmlhttp.readyState == 4) {
if(!isTimeout && _self.xmlhttp.status == 200) {
clearTimeout(timeFlag);
var t = options.receiveType.toLowerCase();
if(t == "html") {
options.success(_self.xmlhttp.responseText);

} else if(t == "xml") {
options.success(_self.xmlhttp.responseXML);

} else if(t == 'json') {
try {
var obj = JSON.parse(_self.xmlhttp.responseText);
options.success(obj);
} catch(e) {
var str = '(' + _self.xmlhttp.responseText + ')'; //json字符串
options.success(eval(str));
}
} else {}

} else {
clearTimeout(timeFlag);
options.error(_self.xmlhttp);
}
}
};

timeFlag = setTimeout(function(){
if(_self.xmlhttp.readyState != 4) {
isTimeout = true;
_self.xmlhttp.abort();
clearTimeout(timeFlag);
}
}, options.timeout);

this.xmlhttp.open(options.method.toUpperCase(), options.url, options.async); //打开与服务器连接
if(options.method.toUpperCase() == "POST") {
this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //post方式要设置请求类型
this.xmlhttp.send(options.data); //发送内容到服务器

} else {
this.xmlhttp.send(null);
}
}
};
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
nong_zai
积分:2716 等级:LV3
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /