Bắt lỗi 404 Tìm nạp JavaScript

Phương thức toàn cầu

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5 bắt đầu quá trình tìm nạp tài nguyên từ mạng, trả về một lời hứa sẽ được thực hiện sau khi có phản hồi

Lời hứa giải quyết đối tượng

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
6 đại diện cho phản hồi đối với yêu cầu của bạn

Lời hứa

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5 chỉ từ chối khi gặp lỗi mạng (thường là khi có vấn đề về quyền hoặc tương tự). Một lời hứa
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5 không từ chối các lỗi HTTP (
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
9, v.v. ). Thay vào đó, trình xử lý
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
0 phải kiểm tra thuộc tính
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
1 và/hoặc
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
2

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
3 được triển khai bởi cả
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
4 và
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
5, điều đó có nghĩa là phương thức
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5 có sẵn trong hầu hết mọi ngữ cảnh mà bạn có thể muốn tìm nạp tài nguyên

Phương pháp

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5 được kiểm soát bởi chỉ thị
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
8 của Chính sách bảo mật nội dung thay vì chỉ thị của các tài nguyên mà nó đang truy xuất

Ghi chú. Các tham số của phương thức

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5 giống hệt với các tham số của hàm tạo
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
50

cú pháp

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
6

Thông số

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
51

Điều này xác định tài nguyên mà bạn muốn tìm nạp. Đây có thể là

  • Chuỗi hoặc bất kỳ đối tượng nào khác có trình tạo chuỗi — bao gồm đối tượng
    // space in "C ontent-Type"
    const headers = {
      'C ontent-Type': 'text/xml',
      'Breaking-Bad': '<3',
    };
    fetch('https://example.com/', { headers });
            
    52 — cung cấp URL của tài nguyên bạn muốn tìm nạp
  • Một đối tượng
    // space in "C ontent-Type"
    const headers = {
      'C ontent-Type': 'text/xml',
      'Breaking-Bad': '<3',
    };
    fetch('https://example.com/', { headers });
            
    53
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
54 Tùy chọn

Một đối tượng chứa bất kỳ cài đặt tùy chỉnh nào mà bạn muốn áp dụng cho yêu cầu. Các tùy chọn có thể là

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
55

Phương pháp yêu cầu, e. g. ,

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
56,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
57. Lưu ý rằng tiêu đề
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
58 không được đặt trên các yêu cầu Tìm nạp bằng phương pháp
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
59 hoặc
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
56. (Hành vi này đã được khắc phục trong Firefox 65 — xem lỗi 1508661. )

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
71

Bất kỳ tiêu đề nào bạn muốn thêm vào yêu cầu của mình, được chứa trong đối tượng

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
72 hoặc đối tượng theo nghĩa đen có giá trị
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
73. Lưu ý rằng một số tên bị cấm

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
74

Bất kỳ nội dung nào bạn muốn thêm vào yêu cầu của mình. đây có thể là

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
75,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
76,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
77,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
78,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
79,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
60, đối tượng chuỗi hoặc ký tự hoặc đối tượng
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
61. Khả năng mới nhất này vẫn còn đang thử nghiệm; . Lưu ý rằng một yêu cầu sử dụng phương pháp
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
56 hoặc
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
59 không thể có phần thân

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
64

Chế độ bạn muốn sử dụng cho yêu cầu, e. g. ,

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
65,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
66 hoặc
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
67

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
68

Kiểm soát những gì trình duyệt thực hiện với thông tin xác thực (cookie, mục nhập xác thực HTTP và chứng chỉ ứng dụng khách TLS). Phải là một trong các chuỗi sau

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
69

Yêu cầu trình duyệt loại trừ thông tin đăng nhập khỏi yêu cầu và bỏ qua mọi thông tin đăng nhập được gửi lại trong phản hồi (e. g. , bất kỳ tiêu đề

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
60 nào)

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
67

Yêu cầu trình duyệt bao gồm thông tin đăng nhập với các yêu cầu đối với các URL có cùng nguồn gốc và sử dụng bất kỳ thông tin đăng nhập nào được gửi lại trong phản hồi từ các URL có cùng nguồn gốc. Đây là giá trị mặc định

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
62

Yêu cầu trình duyệt bao gồm thông tin xác thực trong cả yêu cầu cùng nguồn gốc và yêu cầu khác nguồn gốc và luôn sử dụng bất kỳ thông tin đăng nhập nào được gửi lại trong phản hồi

Ghi chú. Thông tin xác thực có thể được bao gồm trong các yêu cầu gốc chéo đơn giản và "cuối cùng", nhưng không được bao gồm trong các yêu cầu trước CORS

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
63

Một chuỗi cho biết cách yêu cầu sẽ tương tác với bộ đệm HTTP của trình duyệt. Các giá trị có thể,

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
64,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
65,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
66,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
67,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
68 và
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
69, được ghi lại trong bài viết cho thuộc tính
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
63 của đối tượng
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
53

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
82

Cách xử lý phản hồi

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
82

  • const headers = [
      ['Content-Type', 'text/html', 'extra'],
      ['Accept'],
    ];
    fetch('https://example.com/', { headers });
            
    84. Tự động theo chuyển hướng. Trừ khi có quy định khác, chế độ chuyển hướng được đặt thành
    const headers = [
      ['Content-Type', 'text/html', 'extra'],
      ['Accept'],
    ];
    fetch('https://example.com/', { headers });
            
    84
  • const headers = [
      ['Content-Type', 'text/html', 'extra'],
      ['Accept'],
    ];
    fetch('https://example.com/', { headers });
            
    86. Hủy bỏ có lỗi nếu chuyển hướng xảy ra
  • const headers = [
      ['Content-Type', 'text/html', 'extra'],
      ['Accept'],
    ];
    fetch('https://example.com/', { headers });
            
    87. Người gọi dự định xử lý phản hồi trong ngữ cảnh khác. Xem tiêu chuẩn tìm nạp WHATWG để biết thêm thông tin
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
88

