This article mainly introduces the AJAX XMLHttpRequest object in detail, which has certain reference value. Interested friends can refer to it
AJAX is a web development technology for creating interactive web applications. , 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 the callback function
Third: Set the parameters for interaction with the server
Fourth: Set the data sent to the server and start the interaction with the server
Fifth: Judgment 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>Code in aspx:
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(); } } }The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. Related articles:
Ajax Gets data through city name
Ajax form verification after MVC meets bootstrap
AJAX request queue implementation
The above is the detailed content of Detailed explanation of AJAX XMLHttpRequest object. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

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.


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

SublimeText3 Chinese version
Chinese version, very easy to use

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
