Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ajax function

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

This article mainly introduces how to use the ajax function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The code is as follows:

/ *

Call method:

1.POST mode

Var txt = escape (sender.value); / / document.getElementById (") .value)

Var data = "name=" + txt + "& pwd=" + txt

Var option = {"url": "handler/Handler.ashx"

, "action": "POST"

, "callback": function () {

If (xmlHttp.readyState = = 4) {/ / the server responded

If (xmlHttp.status = = 200) {/ / Service responds correctly

Alert (xmlHttp.responseText)

}

XmlHttp = null; / / Recycling Resources

}

}

, "data": data

}

Ajax (option)

2.GET mode

Var txt = escape (sender.value); / / document.getElementById (") .value)

Var option = {"url": "handler/Handler.ashx&name=" + txt + "& pwd=" + txt

, "action": "POST"

, "callback": function () {

If (xmlHttp.readyState = = 4) {/ / the server responded

If (xmlHttp.status = = 200) {/ / Service responds correctly

Alert (xmlHttp.responseText)

}

XmlHttp = null; / / Recycling Resources

}

}

}

Ajax (option)

, /

Function ajax (option) {

CreateXMlHttpRequest (); / / create a xmlHttpRequest object

If (option! = null & & option! = undefined) {

If (option.url = = null & & option.url = = undefined) {

XmlHttp = null

Alert ("missing necessary parameter option.url")

Return

}

If (option.action = = null & & option.action = = undefined) {

XmlHttp = null

Alert ("missing necessary parameter option.action")

Return

}

XmlHttp.open (option.action, option.url, true)

If (option.contentType! = null & & option.contentType! = undefined) {

XmlHttp.setRequestHeader ("Content-Type", option.contentType)

} else {

XmlHttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")

}

If (option.callback! = null & & option.callback! = undefined) {

XmlHttp.onreadystatechange = option.callback

}

If (option.action.toUpperCase ()) = = "POST") {

XmlHttp.send (option.data)

} else {

XmlHttp.send (null)

}

}

}

It is best to recycle xmlHttp = null after the var xmlHttp; / / call is completed.

/ * get the element * /

Function g (arg) {

Var t = document.getElementById (arg)

If (null! = t & & t! = undefined) {

Return t

}

T = document.getElementsByName (arg)

If (null! = t & & t! = undefined) {

Return t

}

T = document.getElementsByTagName (arg)

If (null! = t & & t! = undefined) {

Return t

}

}

/ * create an ajax request object * /

Function createXMlHttpRequest () {

Try {/ / Firefox, Chrome, Surfri, Opera+8

XmlHttp = new XMLHttpRequest ()

}

Catch (ie) {

Try {/ / IE6+

XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP")

} catch (ie) {

XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP")

}

}

Return xmlHttp

}

Thank you for reading this article carefully. I hope the article "how to use the ajax function" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Articles services methods functions essentials parameters objects servers resources code values elements interests at the same time more best friends knowledge editing industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Shulou Tech Info Xiaomi macOS