AJAX requests with ES2015 Promises
npm install ajaxify-progressExtremely simple to use library for AJAX request that's based on ES2015
Promises.
If second argument is boolean true the browser will send request for data in
JSON format. If argument remains empty it will just return text.
Also to set custom headers you can pass it an object with headers key
that also should be an object.
``javascript
ajaxify.get("http://jsonplaceholder.typicode.com/posts/", true)
.progress(() => {
displayProgressBar();
})
.then((data) => {
displayPosts(data);
})
.catch(() => {
displayError();
});
`
To send data with one of write methods you would add data property onheaders
config object that also can contain . If headers object is ommited orContent-Type: application/x-www-form-urlencoded
left blank header will be
automatically set.
`javascript
const config = {
data: {
email: "me@email.com",
username: "johndoe",
password: "secretPass" // just demo
}
};
ajaxify.post("https://localhost:3000/user", config)
.progress(() => {
displayProgressBar();
})
.then(() => {
displaySuccesMessage("User was successfully created!");
})
.catch(() => {
displayErrorMessage("Failed to create new user!");
});
``