const axios = require('axios');
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
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