Get the App
SLTechnology News&Howtos  ›  Development  › 

The implementation code for the javascript Ajax class

Shulou Source: shulou.com Published: 2022-06-03 20:02:09 09月12日 Update

This article focuses on "javascript Ajax class implementation code", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "javascript Ajax class implementation code" it!

Compared with the current Ajax frameworks, what are the advantages and disadvantages? Let's talk about it after reading it:

1 、 Ajax.js

The copy code is as follows:

/ *

AJAX v1.4

HJF 2009-7-5

, /

Function AjaxDO () {

This.HttpRequest = null

This.openMethod = null; / / HTTP request method, which is Get, Post or Head

This.openURL = null; / / is the target URL. For security reasons, this URL can only be in the same domain, otherwise it will prompt an "no permission" error.

This.openAsync = null; / / specifies whether to continue to execute the following code while waiting for the server to return information. If False, execution does not continue until the server returns information. The default is True.

This.ProcessRequestFunction = function (_ HttpRequest) {return;} / / function entry for processing returned information

This.ProcessRequestParam = null; / / additional parameters when processing access information

This.LoadingImg = null; / / the picture being loaded, usually .gif animation

/ / initialize HttpRequest

This.InitHttpRequest = function () {

Var http

/ / try {

/ / http = new ActiveXObject ("Msxml2.XMLHTTP")

/ /} catch (e) {

/ / try {

/ / http = new ActiveXObject ("Microsoft.XMLHTTP")

/ /} catch (e) {

/ / http = false

/ /}

/ /}

Try {

If (window.ActiveXObject) {

For (var iTunes 5; I; imurf -) {

Try {

If (iTunes 2) {

Http = new ActiveXObject ("Microsoft.XMLHTTP")

} else {

Http = new ActiveXObject ("Msxml2.XMLHTTP." + I + ".0")

}

Break

} catch (e) {

/ / alert (I)

Http = false

}

}

} else if (window.XMLHttpRequest) {

Http = new XMLHttpRequest ()

If (http.overrideMimeType) {

Http.overrideMimeType ("text/xml")

}

}

} catch (e) {

Http = false

}

If (! http) {

Alert ("cannot create XMLHttpRequest object instance")

Return http

}

This.HttpRequest = http

Return http

}

/ / detect this.HttpRequest

This.checkHttpRequest = function () {

If (! this.HttpRequest) {

Return this.InitHttpRequest ()

}

Return this.HttpRequest

}

/ / modify MIME category

/ / http.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); / / if you want to transfer files or Post content to the server

/ / http.setRequestHeader ("Content-Type", "text/xml")

/ / http.setRequestHeader ("Content-Type", "gb2312")

This.setRequestHeader = function (mime) {

If (! this.checkHttpRequest ()) {

Return false

}

Try {

This.HttpRequest.setRequestHeader ("Content-Type", mime)

Return true

} catch (e) {

If (e instanceof Error) {

Alert ("error modifying MIME category")

Return false

}

}

}

/ / set the event trigger for state change

This.setOnReadyStateChange = function (funHandle, Param) {

If (! this.checkHttpRequest ()) {

Return false

}

This.ProcessRequestFunction = funHandle

This.ProcessRequestParam = Param

Return true

}

This.setLoadingImg = function (ImgID) {

This.LoadingImg = ImgID

}

/ / establish a HTTP connection

/ / open ("method", "URL" [, asyncFlag [, "userName" [, "password"])

This.Open = function (method, url, async, username, psw) {

If (! this.checkHttpRequest ()) {

Return false

}

This.openMethod = method

This.openURL = url

This.openAsync = async

If ((this.openMethod==null) | ((this.openMethod.toUpperCase ()! = "GET") & & (this.openMethod.toUpperCase ()! = "POST") & & (this.openMethod.toUpperCase ()! = "HEAD")) {

Alert ("Please specify the method of the HTTP request, which is Get, Post or Head")

Return false

}

If ((this.openURL==null) | | (this.openURL== "")) {

Alert ("Please specify target URL")

Return false

}

Try {

This.HttpRequest.open (this.openMethod, this.openURL, this.openAsync, username, psw)

} catch (e) {

If (e instanceof Error) {

Alert ("unable to establish HTTP connection")

Return false

}

}

If (this.openMethod.toUpperCase ()) = = "POST") {

If (! this.setRequestHeader ("application/x-www-form-urlencoded")) {

Alert ("failed to modify MIME category")

Return false

}

}

If (this.openAsync) {/ / asynchronous mode, the program continues to execute

If (this.ProcessRequestFunction==null) {

Alert ("Please specify a function to handle the returned information")

Return false

}

Var _ http_request_ajax = this.HttpRequest

Var _ this_ajax = this

This.HttpRequest.onreadystatechange = function () {

If (_ http_request_ajax.readyState==4) {

If (_ http_request_ajax.status==200) {

_ this_ajax.ProcessRequestFunction (_ http_request_ajax, _ this_ajax.ProcessRequestParam, _ this_ajax.LoadingImg)

} else {

Alert ("there is an exception in the page you requested.")

Return false

}

}

}

}

If (this.LoadingImgression null) {

FunShow (this.LoadingImg)

}

Return true

}

/ / send a HTTP request to the server

/ / format: name=value&anothername=othervalue&so=on

This.Send = function (idata) {

If (! this.checkHttpRequest ()) {

Return false

}

Var data = null

If (this.openMethod.toUpperCase ()) = = "POST") {

Data = funEscapeAll (idata)

}

Try {

This.HttpRequest.send (data)

Return true

} catch (e) {

If (e instanceof Error) {

Alert ("failed to send HTTP request to server")

Return false

}

}

}

/ / process the information returned by the server

This.getResponseText = function (type) {

If (! this.checkHttpRequest ()) {

Return false

}

If (this.HttpRequest.readyState==4) {

If (this.HttpRequest.status==200) {

If ((typewritten null) & & (type.toUpperCase () = = "XML")) {

Return this.HttpRequest.responseXML

}

Return this.HttpRequest.responseText

} else {

Alert ("there is an exception in the page you requested.")

Return false

}

}

}

/ / stop the current request

This.abort = function () {

If (! this.checkHttpRequest ()) {

Return false

}

If (this.LoadingImgression null) {

FunHide (this.LoadingImg)

}

If (this.HttpRequest.readyState > 0 & & this.HttpRequest.readyState non-asynchronous mode

3. Pictures

At this point, I believe that everyone on the "javascript Ajax class implementation code" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Information mode code service program server function processing content actual method category picture client client target script error page UTF-8 Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker macOS OPPO Reno Linux