当前位置: 网学 > 编程文档 > Ajax > 正文

自己动手封装的 ajax

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/07/12
tate = ajax.transport.readyState;
if(readyState == 4) {
var status = ajax.transport.status, transport = ajax, json = ajax.evalJSON();
if(status == 200) {
ajax.options[''onSuccess''](transport, json);
} else {
ajax.options[''onFailure''](transport, json);
}
}
} catch (e) {}
},
setParameters: function(url, params) {
if (params && typeof params == ''string'') {
url += (url.include(''?'') ? ''&'' : ''?'') + params;
} else if (params && typeof params == ''object'') {
for(var param in params) {
url += (url.include(''?'') ? ''&'' : ''?'') + param + ''='' + params[param];
}
}
return url;
},
setRequestHeaders: function() {
var headers = {
''X-Requested-With'': ''XMLHttpRequest'',
''Accept'': ''application/xml, text/xml, text/html, text/javascript, application/javascript, application/json, text/javascript, text/plain, */*'',
''If-Modified-Since'': ''Thu, 01 Jan 1970 00:00:00 GMT''
};
this.method = ajax.options.method;
if (this.method == ''POST'') {
headers[''Content-type''] = ajax.options.contentType +
(ajax.options.encoding ? ''; charset='' + ajax.options.encoding : '''');
if (ajax.transport.overrideMimeType &&
(navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005]) < 2005) {
headers[''Connection''] = ''close'';
}
}
for (var name in headers) {
ajax.transport.setRequestHeader(name, headers[name]);
}
},
evalJSON: function() {
try {
return eval(''('' + ajax.transport.responseText + '')'');
} catch (e) {}
}
};
var Form = {
serialize: function(element) {
var elements = $(element).all;
var queryComponents = [];
for(var i = 0; i < elements.length; i++) {
var parameter = null, method = elements[i].tagName.toLowerCase();
if([''input'', ''select'', ''textarea''].include(method)) {
parameter = Form.Serializers[method](elements[i]);
}
if(parameter != null && parameter.constructor == Array) {
var key = encodeURIComponent(parameter[0]);
var value = encodeURIComponent(parameter);
queryComponents.push(key + ''='' + value);
}
}
return queryComponents.join(''&'');
},
request: function(options) {
var params = this.toQueryParams(options.parameters);
options.parameters = this.serialize(this.form);
if(params) {
options.parameters = options.parameters.concat(''&'' + params);
}
if($(this.form).method) {
options.method = $(this.form).method;
}
return new ajax.request($(this.form).action, options);
},
toQueryParams: function(params) {
var queryComponents = [];
if (params && typeof params == ''string'') {
queryComponents.push(encodeURIComponent(params));
} else if (params && typeof params == ''object'') {
for(var param in params) {
var key = encodeURIComponent(param);
var value = encodeURIComponent(params[param]);
queryComponents.push(key + ''='' + value);
}
}
return queryComponents.join(''&'');
}
};
Form.Serializers = {
input: function(element) {
switch(element.type.toLowerCase()) {
case ''checkbox'':
case ''radio'':
return this.inputSelector(element);
default:
return this.textarea(element);
}
},
inputSelector: function(element) {
if(element.checked) {
return [element.name
  • 上一篇资讯: ajax poller
  • 网学推荐

    免费论文

    原创论文

    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号