Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The implementation code for the javascript Ajax class

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report