关于XMLHTTP的一个想法,希望能够得到众高手们的支持。

zz315 2005-11-24 01:00:09
详见此:http://www.iecn.net/forum/showthread.php?s=&threadid=23840
...全文
2066 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyszh 2005-12-05
  • 打赏
  • 举报
回复
这是prototype.js里的一段,封装的怎么样?
var Ajax = {
getTransport: function() {
return Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
}
}

Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) {
this.options = {
method: 'post',
asynchronous: true,
parameters: ''
}.extend(options || {});
},

responseIsSuccess: function() {
return this.transport.status == undefined
|| this.transport.status == 0
|| (this.transport.status >= 200 && this.transport.status < 300);
},

responseIsFailure: function() {
return !this.responseIsSuccess();
}
}

Ajax.Request = Class.create();
Ajax.Request.Events =
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];

Ajax.Request.prototype = (new Ajax.Base()).extend({
initialize: function(url, options) {
this.transport = Ajax.getTransport();
this.setOptions(options);
this.request(url);
},

request: function(url) {
var parameters = this.options.parameters || '';
if (parameters.length > 0) parameters += '&_=';

try {
if (this.options.method == 'get')
url += '?' + parameters;

this.transport.open(this.options.method, url,
this.options.asynchronous);

if (this.options.asynchronous) {
this.transport.onreadystatechange = this.onStateChange.bind(this);
setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
}

this.setRequestHeaders();

var body = this.options.postBody ? this.options.postBody : parameters;
this.transport.send(this.options.method == 'post' ? body : null);

} catch (e) {
}
},

setRequestHeaders: function() {
var requestHeaders =
['X-Requested-With', 'XMLHttpRequest',
'X-Prototype-Version', Prototype.Version];

if (this.options.method == 'post') {
requestHeaders.push('Content-type',
'application/x-www-form-urlencoded');

/* Force "Connection: close" for Mozilla browsers to work around
* a bug where XMLHttpReqeuest sends an incorrect Content-length
* header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType)
requestHeaders.push('Connection', 'close');
}

if (this.options.requestHeaders)
requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);

for (var i = 0; i < requestHeaders.length; i += 2)
this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
},

onStateChange: function() {
var readyState = this.transport.readyState;
if (readyState != 1)
this.respondToReadyState(this.transport.readyState);
},

respondToReadyState: function(readyState) {
var event = Ajax.Request.Events[readyState];

if (event == 'Complete')
(this.options['on' + this.transport.status]
|| this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
|| Prototype.emptyFunction)(this.transport);

(this.options['on' + event] || Prototype.emptyFunction)(this.transport);

/* Avoid memory leak in MSIE: clean up the oncomplete event handler */
if (event == 'Complete')
this.transport.onreadystatechange = Prototype.emptyFunction;
}
});

Ajax.Updater = Class.create();
Ajax.Updater.ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';

Ajax.Updater.prototype.extend(Ajax.Request.prototype).extend({
initialize: function(container, url, options) {
this.containers = {
success: container.success ? $(container.success) : $(container),
failure: container.failure ? $(container.failure) :
(container.success ? null : $(container))
}

this.transport = Ajax.getTransport();
this.setOptions(options);

var onComplete = this.options.onComplete || Prototype.emptyFunction;
this.options.onComplete = (function() {
this.updateContent();
onComplete(this.transport);
}).bind(this);

this.request(url);
},

updateContent: function() {
var receiver = this.responseIsSuccess() ?
this.containers.success : this.containers.failure;

var match = new RegExp(Ajax.Updater.ScriptFragment, 'img');
var response = this.transport.responseText.replace(match, '');
var scripts = this.transport.responseText.match(match);

if (receiver) {
if (this.options.insertion) {
new this.options.insertion(receiver, response);
} else {
receiver.innerHTML = response;
}
}

if (this.responseIsSuccess()) {
if (this.onComplete)
setTimeout((function() {this.onComplete(
this.transport)}).bind(this), 10);
}

if (this.options.evalScripts && scripts) {
match = new RegExp(Ajax.Updater.ScriptFragment, 'im');
setTimeout((function() {
for (var i = 0; i < scripts.length; i++)
eval(scripts[i].match(match)[1]);
}).bind(this), 10);
}
}
});
patchclass 2005-12-04
  • 打赏
  • 举报
回复
ajax
早就提出你的要求了:-)
jackern 2005-12-04
  • 打赏
  • 举报
回复
XMLHTTP,目前主要有哪些应用?
emu 2005-12-02
  • 打赏
  • 举报
回复
如果光是xmlhttp一个控件的话就不必包装了吧,如果吧XMLHTTP和XMLHttpRequest两个包装起来,对外提供一致的访问,倒是有点实际意义。虽然也有很多人都提供了这样的包装,但是主要都集中在包装数据的发送和接收上,对xml的一致访问还没见到。
lcllcl987 2005-12-01
  • 打赏
  • 举报
回复
ms好像已经搞了啊,ajax就是封装了xmlhttp的一个东西
awaysrain 2005-12-01
  • 打赏
  • 举报
回复
这样的东西已经有不少了,对于浏览器的兼容也很好,对于xmlhttp的封装是ajax框架的基本功能
lisoon 2005-12-01
  • 打赏
  • 举报
回复
现在有太多的成熟框架了。

http://tech.163.com/05/0815/11/1R6M3QEF00091589.html
blueoxygen 2005-12-01
  • 打赏
  • 举报
回复
重复发明轮子。
尽力楼主看看这个 http://prototype.conio.net/
然后读一下与之相关的文章,js方面,基本流行的框架都回用prototypy作为基础。
mxlmwl 2005-12-01
  • 打赏
  • 举报
回复
实际上ajax的核心技术就是xmlhttp,楼主说的那个文章本意正是如此,只是可能还不知道这个名词罢了。目前ajax的开源java框架已经有更多了。
里沃特 2005-12-01
  • 打赏
  • 举报
回复
真的不错,我也来坐坐。
fengforever 2005-12-01
  • 打赏
  • 举报
回复
ajax

atlas 是 ms 发布的一个类似的东西
http://atlas.asp.net/quickstart/default.aspx
yyszh 2005-11-30
  • 打赏
  • 举报
回复
请去看prototype.js
xxuu503 2005-11-30
  • 打赏
  • 举报
回复
AJAX??

查到最多的是阿贾克斯!

赫赫!
lanchong512 2005-11-30
  • 打赏
  • 举报
回复
瞻仰一个,好多的星星,XMLHTTP现在如此得宠,其实除了他的优点以外大家有没有想过他的缺点?真的是一个值得推广和继续发展的技术吗?
BlueDestiny 2005-11-30
  • 打赏
  • 举报
回复
如果要封装,也是考虑跨浏览器问题最多。而且xmlhttp也就那几个方法和属性,个人觉得做成一个构造兼容的xmlhttp就可以了
zz315 2005-11-30
  • 打赏
  • 举报
回复
谢谢大家的回复了。
我提的时候也并没有想太多,但我觉得能有一个简单、通用的东西终归是好的。
ysy8584 2005-11-27
  • 打赏
  • 举报
回复
居然看到两个五星的仰视.
gu1dai 2005-11-27
  • 打赏
  • 举报
回复
楼主去查过AJAX了吗?

可能你要开发的东西别人早已经有成熟的产品了。
gu1dai 2005-11-27
  • 打赏
  • 举报
回复
xmlhttp的操作非常简单,有必要封装成类吗?写成一个函数也就可以了

-------------
同意。
CnEve 2005-11-27
  • 打赏
  • 举报
回复
想法不错,先看看
加载更多回复(15)

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