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 createPostRequest = async () => {
try{
const { data } = await axios.post(url, body_data, {
headers: {
'Authorization': `Basic ${token}`
},
})
console.log(data)
} catch (error) {
console.log(error)
}
}
createPostRequest();
const handleClick = async () => {
let fd = new FormData();
fd.append("name", name);
fd.append("password", password);
const rep = await axios
.post("http://localhost/php/api/students/create.php", fd)
.catch((err) => {
console.log(err, "axios error");
});
console.log(rep.data);
};
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
}
}
import Axios from "axios";
var res = await Axios.post('/search', {searchTerm: 'searchTerm'});
axios.get('/api/updatecart', {
params: {
product: this.product
}
}).then(...)
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