Fetch
12 Sep 20251 minute to read
The Fetch class provides a way to make asynchronous network requests, typically to retrieve resources from a server.
var fetchApi = new Fetch('index.html', 'GET');
fetchApi.send()
.then((value) => {
console.log(value);
}).catch((error) => {
console.log(error);
});Properties
contentType string
Specifies the content type of the request, which is used to indicate the original media type of the resource.
Defaults to null
data string|Object
Specifies the data that needs to be added to the request.
Defaults to null
fetchRequest Request
Specifies the request object that represents a resource request.
Defaults to null
type string
Specifies which request method is to be used, such as GET, POST, etc.
Defaults to GET
url string
Specifies the URL to which the request is to be sent.
Defaults to null
Methods
send
Send the request to server.
| Parameter | Type | Description |
|---|---|---|
| data (optional) |
string | Object
|
Specifies the data that needs to be added to the request. |
Returns Promise
Events
beforeSend Function
Specifies the callback function to be triggered before sending the request to the server.
This can be used to modify the fetchRequest object before it is sent.
onFailure Function
Specifies the callback function to be triggered after the request is failed.
onLoad Function
Specifies the callback function to be triggered after the response is received.
This callback will be triggered even if the request is failed.
onSuccess Function
Specifies the callback function to be triggered after the request is successful.
The callback will contain the server response as a parameter.