【问题标题】:receiving a json response with vue.js2 in laravel在 laravel 中接收带有 vue.js2 的 json 响应
【发布时间】:2017-09-12 05:39:56
【问题描述】:

发送请求并获得基于它的响应这个

我正在尝试使用 Vue 组件来访问它,但我发现错误状态未定义,这是我的 Vue 组件

<script>
    export default {
        mounted() {
                axios.get('/userfound/' + this.profile_user_id)
                .then(function (response) {
                    console.log(response);
                    this.status = response.data.status;                   
                })
                .catch(function (error) {
                    console.log(error);
                  });
                },
        props: ['profile_user_id'],
        data(){
            return {
                status: ''
            }        
        },
        methods:{
            add_friend(){
                // this.loading = false
                axios.get('/add_friend/' + this.profile_user_id)
                .then(function (response) {
                    console.log(response);
                    if (response.data == 1) {
                        this.status = 'waiting'
                    }

                })
                .catch(function (error) {
                    console.log(error);
                  });
                }
            }
        }
</script>

为什么会出现此错误:TypeError: cannot read property 'status' of undefined .. 我试过 "this.status = response.body.status""this.status = response.data.status" 但都没有工作

【问题讨论】:

    标签: json vuejs2 laravel-5.4 vue-component


    【解决方案1】:

    我认为变量的范围存在问题。试试下面的答案:

    <script>
    export default {
        mounted() {
                var self = this;
                axios.get('/userfound/' + self.profile_user_id)
                .then(function (response) {
                    console.log(response);
                    self.status = response.data.status;                   
                })
                .catch(function (error) {
                    console.log(error);
                  });
                },
        props: ['profile_user_id'],
        data(){
            return {
                status: ''
            }        
        },
        methods:{
            add_friend(){
                // you can do same here as well
                var self = this;
                axios.get('/add_friend/' + self.profile_user_id)
                .then(function (response) {
                    console.log(response);
                    if (response.data == 1) {
                        self.status = 'waiting'
                    }
                })
                .catch(function (error) {
                    console.log(error);
                  });
                }
            }
        }
    </script>
    

    【讨论】:

    • 当我在当前页面时它可以工作,但是当我刷新它时会出现同样的错误
    • 这很奇怪。尝试使用 chrome devtools 进行调试。
    • 我已经在此处发布了该代码并且它可以工作,我不知道为什么,它正在记录正确的值,但是当我尝试使用它进行测试时,它似乎不起作用,检查什么我发布了
    • 这是错误的status = response.data.status。变量状态不是指您在组件中的data 中创建的status
    • 我将其改回 status = response.body.status 并再次开始显示错误
    猜你喜欢
    • 2013-08-26
    • 1970-01-01
    • 2018-02-05
    • 2016-10-26
    • 2020-09-16
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多