用axios接连发布请求Vuejs

比塞比乌

我有2个发布请求,如果第一个被触发,则第二个必须等待响应并执行发布请求。我读了一些有关等待和异步的内容,但是效果不好。

let changed =false;

if(//something then){
    changed = true;
    axios.post('/uploadfile/' + id,  // This must be first
        formData,
    {
    headers: {
        'Content-Type': 'multipart/form-data',
    }
    })
    .then(function(response){
        // get response and do something
    })
    .catch(function(){
        //
    }); 
}

// if changed = true, then await for response from first post
axios.post('/uploadfile' + this.id_contract,  // If 
    data,
    {
    headers: {
        Authorization: getToken()
    }
})
.then(function(){
    // do something else
})
.catch(function(){
    //
});
汤米

您可以像这样简单地使用async / await:

async myMethod() {
  let response1 = await axios.post('/1', ...)
  let response2 = await axios.post('/2', ...)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章