Home Web Front-end JS Tutorial jQuery Ajax analysis encyclopedia

jQuery Ajax analysis encyclopedia

Apr 24, 2018 pm 03:01 PM
ajax jquery Encyclopedia

This time I will bring you a complete analysis of jQuery Ajax, what are the precautions when using jQuery Ajax, the following is a practical case, let's take a look.

What is Ajax

AjaxBasic concepts

##Ajax( Asynchronous JavaScript and XML): Translated into Chinese is asynchronous JavaScript and XML.

Functionally, it is a technology that can update part of a web page without reloading the entire web page.

Traditional web page

If you want to update content or submit a form, you need to reload or refresh the page.

Web pages using ajax technology

With a small amount of data exchange through the background server, the web page can achieve asynchronous partial updating.

Before the emergence of Ajax

Before the emergence of Ajax technology, it was a world of synchronous interaction.

Synchronization: The client sends a request, the server processes it, and then responds. During this period, the client is in a waiting state. When the server completes processing the response, the client reloads the page. If If the information is wrong, the client will initiate a request again and wait here.

After the emergence of Ajax

After the emergence of Ajax, the world changed and became an asynchronous world.

Then why didn’t we use asynchronous before? It’s because there was one missing object, the XMLHttpRequest object. Before this object appeared, web development used a synchronous method. After it appeared, we found that asynchronous operations could be performed. This object is used for data exchange between the background and the server, and this data exchange will not reload the entire page. In this case, local data can be updated without refreshing the page. With this object Later, there was Ajax's asynchronous loading and partial refresh.

Implementation of Ajax’s asynchronous loading partial refresh function

1. First of all, the first question is how to use the XHR object

1) First instantiate an XMLHttpRequest

var request = new XMLHttpRquest();

Note: Most browsers now support the XMLHttpRequest object and the new method, but in the era of IE6 and below, XHR is only ActiveXObject

Solution:

Var request; 
If(window.XMLHttpRequest){ 
Reuest = new XMLHttpRequest(); 
}else{ 
Request = new ActiveXObject(“Microsoft.XMLHTTP”); 
}
Copy after login

2) Request:

Before we take a look at what HTTP request is

is a way for computers to communicate through the network rule.

It is a stateless protocol that does not retain connection-related information.

The client sends a request to the server, the server responds, and then the connection is closed

A complete HTTP request has seven steps

A. Establish a TCP connection

B. The client sends the requested command to the server

C. The browser sends request header information

D. The server sends a response

E. The server sends response header information

F. The server sends data to the browser

G. The server closes the TCP connection

Looking at it separately: the HTTP request is divided into four parts

HTTP request method and action (get, pos)

The URL being requested (request address)

Request header (including client environment information, authentication information, etc.)

Request body, Request text.

Get request: Generally used to obtain information (http default request method)

Url传参属性和值都是可见的,对所发内容大小有限制 一般在2000个字符

get请求安全的说法是因为你请求一次和请求一万次效果是一样的不会对数据造成影响。

Post请求:一般用于服务器数据修改

对所发信息没有大小限制

HTTP响应有三部分

1)一个数字和文字组成的状态吗,用来显示请求是成功还是失败

2)响应头,和请求头信息一样包含很多信息,例如服务器类型,日期时间,内容类型和长度等

3)响应体,响应正文

HTTP响应状态吗由三位数字组成,首位数字定义了状态码的类型:

1xx:信息类,表示接收到浏览器请求,正在进一步处理

2xx:成功表示用户请求被正确接受

3xx:重定向,表示没有请求成功,客户必须采取进一步的动作

4xx:客户端错误,表示客户端请求有错误404NOTFound意味着请求中所引用的文档不存在

5xx:服务器错误,表示服务器不能完成对请求的处理

通过XMLHttpRequest发送请求

1.创建

var request = new XMLHttpRquest();

2.发送请求

两个方法:

open(method,url,async),Send(string)这两种方法可以将请求发送到服务器

Request.open(get,get.php,true)
Request.Send()
Request.open(post,post.PHP,true) 
Request.Send()
Request.open(post,post.php,true) 
Request.setRequestHeader(‘Content-Type','application/x-www-form-urlencoded') 
Request.send(“name=王二狗&sex=男”);
Copy after login

send()中的内容是要向后台传递的参数,在get请求是通过url传递参数,所以get中send()里面的内容可以省略,post方式中不能省略,省略了之后就是无意义的请求了

setRequestHeader是用来设置请求参数的类型,form

3.获取响应

responseText:获取字符串形式的响应式数据

responseXML:

Status和statusText:以数字和文本形式返回HTTP状态吗

getAllResponseHeader():获取所有的响应报头

getResponseHeader():查询相应中的某个字段的值

在响应返回成功时得到的通知,在实际操作中要监听

readyState属性的变化,他的变化代表着服务器相应的变化

0:表示服务器请求未初始化,open还没有被调用

1:服务器连接已经建立,open已经被调用

2:请求已经被接受,接收到头部信息

3:处理中,接收到相应主体

4:请求完成,并且响应完成

Var request=new XMLHttpRequest();
Request.open(get,url,true)
Request.send();
request.onreadyState=function(){
 If(request.readeyState===4&&request.Status===200){
 做一些事情 request.responseText
 }
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

使用Jquery获取iframe页面中Dom元素

$.ajax()使用详解

The above is the detailed content of jQuery Ajax analysis encyclopedia. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

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:

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

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.

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

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.

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

PHP and Ajax: Ways to Improve Ajax Security PHP and Ajax: Ways to Improve Ajax Security Jun 01, 2024 am 09:34 AM

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.

See all articles