简体   繁体   中英

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.

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. The this don't refer to the same thing (here, this is not your Vue component).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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