公司涂总封装的一个ajax类:
功能类似prototype.js的ajax类
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>无标题文档</title>
</head>
<body>
<input type=”button” onclick=”bq()” value=”方法一:无参数” /><br /><br /><input type=”button” onclick=”bggg(3667,0,0)” value=”方法二:有参数” />
<script type=”text/javascript”>
//————————方法一:无参数—————————//
function bq()
{
var url=”http://ting.5fad.com/AJAX/getSongInfo.aspx?songid=3667&count=0&flag=0&rnd=”+Math.random();
ajax_obj.get(url, callback,true);
}
//————————方法二:有参数—————————//
function bggg(sid,con,flg)
{
var url=”http://ting.5fad.com/AJAX/getSongInfo.aspx“;
var parm=”songid=”+sid+”&count=”+con+”&flag=”+flg;
ajax_obj.get(url+”?”+parm,callback,true);
}
function callback(datafromServer){
alert(datafromServer);
}
</script>
<script type=”text/javascript”>
/////////////////begin:XAjax/////////////////////////////////
if (ajax_obj == null) var ajax_obj = new XAjax();
function XAjax()
{
// 默认出错处理
this._eh = XAjax._error;
}
XAjax.debug_enable = false;
// GET 请求
//(URL, 回调函数[, 回调函数附加数据, 是否异步])
XAjax.prototype.get = function (url, callback, asyn)
{
asyn = (asyn === undefined)? true: asyn;
var _self = this;
var X = XAjax._object();
if(asyn)
X.onreadystatechange = function()
{ XAjax._callback(X, callback, _self); };
X.open(‘GET’, url, asyn);
if(XAjax.debug_enable)
XAjax._debugger(callback);
X.send(null);
if(asyn){
return X;
}else{
XAjax._callback(X, callback, _self);
}
}
// POST 请求
//(URL, POST数据, 回调函数[, 回调函数附加数据, 是否异步])
XAjax.prototype.post = function (url, sdata, callback, asyn)
{
asyn = (asyn === undefined)? true: asyn;
var _self = this;
var X = XAjax._object();
if(asyn)
X.onreadystatechange = function()
{ XAjax._callback(X, callback, _self); };
X.open(‘POST’, url, asyn);
if(XAjax.debug_enable)
XAjax._debugger(callback);
X.setRequestHeader(‘Content-length’, sdata.length);
X.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);
X.send(sdata);
if(asyn){
return X;
}else{
XAjax._callback(X, callback, _self);
}
}
// 设置出错处理函数
XAjax.prototype.e_handler = function (func)
{
if(func !== undefined) this._eh = func;
}
// 创建一个兼容的XHR对象。
// 改了一下:IE7中优先采用Native XHR
XAjax._object = function()
{
var A;
if(typeof XMLHttpRequest != ‘undefined’) {
A = new XMLHttpRequest();
}else{
var _msxmlhttp = new Array(
‘Msxml2.XMLHTTP.6.0′,
‘Msxml2.XMLHTTP.3.0′,
‘Msxml2.XMLHTTP’,
‘Microsoft.XMLHTTP’);
for(var i = 0; i < _msxmlhttp.length; i++) {
try {
if(A = new ActiveXObject(_msxmlhttp[i])) break;
} catch (e) {
A = null;
}
}
}
if(!A)
alert(“Could not create connection object.”);
return A;
}
//– 私有函数 —————————————————————–
XAjax._callback = function (req, callback, obj)
{
if(req.readyState == 4) {
if(req.status != 200) {
//req.onreadystatechange = null;
if(obj._eh) obj._eh(req, callback);
}else{
callback(req.responseText);
//req.onreadystatechange = null;
}
}
}
// Debug: 显示采用的回调函数。
XAjax._debugger = function (func)
{
alert(‘running: ‘ + XAjax._fname(func));
}
// 默认的出错处理
XAjax._error = function (req, callback)
{
alert(req.statusText + ‘\nShould run: ‘ + XAjax._fname(callback));
}
// 提取函数名(含参数)
XAjax._fname = function (func)
{
var S = func.toString();
return S.slice(9, S.indexOf(‘)’, 10)) + ‘)’;
}
/////////////////end:XAjax/////////////////////////////////////////////////////////////
</script>
</body>
</html>
- 本文固定链接: http://www.css88.com/archives/169
- 转载请注明: 愚人码头 于 WEB前端开发 发表