grab.js
is a lightweight AJAX library that simplifies making XMLHttpRequests in JavaScript. It provides a convenient and chainable interface for configuring and executing AJAX requests with features such as handling headers, enabling CORS, setting timeouts, and more.
Check out the functionality of grab.js
in the following link: DEMO
Include the grab.js
script in your HTML file to use it:
<script src="https://cdn.jsdelivr.net/gh/kostastepetes/grab-js/dist/grab.min.js></script>
Specify the HTTP method, URL, and asynchronous flag for the request.
grab.perform("GET", "https://jsonplaceholder.typicode.com/posts", true);
Set the target HTML element and callback function for the request.
grab.here('HTMLElement', yourCallbackFunction);
Set the request payload data.
grab.setData(JSON.stringify({ title: 'Post Title', body: 'Post Body', userId: 1 }));
Set the headers for the request.
grab.setHeaders({ 'Content-type': 'application/json' });
Enable or disable Cross-Origin Resource Sharing (CORS) globally for all requests made using the grab.js
library.
grab.enableCors = true;
grab.enableCors = false;
Set a callback function to handle errors during the request.
grab.error(function() {
console.error('An error occurred during the request.');
});
Abort the ongoing request.
grab.abort();
Execute the configured request.
grab.done();
function performSpecificHeaderPOST() {
grab.perform('POST', 'https://jsonplaceholder.typicode.com/posts', true)
.here('HTMLElement', yourCallbackFunction)
.setData(JSON.stringify({ title: 'Post Title', body: 'Post Body', userId: 1 }))
.setHeaders({ 'Content-type': 'application/json' })
.done();
}
function performMultipleHeadersPOST() {
grab.perform('POST', 'https://jsonplaceholder.typicode.com/posts', true)
.here('HTMLElement', yourCallbackFunction)
.setData(JSON.stringify({ title: 'Post Title', body: 'Post Body', userId: 1 }))
.setHeaders({
'Content-type': 'application/json',
'Authorization': 'Bearer your_access_token'
})
.done();
}
function performSpecificHeaderGET() {
grab.perform('GET', 'https://jsonplaceholder.typicode.com/posts/1', true)
.here('HTMLElement', yourCallbackFunction)
.setHeaders({ 'Accept': 'application/json' })
.done();
}
function performAllHeadersGET() {
grab.perform('GET', 'https://jsonplaceholder.typicode.com/posts/1', true)
.here('HTMLElement', 'all')
.done();
}
function performAbort() {
setTimeout(function() {
grab.abort(); // Simulate aborting the request after a delay
}, 2000); // Abort after 2 seconds
}
function performCORS(enable) {
grab.enableCors = enable;
grab.perform('GET', 'https://jsonplaceholder.typicode.com/posts/1', true, enable)
.here('HTMLElement', yourCallbackFunction)
.done();
}
function performErrorHandling() {
grab.perform('POST', 'https://jsonplaceholder.typicode.com/posts', true)
.here('HTMLElement', yourCallbackFunction)
.setData(JSON.stringify({ title: 'Invalid Post' }))
.setHeaders({ 'Content-type': 'application/json' })
.error(function() {
console.log('Error occurred during the request.');
})
.done();
}
Check out the GitHub Repository in the following link: GITHUB