Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write an Ajax.Request class

Shulou Source: shulou.com Published: 2022-06-03 06:53:44 11月15日 Update

Editor to share with you how to write an Ajax.Request class, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

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, and can not change the code has been written in the prototype.js framework, can only be in accordance with the style of prototype, write their own ajax class, to achieve zero cost migration framework.

The new ajax class is as follows:

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.length0) _ p ["parameters"] + ='& _ ='

If (_ p ["method"] .toUpperCase () = = "GET") _ url + = (_ url.match (/\? /)?'&':'?) + _ p ["parameters"]

_ x.open (_ p ["method"], _ url,_p ["asynchronous"])

_ x.onreadystatechange = function () {

If (_ x.readyState==4) {

If (_ x.status==200) {

_ p ["onComplete"]? _ p ["onComplete"] (_ x): ""

} else {

_ p ["onError"]? _ p ["onError"] (_ x): ""

}

}

}

If (_ p ["method"] .toUpperCase () = = "POST") _ x.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")

_ x.send (_ p ["method"] .toUpperCase () = = "POST"? _ p ["parameters"]: null)

}

This class is saved as a js file with a volume of less than 1k, which is small enough. Ha ha.

Call method:

Var myAjax = new Ajax.Request (

"http://localhost/abc.asp",

{

Method: "post"

Parameters: "demo=123456789abc"

OnComplete:function (xmlhttp) {

Alert (xmlhttp.responseText)

}

}

);

The style of the call 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.

These are all the contents of the article "how to write an Ajax.Request Class". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Article volume content only method framework program style same three not much two from code function function parameter most attribute cost Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Shulou Technology Shulou Tech Info OPPO Reno