Detailed explanation of the use of AJAX's XMLHttpRequest object
This time I will bring you a detailed explanation of the use of AJAX's XMLHttpRequest object. What are the precautions when using AJAX's XMLHttpRequest object. Here are actual cases, let's take a look.
AJAX is a web development technology for creating interactive web applications. It is a collection of asynchronous Javascript and XML. Its core is the XMLHttpRequest object, which can partially update web pages without submitting the entire page to the server. It is a key technology in the AJAX Web application architecture.
Basic attributes:
Basic method:
First: Create an XMLHttpRequest object
Second: Register
Callback functionThird: Set parameters for interaction with the server
Fourth: Set the data sent to the server and start interaction with the server
Fifth: Determine whether the interaction with the server is completed, and whether the server returns correct data
HTML code:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript" > var xmlhttp; function submit() { //1、创建XMLHttpRequest对象 if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); if (xmlhttp.overrideMineType) {//针对某些特定版本的mozillar浏览器的BUG进行修正 //将覆盖发送给服务器的头部,强制 text/xml 作为 mime-type xmlhttp.overrideMineType("text/xml"); } } else if (window.ActiveXObject) {//针对IE浏览器进行处理 var activexName = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Miscrosoft XMLHTTP"]; for (var i = 0; i < activexName.length; i++) { try{ xmlhttp=new ActiveXObject(activexName[i]); break; } catch (e) { } } } if (xmlhttp == undefined || xmlhttp == null) { alert("当前浏览器不支持插件XMLHttpRequest对象,请更换浏览器"); return; } //2、注册回调函数 xmlhttp.onreadystatechange = callback; //获取当前值 var username = document.getElementById("userName").value; //设置字符串参数,并进行编码(用于POST提交方式) var args = "username=" + encodeURIComponent(username); //GET 交互 //3、设置和服务器端交互的参数 //xmlhttp.open("GET", "XMLHttpRequest.aspx?username=" + username, true); //POST交互 //3、设置和服务器端交互的参数 //使用Post方式不用担心缓存 xmlhttp.open("POST", "XMLHttpRequest.aspx?username=" + username, true); //设置Content-Type类型,告知服务器实体中有参数 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //4、设置向服务器端发送的数据,启动和服务器端的交互 //用于GET提交 //xmlhttp.send(null); //用于POST提交 xmlhttp.send(args); function callback() { //5、判断和服务器端的交互是否完成,还有判断服务器端是否正确返回了数据 if (xmlhttp.readyState == 4) {//readyState=4表示交互完成 if (xmlhttp.status == 200) {//status=200表示正确返回了数据 //纯文本数据的接收方法 var message = xmlhttp.responseText; var p = document.getElementById("message"); p.innerHTML = message; } } } } </script> </head> <body> <input type="text" id="userName" /> <input type="button" value="用户名校验" onclick="submit()" /> <br /> <p id="message"></p> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace XMLHttpRequest五步法 { public partial class XMLHttpRequest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Response.Clear(); //获取当前值,get提交使用Request.QueryString方法 //string username = Request.QueryString["username"]; //POST提交,使用Request.Form string username = Request.Form["username"]; Response.Write("姓名:'" + username + "'<br/>时间:'" + DateTime.Now.ToString() + "'"); Response.End(); } } }
Summary:
XMLHttpRequest is the core part of AJAX and needs to be understood well. When I first came into contact with it, I didn't understand what was going on, and the video also talked about native AJAX. On the surface, it seemed not easy to understand, but through specific demo practice, you will find that it is actually quite easy to understand. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:Ajax and form+iframe method to implement file upload (detailed graphic and text explanation)
The above is the detailed content of Detailed explanation of the use of AJAX's XMLHttpRequest object. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make
