ShuDudu's Home was started in 2011, but the web data is lost, so now begin again, I would like to make some friends, I hope you like ShuDudu's home.
Current position: ShuDudu > Life >

[Abstract] An Ajax.Request class

Saturday on May 26th, 2007Life

purpose: because some modules in the blog program need to use ajax, directly use prototype.js is relatively large (more than 40 k), and only use the ajax function, so in order to reduce the burden of download, can not change the code that has been written in the prototype.js framework, can only be in accordance with the style of prototype, write an ajax class, to achieve zero cost migration framework.

ajax class is as follows, prototype.js:

 var Ajax = {xmlhttp:function () {Try {Return new ActiveXObject ('Msxml2.XMLHTTP')} catch (e) {Try {Return new ActiveXObject ('Microsoft.XMLHTTP')} catch (e) {Return new XMLHttpRequest ()}}}}Ajax.Request = function () {If (arguments.length<2) returnVar _ p = {asynchronous:true,method:"GET",parameters:""};//default optionFor (var key in arguments [1]) {//custom option overwrite default option_ p [key] = arguments [1] [key]}Var _ x = Ajax.xmlhttp ();//xml objVar _ url = arguments [0];//strIf (_ p [& quot;parameters"] .length & gt;0) _ p [& quot;parameters"] + ='& _='If (_ p [& quot;method"] .toUpperCase () = & quot;GET") _ url + = (_ url.match (/\?/)?'& ':'?) + _ p [& quot;parameters"]_ x.open (_ p [& quot;method"], _ url,_p [& quot;asynchronous"])_ x.onreadystatechange = function () {If (_ x.readyState==4) {If (_ x.status==200) {_ p [& quot;onComplete"]? _ p [& quot;onComplete"] (_ x): & quot;"} else {_ p [& quot;onError"]? _ p [& quot;onError"] (_ x): & quot;"}}}If (_ p [& quot;method"] .toUpperCase () = & quot;POST") _ x.setRequestHeader (& quot;Content-Type","application/x-www-form-urlencoded")_ x.send (_ p [& quot;method"] .toUpperCase () = = & quot;POST"? _ p [& quot;parameters"]: null)}

call method:

 var myAjax = new Ajax.Request (& quot; http://localhost/abc.asp",{Method:"post"Parameters:"demo=123456789abc"OnComplete:function (xmlhttp) {Alert (xmlhttp.responseText)}});The style of 

calls is exactly the same as before!

at present, there are only two callback functions in this new class: onComplete and onError,Ajax have only one method, Request. After all, blog programs do not need so many applications. The parameters attribute has a default value: {asynchronous:true,method: "GET", parameters: ""}, from which you can see that if asynchronous, method, and parameters are not passed in during the call, the class will use the default value.
-this article is from: happyshow-

Copyright Protection: ShuDudu from the original article, reproduced Please keep the link: https://www.shududu.com/life/Abstract-An-Ajax-Request-class.htm