This article mainly summarizes the relevant knowledge of XMLHttpRequest, the core content of Ajax, for everyone. It is very detailed and recommended to everyone. If you need it, please refer to it.
Ajax: "Asynchronous JavaScript and XML" (asynchronous JavaScript and XML), a comprehensive technology: using JavaScript object XMLHttpRequest for asynchronous data exchange; JavaScript operating DOM to achieve dynamic effects; using XHTML CSS to express information ;XML and XSLT manipulate data. This article focuses on using the XMLHttpRequest object for asynchronous data exchange with the server.
How to use
XMLHttpRequest five-step usage:
1. Create the object;
2. Register the callback function;
3. Use open Method sets the basic information for interacting with the server;
4. Sets the data to be sent and starts interacting with the server;
5. Implements the callback function.
Since five steps are required every time the XMLHttpRequest object is used, the use of the object can be encapsulated in a js file, and the corresponding functions can be completed by passing some parameters and using its methods. The implementation is as follows:
//Using the encapsulation method, people only provide http requests, URL addresses, data, success and failure callback methods
//1. Define the construction method of the 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(); //解决浏览器在服务器端响应由于没有Text头的时候可能无法工作的问题 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 n=0;n
Extension issues
1. Browser cache
2. Chinese garbled characters
3. Cross-domain access
For question 1, Problem 3 can be solved by changing the URL address. Problem 1 can be solved by adding a timestamp at the end of the URL address, and problem 3 can be solved through proxy. Just add the corresponding judgment before executing the third step in send():
// Solve the cache conversion: add the timestamp
if(url.indexOf("?") >= 0 ){ url = url + "&t=" + (new Date())。valueOf(); } else { url = url + "?t=" + (new Date())。valueOf(); } //解决跨域的问题 if(url.indexOf("http://") >= 0) { url.replace("?","&"); url = "Proxy?url=" + url; }
Question 3 corresponds to the proxy service End implementation:
/** * 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 I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取参数,最后得到请求url地址类似于:url = http://192.168…/AJAX/AJAXServer?aa=11&bb=22&cc=33 StringBuilder url = new StringBuilder(); url.append(request.getParameter("url")); //获取访问的跨域地址url = http://192.168…/AJAX/AJAXServer Enumeration enu = request.getParameterNames(); boolean flag = false; //定义标志变量,表示是否为拼接的第一个参数 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"));
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Solution to the problem of ajax cross-domain request data cookie loss
Use ajax to change page content and address bar URL without refreshing
##
The above is the detailed content of Ajax core XMLHttpRequest summary. For more information, please follow other related articles on the PHP Chinese website!

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
