Home > Web Front-end > JS Tutorial > There are five common Ajax submission methods

There are five common Ajax submission methods

WBOY
Release: 2024-01-17 11:09:16
Original
1278 people have browsed it

There are five common Ajax submission methods

Learning the five common submission methods in Ajax requires specific code examples

Introduction:
With the development of Web applications and users' demand for interactivity and real-time As the demand for functionality increases, Ajax technology has become an indispensable part of front-end development. Ajax (Asynchronous JavaScript and XML) is a technology that uses JavaScript for asynchronous communication, which can realize data interaction with the server and update page content without refreshing the entire page. In Ajax, submitting data is inevitable. This article will introduce five common submission methods and provide specific code examples.

1. GET method
The GET method is the most common submission method. Data is usually transferred through the URL, that is, the data is appended to the end of the URL. The following is a code example of the GET method:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api?param1=value1&param2=value2', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = xhr.responseText;
    // 处理返回的数据
  }
};
xhr.send();
Copy after login

2. POST method
The POST method sends data to the server as part of the request, and the data will not be exposed in the URL. The following is a code example of the POST method:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://example.com/api', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = xhr.responseText;
    // 处理返回的数据
  }
};
xhr.send('param1=value1&param2=value2');
Copy after login

3. FormData method
FormData is an API used to build form data, which can easily convert form data into key-value pairs. The following is a code example of the FormData method:

var formData = new FormData();
formData.append('param1', 'value1');
formData.append('param2', 'value2');

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://example.com/api', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = xhr.responseText;
    // 处理返回的数据
  }
};
xhr.send(formData);
Copy after login

4. JSON method
JSON (JavaScript Object Notation) is a lightweight data exchange format commonly used for front-end and back-end data transmission. The following is a code example in JSON mode:

var data = {
  param1: 'value1',
  param2: 'value2'
};

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://example.com/api', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = xhr.responseText;
    // 处理返回的数据
  }
};
xhr.send(JSON.stringify(data));
Copy after login

5. XML method
XML (eXtensible Markup Language) is a markup language used to store and transmit structured data. The following is an XML code example:

var xml = '<data><param1>value1</param1><param2>value2</param2></data>';

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://example.com/api', true);
xhr.setRequestHeader('Content-Type', 'text/xml');
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = xhr.responseText;
    // 处理返回的数据
  }
};
xhr.send(xml);
Copy after login

Summary:
This article introduces five common submission methods in Ajax, including GET, POST, FormData, JSON and XML. Each method provides specific code examples to help readers understand and use these methods. In actual development, we can choose an appropriate method for data submission based on needs and scenarios to improve user experience and page performance.

The above is the detailed content of There are five common Ajax submission methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template