Home > Web Front-end > JS Tutorial > Exploring AJAX Requests: Learn about the various AJAX request methods

Exploring AJAX Requests: Learn about the various AJAX request methods

WBOY
Release: 2024-01-30 09:22:06
Original
540 people have browsed it

Exploring AJAX Requests: Learn about the various AJAX request methods

Exploring AJAX requests: To understand various AJAX request methods, specific code examples are required

Introduction:
In modern Web development, AJAX (Asynchronous JavaScript and XML) is widely used to implement asynchronous data exchange between web pages and servers, allowing users to obtain the latest data without refreshing the entire page. This article will introduce the basic principles of AJAX and how to use different AJAX request methods, while providing specific code examples.

1. The basic principle of AJAX
The basic principle of AJAX is to exchange data with the server through the browser's built-in XMLHttpRequest object. It sends requests and receives responses on the page through JavaScript, and then uses DOM operations to insert data into the corresponding location on the page to achieve partial refresh of the page.

2. Common methods of using AJAX

  1. GET request:
    GET request is one of the most common AJAX request methods, which is used to obtain data from the server. The following is an example of a GET request implemented using native JavaScript:
function getData(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var data = JSON.parse(xhr.responseText);
      callback(data);
    }
  };
  xhr.send();
}

getData("https://api.example.com/data", function(data) {
  console.log(data);
});
Copy after login
  1. POST request:
    POST request is used to send data to the server and is often used in scenarios such as form submission. Here is an example of a POST request using jQuery:
$.ajax({
  url: "https://api.example.com/post",
  type: "POST",
  data: { username: "example", password: "123456" },
  success: function(response) {
    console.log(response);
  }
});
Copy after login
  1. PUT request:
    PUT request is used to update data on the server. The following is an example of a PUT request implemented using the fetch API:
fetch("https://api.example.com/update", {
  method: "PUT",
  body: JSON.stringify({ id: 1, name: "example" }),
  headers: {
    "Content-Type": "application/json"
  }
})
.then(function(response) {
  return response.json();
})
.then(function(data) {
  console.log(data);
});
Copy after login
  1. DELETE request:
    DELETE request is used to delete data on the server. The following is an example of a DELETE request implemented using Axios:
axios.delete("https://api.example.com/delete", { params: { id: 1 } })
.then(function(response) {
  console.log(response.data);
})
.catch(function(error) {
  console.log(error);
});
Copy after login

3. Processing of cross-domain requests
Due to the restriction of the same origin policy, AJAX requests can only be sent to the same domain name by default. under the server. In order to solve this problem, you can use JSONP, CORS, proxy, etc. The following is an example of using JSONP to implement cross-domain requests:

function getData(callback) {
  var script = document.createElement("script");
  script.src = "https://api.example.com/data?callback=" + callback;
  document.body.appendChild(script);
}

function handleData(data) {
  console.log(data);
}

getData("handleData");
Copy after login

4. Error handling of AJAX requests
When making AJAX requests, you need to handle possible errors. The following is an example of using the fetch API to implement error handling:

fetch("https://api.example.com/data")
.then(function(response) {
  if (response.ok) {
    return response.json();
  } else {
    throw new Error("请求失败,状态码:" + response.status);
  }
})
.then(function(data) {
  console.log(data);
})
.catch(function(error) {
  console.log("请求错误:" + error);
});
Copy after login

Conclusion:
Through the introduction of this article, we have understood the basic principles of AJAX and learned to use different AJAX request methods. It is hoped that these specific code examples can help readers better grasp the use of AJAX requests, thereby improving the efficiency and user experience of web development.

The above is the detailed content of Exploring AJAX Requests: Learn about the various AJAX request methods. For more information, please follow other related articles on the PHP Chinese website!

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