Skip to content

Instantly share code, notes, and snippets.

@RafaelFigueiredo RafaelFigueiredo/ajax.js
Last active Mar 30, 2020

Embed
What would you like to do?
// read documentation of Axios at https://github.com/axios/axios
// <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> - client-side
//
const axios = require('axios')
const Object = (function(){
const endpoint = "/api/object"
return {
create: function(form_data){
axios.post(endpoint, form_data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
},
read: function(id){
// Make a request for a user with a given ID
axios.get(`/${endpoint}?id=${id}`)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
},
update: function(id, form_data){
// Make a request for a user with a given ID
axios.put(`/${endpoint}?id=${id}`, form_data)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
},
delete: function(id){
// Make a request for a user with a given ID
axios.delete(`/${endpoint}?id=${id}`)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
},
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.