Một chuỗi chỉ định liên kết giới thiệu của yêu cầu. Đây có thể là một URL có cùng nguồn gốc,

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
89 hoặc một chuỗi trống

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
60

Chỉ định chính sách liên kết giới thiệu để sử dụng cho yêu cầu. Có thể là một trong số

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
61,
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
62,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
67,
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
64,
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
65,
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
66,
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
67, hoặc
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
68

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
69

Chứa giá trị toàn vẹn nguồn con của yêu cầu (e. g. ,

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
60)

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
61

Tùy chọn

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
61 có thể được sử dụng để cho phép yêu cầu tồn tại lâu hơn trang. Tìm nạp bằng cờ
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
61 là sự thay thế cho API
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
64

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
65

Một thể hiện đối tượng

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
66;

Giá trị trả về

Một

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
68 phân giải thành một đối tượng
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
6

ngoại lệ

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
00
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
01

Yêu cầu đã bị hủy bỏ do một cuộc gọi đến phương thức

const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
67
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
03

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
04

Có thể xảy ra vì những lý do sau

Lý do Ví dụ thất bạiTên tiêu đề không hợp lệ.
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
Giá trị tiêu đề không hợp lệ. Đối tượng tiêu đề phải chứa chính xác hai phần tử.
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
URL hoặc lược đồ không hợp lệ hoặc sử dụng lược đồ tìm nạp không hỗ trợ hoặc sử dụng lược đồ không được hỗ trợ cho một chế độ yêu cầu cụ thể.
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5URL bao gồm thông tin xác thực.
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
7URL liên kết giới thiệu không hợp lệ.
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
6Chế độ không hợp lệ (_______005 và
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
06).
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
6Nếu chế độ bộ đệm yêu cầu là "chỉ khi được lưu vào bộ đệm" và chế độ yêu cầu không phải là "cùng nguồn gốc".
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
8Nếu phương thức yêu cầu là mã thông báo tên không hợp lệ hoặc một trong các tiêu đề bị cấm (
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
07,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
08 hoặc
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
09).
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
6Nếu chế độ yêu cầu là "no-cors" và phương thức yêu cầu không phải là phương thức được liệt kê an toàn của CORS (
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
10,
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
11 hoặc
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
12).
const headers = [
  ['Content-Type', 'text/html', 'extra'],
  ['Accept'],
];
fetch('https://example.com/', { headers });
        
6Nếu phương thức yêu cầu là
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
10 hoặc
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
11 và phần thân không phải là null hoặc không phải là không xác định.
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
0Nếu quá trình tìm nạp gây ra lỗi mạng

ví dụ

Trong ví dụ về Yêu cầu tìm nạp của chúng tôi (xem Yêu cầu tìm nạp trực tiếp), chúng tôi tạo một đối tượng

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
53 mới bằng cách sử dụng hàm tạo có liên quan, sau đó tìm nạp nó bằng lệnh gọi
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5. Vì chúng tôi đang tìm nạp một hình ảnh, chúng tôi chạy
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
17 trên phản hồi để cung cấp cho nó loại MIME thích hợp để nó được xử lý đúng cách, sau đó tạo URL đối tượng của nó và hiển thị nó trong phần tử
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
18

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
1

Trong ví dụ Fetch with init then Request (xem Fetch Request init live), chúng ta làm điều tương tự ngoại trừ việc chúng ta chuyển vào một đối tượng

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
19 khi chúng ta gọi
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
5

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
2

Bạn cũng có thể chuyển đối tượng

// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
19 bằng hàm tạo
// space in "C ontent-Type"
const headers = {
  'C ontent-Type': 'text/xml',
  'Breaking-Bad': '<3',
};
fetch('https://example.com/', { headers });
        
53 để có được hiệu ứng tương tự

Làm cách nào để bắt lỗi 404 trong JavaScript?

Tất cả những gì bạn cần làm là thêm chức năng phần mềm trung gian ở dưới cùng của ngăn xếp (bên dưới tất cả các chức năng khác) để xử lý lỗi 404 . ứng dụng. sử dụng((req, res, next) => { res. trạng thái(404).

Tìm nạp có bị lỗi trên 404 không?

Lời hứa tìm nạp() chỉ từ chối khi gặp phải lỗi mạng (thường xảy ra khi có vấn đề về quyền hoặc tương tự). Lời hứa tìm nạp() không từ chối các lỗi HTTP ( 404 , v.v. ) . Thay vào đó, trình xử lý then() phải kiểm tra Phản hồi. ok và/hoặc Phản hồi. thuộc tính trạng thái.

Tìm nạp bắt lỗi gì?

Hàm tìm nạp() sẽ tự động đưa ra lỗi đối với lỗi mạng nhưng không báo lỗi HTTP như phản hồi 4xx hoặc 5xx. Đối với lỗi HTTP, chúng tôi có thể kiểm tra phản hồi. ok để xem yêu cầu có thất bại hay không và tự từ chối lời hứa bằng cách gọi return Promise.

Tìm nạp có ném vào 500 không?

Đối tượng Promise được trả về bởi hàm tìm nạp() không từ chối lỗi khi trạng thái lỗi HTTP được trả về (400 trở lên) như hầu hết các API thông thường vẫn làm. Ví dụ: Mã trạng thái 400 và 500 không gây ra sự từ chối , nhưng 404 có thể hoặc không gây ra sự từ chối lời hứa.