admin管理员组文章数量:1430489
I'am sending data to server with axios library
the query is working, but i'am not able to wait the answer of axios
import axios from '../helper/axiosAPI'
/** data send to axios */
let formData = new FormData();
formData.append('file',this.file);
/**Axios query */
try{
axios.post(formData)
.then(res=> console.log('result : ' + res)) // no data recieve
}
catch(err){
console.log(err)
}
here my post request (axiosAPI file):
axios = require('axios').default;
const uri = process.env.VUE_APP_URI;
const endPoint = 'professional/upload'
exports.post = async (formData) =>{
axios({
method:'post',
url:uri + endPoint,
data:formData,
headers:{
"Content-Type": "Content-Type: multipart/form-data"
}
})
.then(response => {
console.log('axios : ' + response) // response is recieved
return response.data })
.catch(err => console.log(`Erreur AXIOS API : + ${err}`));
}
my Axios query work well and data is post to server. But i am not succed to wait the reponse from the server.
console.log('result : ' + res)=> (res is undefined -> not waiting for axios response)
console.log('axios : ' + response) =>(response is recieve from server)
It will be very nice if you help me to find a solution to wait axios result
thanks a lot
I'am sending data to server with axios library
the query is working, but i'am not able to wait the answer of axios
import axios from '../helper/axiosAPI'
/** data send to axios */
let formData = new FormData();
formData.append('file',this.file);
/**Axios query */
try{
axios.post(formData)
.then(res=> console.log('result : ' + res)) // no data recieve
}
catch(err){
console.log(err)
}
here my post request (axiosAPI file):
axios = require('axios').default;
const uri = process.env.VUE_APP_URI;
const endPoint = 'professional/upload'
exports.post = async (formData) =>{
axios({
method:'post',
url:uri + endPoint,
data:formData,
headers:{
"Content-Type": "Content-Type: multipart/form-data"
}
})
.then(response => {
console.log('axios : ' + response) // response is recieved
return response.data })
.catch(err => console.log(`Erreur AXIOS API : + ${err}`));
}
my Axios query work well and data is post to server. But i am not succed to wait the reponse from the server.
console.log('result : ' + res)=> (res is undefined -> not waiting for axios response)
console.log('axios : ' + response) =>(response is recieve from server)
It will be very nice if you help me to find a solution to wait axios result
thanks a lot
Share Improve this question asked Sep 23, 2021 at 21:02 aviateur22aviateur22 451 silver badge8 bronze badges 1-
Your problem is the
return response.data
. It is not returning the value to thepost
function but to thethen
lambda function. You could instead use either Promise resolve syntax or full async await syntax. This mix and match here is confusing and causing unwanted behavior. – Gonnen Daube Commented Sep 23, 2021 at 21:12
2 Answers
Reset to default 4You can use the await
with axios:
Like this:
try
{
let res = await axios.post(formData);
console.log('result : ' + res); // no data received
}
catch(err)
{
console.log(err);
}
and this:
const axios = require('axios').default;
const uri = process.env.VUE_APP_URI;
const endPoint = 'professional/upload';
exports.post = async (formData) =>{
try
{
let response = await axios(
{
method: 'post',
url: uri + endPoint,
data: formData,
headers:
{
"Content-Type": "Content-Type: multipart/form-data"
}
}
);
console.log('axios : ' + response); // response is received
return response.data;
}
catch (err)
{
console.log(err);
}
}
See also:
https://stackoverflow./a/49661388/194717
https://www.npmjs./package/axios
Since your handle is async so you need to put await before axios calling.
exports.post = async (formData) =>{
return await axios({
method:'post',
url:uri + endPoint,
data:formData,
headers:{
"Content-Type": "Content-Type: multipart/form-data"
}
})
本文标签: javascriptaxios APIwait for responseStack Overflow
版权声明:本文标题:javascript - axios API - wait for response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745557480a2663271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论