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 understand Ajax Core XMLHttpRequest

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

Share

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

This article mainly explains "how to understand the core XMLHttpRequest of Ajax". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to understand the core XMLHttpRequest of Ajax.

Usage

Five-step usage of XMLHttpRequest:

The copy code is as follows:

1. Create object

two。 Register callback function

3. Use the open method to set the basic information for interacting with the server

4. Set up the data sent and start to interact with the server

5. Implement the callback function.

Since each application of a XMLHttpRequest object requires a five-step operation, the use of the object can be encapsulated in a js file, and the corresponding function can be accomplished by passing some parameters using its method, as follows:

The copy code is as follows:

/ / using the encapsulation method personnel only provide http requests, url addresses, data, and callback methods for success and failure

/ / 1. Define the constructor of a XMLHttpRequest object

Var MyXMLHttpRequest = function () {

Var xmlhttprequest

If (window.XMLHttpRequest) {

/ / IE7,IE8,FireFox,Mozillar,Safari,Opera

/ / alert ("IE7,IE8,FireFox,Mozillar,Safari,Opera")

Xmlhttprequest = new XMLHttpRequest ()

/ / resolve the problem that browsers may not work when they respond on the server side without Text headers

If (xmlhttprequest.overrideMimeType) {

Xmlhttprequest.overrideMimeType ("text/xml")

}

} else if (window.ActiveXObject) {

/ / IE6,IE5.5,IE5

Alert ("IE6,IE5.5,IE5")

Var activexName = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]

For (var nasty 0ntern

Extension problem

1. Browser cach

two。 Chinese garbled code

3. Cross-domain access

Both problem 1 and problem 3 can be solved by changing the url address. Problem 1 can be time-stamped at the end of the url address, and problem 3 can be solved by proxy. Simply add the judgment before the third step in send ():

The copy code is as follows:

/ / resolve the conversion of cache: add timestamp

If (url.indexOf (?) > = 0) {

Url = url + "& t =" + (new Date ()). ValueOf ()

} else {

Url = url +? t = "+ (new Date ()). ValueOf ()

}

/ / solve cross-domain problems

If (url.indexOf ("http://") > = 0) {

Url.replace (?)

Url= "Proxy?url=" + url

}

Question 3 corresponds to the proxy server implementation:

The copy code is as follows:

/ * *

* Handles the HTTP GET method.

*

* @ param request servlet request

* @ param response servlet response

* @ throws ServletException if a servlet-specific error occurs

* @ throws IOException if an Imax O error occurs

, /

@ Override

Protected void doGet (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

/ / get the parameter, and finally get the request url address similar to: url = http://192.168 … / AJAX/AJAXServer?aa=11&bb=22&cc=33

StringBuilder url = new StringBuilder ()

Url.append (request.getParameter ("url"))

/ / get the cross-domain address url = http://192.168. / AJAX/AJAXServer

Enumeration enu = request.getParameterNames ()

Boolean flag = false; / / defines the flag variable, indicating whether it is the first parameter of splicing

While (enu.hasMoreElements ()) {

String paramName = (String) enu.nextElement ()

If (! ParamName.equals ("url")) {

String paramValue = request.getParameter (paramName)

ParamValue = URLEncoder.encode (paramValue, "utf-8")

If (! Flag) {

Url.append (?) Append (paramName). Append ("="). Append (paramValue)

Flag = true

} else {

Url.append ("&"). Append (paramName). Append ("="). Append (paramValue)

}

}

}

Response.setContentType ("text/html;charset=utf-8")

PrintWriter out = response.getWriter ()

If (url! = null & & url.length () > 0) {

URL connectionUrl = new URL (url.toString ())

BufferedReader reader = new BufferedReader (new InputStreamReader (connectionUrl.openStream (), "utf-8"))

Thank you for your reading, the above is the content of "how to understand Ajax core XMLHttpRequest", after the study of this article, I believe you have a deeper understanding of how to understand Ajax core XMLHttpRequest, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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