Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of AJAX class code

Shulou Source: shulou.com Published: 2022-06-03 07:47:13 09月23日 Update

I believe many inexperienced people don't know what to do about the use of AJAX code. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Basic usage:

The code is as follows:

Var ajax = new AjaxObj (url)

Ajax.addListener (200, function (r) {

Alert (r)

});

Ajax.send ()

You can also call continuously:

The code is as follows:

Var ajax = new AjaxObj (url) .addListener (200,200, function (r) {

Alert (r)

}) .send ()

In addition, custom POST or GET requests are supported, and different HTTP status codes are monitored. Read the code and figure it out for yourself:)

Complete code:

The code is as follows:

AjaxObj = function (url, method, content) {

This.r = null

This.url = url

This.method = method

This.content = content

This.header = {}

This.header ["Connection"] = "close"

This.header ["Content-type"] = "application/x-www-form-urlencoded"

Var self = this

If (window.XMLHttpRequest) {

This.r = new XMLHttpRequest ()

} else if (window.ActiveXObject) {

Try {

This.r = new ActiveXObject ("Msxml2.XMLHTTP")

} catch (e) {

Try {

This.r = new ActiveXObject ("Microsoft.XMLHTTP")

} catch (e) {

}

}

}

This.addListener = function (http_status, func) {

If (! this.L)

This.L= []

This.L [http _ status] = func

Return this

}

This.setHeader = function (name, value) {

This.header [name] = value

This.r.setRequestHeader (name, value)

Return this

}

This.send = function () {

If (this.method! = "post" & & this.method! = "get")

This.method = "get"

This.r.open (this.method, this.url, true)

For (var h in this.header) {

This.r.setRequestHeader (h, this.header [h])

}

This.r.send (this.content)

}

If (this.r) this.r.onreadystatechange = function () {

If (self.r.readyState = = 4 & & self.L [self.r.status]! = null)

Self.L [self.r.status] (self.r.responseText)

}

}

After reading the above, have you mastered the usage of AJAX class code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Code content method more problem different helpless for this reason skills methods status articles experience industry information information channels channels support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Apple Docker Xiaomi