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 are the useful AJAX class codes?

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

Share

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

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!

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

  • How does html realize that the script never goes wrong

    This article will explain in detail how to achieve html script never go wrong, the editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article. Scripts never go wrong

    12
    Report