Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the useful AJAX class codes?

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

Editor to share with you what the AJAX class code is good to use, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Ajax.js

-[Ajax class]--

The code is as follows:

Function Ajax (recvType) {

Var aj=new Object ()

Aj.recvType=recvType? RecvType.toUpperCase (): 'HTML'; / / the file type passed to the parameter

Aj.targetUrl=''

Aj.sendString=''

Aj.resultHandle=null

/ * create XMLHttpRequest object * /

Aj.createXMLHttpRequest=function () {

Var xmlHttp = false

If (window.XMLHttpRequest) {/ / create XMLHttpRequest objects in non-IE

XmlHttp = new XMLHttpRequest ()

} else if (window.ActiveXObject) {

Try {

XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP"); / / create according to the new version of IE

} catch (error1) {/ / creation failed

Try {

XmlHttp = new ActiveXobject ("Microsoft.XMLHttp"); / / create by the old version of IE

} catch (error2) {/ / creation failed

XmlHttp = false

}

}

}

Return xmlHttp

}

Aj.XMLHttpRequest=aj.createXMLHttpRequest ()

/ * process the response from the server * /

Aj.processHandle=function () {

If (aj.XMLHttpRequest.readyState = = 4) {

If (aj.XMLHttpRequest.status = = 200) {

If (aj.recvType== "HTML")

Aj.resultHandle (aj.XMLHttpRequest.responseText)

Else if (aj.recvType== "XML")

Aj.resultHandle (aj.XMLHttpRequest.responseXML)

}

}

}

/ * define the method passed using the get method * /

Aj.get=function (targetUrl, resultHandle) {

Aj.targetUrl=targetUrl

If (resultHandlewritten null) {

Aj.XMLHttpRequest.onreadystatechange=aj.processHandle

Aj.resultHandle=resultHandle

}

If (window.XMLHttpRequest) {

Aj.XMLHttpRequest.open ("get", aj.targetUrl)

Aj.XMLHttpRequest.send (null)

} else {

Aj.XMLHttpRequest.open ("get", aj.targetUrl, true)

Aj.XMLHttpRequest.send ()

}

}

/ * define the method passed using the post method * /

Aj.post=function (targetUrl, sendString, resultHandle) {

Aj.targetUrl=targetUrl

If (typeof (sendString) = = "object") {

Var str= ""

For (var pro in sendString) {

Str+=pro+ "=" + sendString [pro] + "&"

}

Aj.sendString=str.substr (0, str.length-1)

} else {

Aj.sendString=sendString

}

If (resultHandlewritten null) {

Aj.XMLHttpRequest.onreadystatechange=aj.processHandle

Aj.resultHandle=resultHandle

}

Aj.XMLHttpRequest.open ("post", targetUrl)

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

Aj.XMLHttpRequest.send (aj.sendString)

}

Return aj

}

-[usage]--

UseAjax.html

The copy code is as follows:

Var ajax=Ajax ()

/ * how to use get * /

Ajax.get ("server.php?name=zhangsan&phone=778", function (data) {

Alert (data); / / data is the data read from the server

});

/ * the first way to use post * /

/ * ajax.post ("server.php", "name=ligang&phone=222", function (data) {

Alert (data)

});

, /

/ * the second way to use post * /

/ * ajax.post ("server.php", {name: "tom", phone: "456"}, function (data) {

Alert (data)

});

, /

Server.php

Enter the address of the useAjax.html in the browser, if it appears

Then the Ajax method is used correctly

The above is all the content of this article "what are the useful AJAX class codes?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Method code method article easy to use content object server service not much usage address most name data file more browser phone knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi Xiaomi OPPO Reno Docker