Request
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
* Some parts of this feature may have varying levels of support.
Note: This feature is available in Web Workers.
Giao diện Request của Fetch API đại diện cho một yêu cầu tài nguyên.
Bạn có thể tạo một đối tượng Request mới bằng hàm khởi tạo Request(), nhưng thường gặp hơn là nhận một đối tượng Request được trả về từ một thao tác API khác, chẳng hạn FetchEvent.request của service worker.
Constructor
Request()-
Tạo một đối tượng
Requestmới.
Thuộc tính thể hiện
Request.bodyRead only-
Một
ReadableStreamchứa nội dung body. Request.bodyUsedRead only-
Trả về
truehoặcfalseđể cho biết body đã được dùng trong request hay chưa. Request.cacheRead only-
Chứa chế độ cache của request, ví dụ
default,reload,no-cache. Request.credentialsRead only-
Chứa giá trị điều khiển việc có bao gồm thông tin xác thực trong request hay không, ví dụ
omit,same-origin,include. Mặc định làsame-origin. Request.destinationRead only-
Chuỗi mô tả loại nội dung đang được yêu cầu.
Request.duplexRead only Experimental-
Chế độ duplex của request, xác định việc trình duyệt có phải gửi toàn bộ request trước khi xử lý response hay không.
Request.headersRead only-
Chứa đối tượng
Headersliên kết với request. Request.integrityRead only-
Chứa giá trị subresource integrity của request, ví dụ
sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=. -
Một giá trị boolean cho biết request có phải là điều hướng lịch sử hay không.
Request.keepaliveRead only-
Chứa thiết lập
keepalivecủa request (truehoặcfalse), cho biết trình duyệt có tiếp tục giữ request này hoạt động nếu trang khởi tạo nó bị tải dỡ trước khi request hoàn tất hay không. Request.methodRead only-
Chứa phương thức của request (
GET,POST, v.v.). Request.modeRead only-
Chứa chế độ của request, ví dụ
cors,no-cors,same-origin,navigate. Request.redirectRead only-
Chứa chế độ xử lý chuyển hướng. Giá trị có thể là
follow,errorhoặcmanual. Request.referrerRead only-
Chứa referrer của request, ví dụ
client. Request.referrerPolicyRead only-
Chứa chính sách referrer của request, ví dụ
no-referrer. Request.signalRead only-
Trả về
AbortSignalliên kết với request. Request.urlRead only-
Chứa URL của request.
Phương thức thể hiện
Request.arrayBuffer()-
Trả về một promise giải quyết với biểu diễn
ArrayBuffercủa body request. Request.blob()-
Trả về một promise giải quyết với biểu diễn
Blobcủa body request. Request.bytes()-
Trả về một promise giải quyết với biểu diễn
Uint8Arraycủa body request. Request.clone()-
Tạo một bản sao của đối tượng
Requesthiện tại. Request.formData()-
Trả về một promise giải quyết với biểu diễn
FormDatacủa body request. Request.json()-
Trả về một promise giải quyết với kết quả phân tích body request dưới dạng
JSON. Request.text()-
Trả về một promise giải quyết với biểu diễn văn bản của body request.
Note:
Các hàm đọc request body chỉ chạy được một lần; các lần gọi tiếp theo sẽ bị từ chối với TypeError, cho biết luồng body đã được dùng rồi.
Ví dụ
Trong đoạn mã sau, chúng ta tạo một request mới bằng hàm khởi tạo Request() rồi lấy một vài giá trị thuộc tính của request:
const request = new Request("https://www.mozilla.org/favicon.ico");
const url = request.url;
const method = request.method;
const credentials = request.credentials;
Sau đó bạn có thể fetch request này bằng cách truyền đối tượng Request vào lời gọi fetch(), ví dụ:
fetch(request)
.then((response) => response.blob())
.then((blob) => {
image.src = URL.createObjectURL(blob);
});
Trong đoạn mã sau, chúng ta tạo một request mới bằng hàm khởi tạo Request() với một số dữ liệu khởi tạo và nội dung body cho một request API cần payload:
const request = new Request("https://example.com", {
method: "POST",
body: '{"foo": "bar"}',
});
const url = request.url;
const method = request.method;
const credentials = request.credentials;
const bodyUsed = request.bodyUsed;
Note:
Body chỉ có thể là một đối tượng Blob, ArrayBuffer, TypedArray, DataView, FormData, URLSearchParams, ReadableStream hoặc String, cũng như một literal chuỗi, nên nếu muốn thêm một object JSON vào payload thì bạn cần stringify object đó.
Sau đó bạn có thể fetch request API này bằng cách truyền đối tượng Request vào lời gọi fetch() và nhận response:
fetch(request)
.then((response) => {
if (response.status !== 200) {
throw new Error("Something went wrong on API server!");
}
return response.json();
})
.then((response) => {
console.debug(response);
// …
})
.catch((error) => {
console.error(error);
});
Thông số kỹ thuật
| Specification |
|---|
| Fetch> # request-class> |