Extremely 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
.
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 on
config object that also can contain headers
. If headers
object is ommited or
left blank Content-Type: application/x-www-form-urlencoded
header will be
automatically set.
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!");
});