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

What is the use of AJAX class code

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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!

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