Axios is a simple promise based HTTP client for the browser and node.js. Axios
provides a simple to use library in a small package with a very extensible.
Just like javascript fetching functionalities, axios provides the the fetching
data functionality from api.
Install axios using npm:
$ npm install axios
Install axios Using bower:
$ bower install axios
Install axios using yarn:
$ yarn add axios
Import axios as follows:
const axios = require('axios').default;
// Make a get request with axios sample code
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
You can start learning on axios from the below website:
https://axios-http.com/docs/intro
import axios from 'axios';
async makeRequest() {
try {
const response = await axios.post('your_request_url_here', {
// Enter your body parameters here
});
}
catch(e) {
// Handle your error here
}
}
// Axios returns a promise and hence you can use .then(), .catch for error
// handling if you don't want to use async/await(use try/catch for error
// handling)
const onSubmit = async (FormData) => {
FormData.bFlats = meters;
try {
let res = await axios({
method: 'post',
url: 'http://localhost:5000/api/v1/buildings',
data: FormData
});
console.log(res.data);
if (res.data.status === 'success') {
alert("Successfully Inserted");
reset();
}
} catch (error) {
console.log(error.response.data.message); // this is the main part. Use the response property from the error object
}
}
async function send()
{
let data = new FormData();
data.append("title", "title ???");
data.append("note", "note");
data.append("csrfmiddlewaretoken", '{{csrf_token}}') // 3
let res = await axios.post("/blog/third", data);
console.log(res.data.my_data);
}
def third(request):
if request.method=="POST":
title = request.POST["title"]
note = request.POST["note"]
data = {
'my_data': title
}
return JsonResponse(data)
#much simpler than fetch