簡體   English   中英

從 URL 中提取令牌並使用 Axios 發送 post 請求 - Vue js

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

我一直在嘗試從http://test.com/confirm?token=MMsndiwhjidh...提取令牌,然后將發布請求發送到另一台服務器。

我試過這個:

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
        })
    }
}

我收到以下錯誤:

錯誤日志

錯誤日志

我想我無法正確提取令牌。

原因是您在 then / catch 塊中使用聲明性函數而不是箭頭函數。 this不是指同一件事(這里, this不是您的 Vue 組件)。

像這樣嘗試:

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

我不會嘗試自己解釋差異,因為網上有很多關於它的文章。 這是一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM