简体   繁体   English

从 URL 中提取令牌并使用 Axios 发送 post 请求 - Vue js

[英]Extracting token from the URL and sending a post request using Axios - Vue js

I have been trying to extract a token from say http://test.com/confirm?token=MMsndiwhjidh... and then send a post request to another server.我一直在尝试从http://test.com/confirm?token=MMsndiwhjidh...提取令牌,然后将发布请求发送到另一台服务器。

I have tried this:我试过这个:

export default {
    data() {
        return {
            confirmation : false,
            somethingWrong: false       
        }
    },
    
    created: function() {
        axios.post('/confirm', null, {
            method: 'post',
            params: {
                token:this.$route.query.token
            }
        })
        .then(function(res) {
            console.log(res)
            this.confirmation = true       
        })
        .catch(function(error) {
            console.log(error)
            this.somethingWrong = true
        })
    }
}

I got the following errors:我收到以下错误:

错误日志

错误日志

I think I am not able to extract the token properly.我想我无法正确提取令牌。

The reason is you're using declarative functions instead of arrow functions in your then / catch blocks.原因是您在 then / catch 块中使用声明性函数而不是箭头函数。 The this don't refer to the same thing (here, this is not your Vue component). this不是指同一件事(这里, this不是您的 Vue 组件)。

Try like this:像这样尝试:

.then((res) => {
    console.log(res)
    this.confirmation = true       
})

I won't try to explain the difference myself as there are plenty of articles on the web about it.我不会尝试自己解释差异,因为网上有很多关于它的文章。 Here's one 这是一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM