In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to write the basic ajax code for js. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
The code is as follows:
Var xmlhttp = null
Function myajax () {
/ / 1. Create XMLHttpRequest object
/ / 2. You need to write different code for different ways of creating this object for IE and other browsers
If (window.XMLHttpRequest) {
/ / for FF,Mozilar,Opera,Safari,IE7,IE8
Xmlhttp = new XMLHttpRequest ()
/ / fixed some browsers bug
If (xmlhttp.overrideMimeType) {
Xmlhttp.overrideMimeType ("text/xml")
}
} else if (window.ActiveXObject) {
/ / for browsers below IE6
Var activexName = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP", ""]
For (var I = 0; I < activexName.length; iTunes +) {
Try {
/ / take out a control name to create, stop if the creation is successful, or throw an exception
Xmlhttp = new ActiveXObject (activexName [I])
Break
} catch (e) {}
}
}
/ / you need to confirm whether the xmlhttp is created successfully.
If (! xmlhttp) {
Alert ("XMLHTTPRequest creation failed!")
Return
} else {
Alert (xmlhttp)
}
/ / register the callback function. Note that the callback function cannot be registered with parentheses. If added, the value of the function will be returned to onreadystatechange.
Xmlhttp.onreadystatechange = callback
/ / set connection information
/ / the first parameter indicates the http request method, which supports all http request methods, mainly using get and post
/ / the second parameter indicates the url address of the request, and the parameter requested by get is also in urlKh.
/ / the third parameter indicates whether to interact asynchronously or synchronously, and true indicates async.
Xmlhttp.open ("GET", "servlet/CheckUserName?userName=" + userName, true)
/ / send data representation and server-side interaction
/ / in synchronous mode, the name send will not be executed until the data on the server comes back.
Xmlhttp.send (null)
/ / in asynchronous mode, the sentence "send" is executed immediately.
/ / the code requested in POST mode
/ / xmlhttp.open ("POST", "servlet/CheckUserName", true)
/ / in POST mode, you need to set the request header of http.
/ / xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")
/ / send data in POST mode
/ / xmlhttp.send ("userName=" + userName)
}
/ / callback function
Function callback () {
/ / determine that the state of the object is interactive.
If (xmlhttp.readyState = = 4) {
/ / determine whether the http interaction is successful or not
If (xmlhttp.status = = 200) {
/ / get the data returned by the server
/ / get the plain text data output from the server
Var responseText = xmlhttp.responseText
Alert (responseText)
}
}
}
About js basic ajax code how to write to share here, hope the above content can have certain help to everybody, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.