axios logo
axios
Posted: Tuesday, 8 November 2022 at 13:42
Updated: Tuesday, 8 November 2022 at 17:07

Tính năng

  1. Tạo request từ trình duyệt bằng XMLHttpRequest
  2. Tạo request từ node.js bằng http
  3. Hỗ trợ Promise API
  4. Đón chặn request và response
  5. Biến đổi dữ liệu request và response
  6. Bãi bỏ request
  7. Tự động chuyển đổi cho dữ liệu JSON
  8. Hỗ trợ phía client bảo vệ chống lại XSRF

Cài đặt

  1. Sử dụng npm: $ npm install axios
  2. Sử dụng yarn: $ yarn add axios

Ví dụ

importaxios from 'axios';
// Tạo một request để truy xuất người dùng ứng với ID cho sẵn:
axios.get('/user?ID=12345')
.then(function(response){
// xử trí khi thành công
console.log(response);
})
.catch(function(error){
// xử trí khi bị lỗi
console.log(error);
})
.then(function(){
// luôn luôn được thực thi
});
// Cái request bên trên cũng có thể được làm bằng cách sau, tùy
ý
axios.get('/user',{
params:{
ID:12345
}
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
})
.then(function(){
// luôn luôn được thực thi
});
// Nếu muốn sử dụng async/await thì viết như sau
asyncfunctiongetUser(){
try{
const response =await axios.get('/user?ID=12345');
console.log(response);
}catch(error){
console.error(error);
}}

Find an issue with this page? Fix it on GitHub

Need help? Email mooncomvn130@gmail

Copyright © 2022 Fireship LLC