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

How to write the basic ajax code of js

2025-01-16 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report