In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "the cross-domain method of Ajax in javascript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the cross-domain method of Ajax in javascript".
Ajax technology can request additional data from the server without unloading the page, which will bring a better user experience.
XMLHttpRequest object
The core of Ajax technology is XMLHttpRequest object (XHR for short).
Create a XHR object
Var xhr = new XMLHttpRequest ()
The usage of XHR
The first method to call is open (), which takes three parameters: the type of request to be sent ("get", "post", and so on), the URL of the request, and a Boolean value indicating whether the request is sent asynchronously.
Xhr.open ("get", "example.php", false)
Xhr.send (null)
The send () method here receives one parameter, the data to be sent as the body of the request. If you do not need to send data through the request body, you must pass in null because this parameter is required for some browsers. After calling send (), the request is dispatched to the server.
After receiving the response, the data of the response is automatically populated with the properties of the XHR object. The related properties are briefly described below.
ResponseText: the text returned as the body of the response.
ResponseXML: if the content type of the response is "text/xml" or "application/xml", this property holds the XML DOM document containing the response data.
Status: HTTP status of the response.
Description of the statusText:HTTP status.
Xhr.open ("get", "example.txt", false)
Xhr.send (null)
If ((xhr.status > = 200 & & xhr.status)
< 300) || xhr.status == 304){ alert(xhr.responseText); } else { alert("Request was unsuccessful: " + xhr.status); } 像前面这样发送同步请求当然没有问题,但多数情况下,我们还是要发送异步请求,才能让 JavaScript 继续执行而不必等待响应。此时,可以检测 XHR 对象的 readyState 属性,该属性表示请求/响应过程的当前活动阶段。这个属性可取的值如下。 0:未初始化。尚未调用 open()方法。 1:启动。已经调用 open()方法,但尚未调用 send()方法。 2:发送。已经调用 send()方法,但尚未接收到响应。 3:接收。已经接收到部分响应数据。 4:完成。已经接收到全部响应数据,而且已经可以在客户端使用了。 var xhr = createXHR(); xhr.onreadystatechange = function(){ if (xhr.readyState == 4){ if ((xhr.status >= 200 & & xhr.status < 300) | | xhr.status = = 304) {
Alert (xhr.responseText)
} else {
Alert ("Request was unsuccessful:" + xhr.status)
}
}
}
Xhr.open ("get", "example.txt", true)
Xhr.send (null)
Function createXHR () {
If (typeof XMLHttpRequest! = "undefined") {
Return new XMLHttpRequest ()
} else if (typeof ActiveXObject! = "undefined") {
If (typeof arguments.callee.activeXString! = "string") {
Var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"
"MSXML2.XMLHttp"]
I, len
For (I < len; version +) {
Try {
New ActiveXObject (versions [I])
Arguments.callee.activeXString = versions [I]
Break
} catch (ex) {
/ / Skip
}
}
}
Return new ActiveXObject (arguments.callee.activeXString)
} else {
Throw new Error ("No XHR object available.")
}
}
At this point, I believe you have a deeper understanding of "the cross-domain method of Ajax in javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.