【问题标题】:Axios then function also does catch?axios then 函数也确实catch?
【发布时间】:2019-06-11 17:47:03
【问题描述】:

我的帖子成功了,但是除了console.log()之外我不能在then语句中做任何事情;它总是跳到抓住。

axios.post('/group/15/discussion/'+ this.discussion.id+ '/schedule',this.form, {handleErrors:true})
                    .then(function(response) {
                        //this.form.valid = true;
                        console.log("success");

                        console.log('set form valid');
                        this.$refs['schedule-group-discussion-'+this.id].hide();
                        console.log('close modal');
                        /*this.$emit('updateDiscussion',response.data.discussion);
                        this.$toast.success(response.data.message,'Success!',{icon: 'fas fa-check-circle'});*/
                    })
                    .catch((error) => {
                        console.log("catch");
                        this.form.valid = false;
                    });

这是我的控制台日志,按顺序排列: 成功 设置表格有效 赶上

怎么了?

【问题讨论】:

  • 你的 POST 响应状态码 200?
  • 是的,它确实有代码 200
  • 根据控制台日志输出,可以假设 this.$refs['schedule-group-discussion-'+this.id].hide();产生错误。您是否尝试过检查/记录捕获的错误?
  • @DavidHorák 是的,你是对的。它说“这个”是未定义的/
  • @JasonSpick 给你,这个问题与 axios 无关,而是与未定义的引用有关,应该很容易解决。

标签: vue.js vuejs2 axios


【解决方案1】:

问题是您的this 范围。你实际上得到了一个错误,cannot read property valid of undefined,但你没有console.log(error)。您还需要在.then() 中使用fat arrow function

.then(function(response) {

.then((response) => {

现在this 的范围正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 2019-04-28
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多