Axios
- axios hampir mirip seperti fetch bedanya axios adalah sebuah libary sedangkan fetch adalah API yang tersedia di web browser
- libary axios
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
- contoh axios
axios.get("https://jsonplaceholder.typicode.com/posts");
.then(function (response) {
// handle succes
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always execute
})
load content menggunakan axios
function loadContent() {
document.getElementById("btn-load").innerHTML = "Loading...";
document.getElementById("btn-load").setAttribute("disable", "true");
axios.get("https://jsonplaceholder.typicode.com/posts")
.then(function (response) {
var template = response.data.map(post => {
return `
<h3>${post.title}</h3>
<p>${post.body}</p>
<hr>
`
}).join("")
document.getElementById("result").innerHTML = template;
}).catch(function (error) {
// handle error
console.log(error)
})
.then(function () {
document.getElementById("btn-load").innerHTML = "done;"
document.getElementById("btn-load").removeAttribute("disable");
})
}