Several JavaScript methods to implement native ajax
In data-based applications, the data required by users, such as contact lists, can be obtained from a server independent of the actual web page and can be dynamically written into the web page, coloring the slow web application experience and making it look like a desktop Same application. Since js has various frameworks, such as jquery, using ajax has become quite simple. But sometimes in order to pursue simplicity, there may be no need to load a huge js plug-in like jquery in the project. But what should I do if I want to use ajax function? Let me share with you several ways to use javascript to implement native ajax.
First, you must create an XMLHttpRequest object before implementing ajax. If the browser that creates the object is not supported, you need to create an ActiveXObject. The specific method is as follows:
var xmlHttp; function createxmlHttpRequest(){ if(window.ActiveXObject){ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest) xmlHttp=new XMLHttpRequest(); }
The following uses the xmlHttp created above to implement the simplest ajax get request:
function doGet(url){//注意在传参数值的时候最好使用encodeURI处理一下,防止乱码 createxmlHttpRequest(); xmlHttp.open("GET",url); xmlHttp.send(null); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4&&xmlHttp.status==200){ alert('success'); }else{ alert('fail'); } } }
Uses the xmlHttp created above xmlHttp implements the simplest ajax post request:
function doPost(url,data){//注意在传参数值的时候最好使用encodeURI处理一下,防止乱码 createxmlHttpRequest(); xmlHttp.open("POST",url); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(data); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4&&xmlHttp.status==200){ alert('success'); }else{ alert('fail'); } } }
Let’s share an encapsulation of the $.ajax method that simulates jquery seen on the Internet:
var createAjax=function(){ var xhr=null; try{//IE系列浏览器 xhr=new ActiveXObject("microsoft.xmlhttp"); }catch(e1){ try{//非IE浏览器 xhr=new XMLHttpRequest(); }catch(e2){ window.alert("您的浏览器不支持ajax,请更换!"); } } return xhr; }; var ajax=function(conf){ var type=conf.type;//type参数,可选 var url=conf.url;//url参数,必填 var data=conf.data;//data参数可选,只有在post请求时需要 var dataType=conf.dataType;//datatype参数可选 var success=conf.success;//回调函数可选 if(type==null){//type参数可选,默认为get type="get"; } if(dataType==null){//dataType参数可选,默认为text dataType="text"; } var xhr=createAjax(); xhr.open(type,url,true); if(type=="GET"||type=="get"){ xhr.send(null); }else if(type=="POST"||type=="post"){ xhr.setRequestHeader("content-type","application/x-www-form-urlencoded"); xhr.send(data); } xhr.onreadystatechange=function(){ if(xhr.readyState==4&&xhr.status==200){ if(dataType=="text"||dataType=="TEXT"){ if(success!=null){//普通文本 success(xhr.responseText); } }else if(dataType=="xml"||dataType=="XML"){ if(success!=null){//接收xml文档 success(xhr.responseXML); } }else if(dataType=="json"||dataType=="JSON"){ if(success!=null){//将json字符串转换为js对象 success(eval("("+xhr.responseText+")")); } } } }; }; 该
This method is also very simple to use. It’s the same as jquery’s $.ajax method, but it doesn’t have as many parameters. It just simply implements some basic ajax functions. The usage method is as follows:
ajax({ type:"post",//post或者get,非必须 url:"test.jsp",//必须的 data:"name=dipoo&info=good",//非必须 dataType:"json",//text/xml/json,非必须 success:function(data){//回调函数,非必须 alert(data.name); } });
Have you learned the above methods of javascript to implement native ajax? I hope everyone has to help.
Related recommendations:
How to implement ajax to jump to a new jsp page
jQuery uses ajax to read local json files Case
How to solve the problem of multiple ajax page requests and page loading blocking
The above is the detailed content of Several JavaScript methods to implement native ajax. 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

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

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

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

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

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

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.
