3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

axios.get()で"TypeError: Cannot use 'in' operator to search for 'validateStatus' in x"

Last updated at Posted at 2021-02-02

概要

vuexaxios.get()する際に以下のコードで実装したらタイトルのエラーにぶち当たったのでその解決策を起筆します。

.vue
<script>
export default {
    props:{
        postId: String //postId = x;
    },
    mounted(){
        this.$store.dispatch('individual/getIndividual', this.postId);
    }
}
</script>
.js
const actions = {
    getIndividual(context, data){
        axios.get('/api/individual',data).then((result)=>{
        //省略
        }).catch(error=>{
        //省略
        })
    }
};

原因

axios.get()をする時には、第二引数に直接パラメータを記述するのではなく、送りたいパラメータをparamsとして指定してあげる必要があります。

解決策

.js
const actions = {
    getIndividual(context, data){
        axios.get('/api/individual',data).then((result)=>{
        //省略
        }).catch(error=>{
        //省略
        })
    }
};

ではなく、

.js
const actions = {
    getIndividual(context, data){
        axios.get('/api/individual',{
            params:{
                post_id: data
            }
        }).then((result)=>{
            //省略
        }).catch(error=>{
            //省略
        })
    }
};

参考文献

公式GitHub
axiosのパラメータ指定方法まとめ

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?