Nuxt-Axios post with header and body

nuxt

#1

Where i’m mistake?

when i make post request in some html file like that.

<head>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
    <script>
        axios({
        method: 'post',
        url: 'http://some.com/some/',
        headers: {"token": "aspsi39393dndj383", "Content-Type": "application/json"},
        data: {
          page: 1
        }
      });
    </script>
</body>

everything work well, it’s return me 200 ok status but when i try it in nuxt-axios via vuex store like:

await this.$axios.post('http://some.com/some/', {headers: {"token": "aspsi39393dndj383", "Content-Type": "application/json"},
                    data: {
                      page: 1
                    }}).then(response => {
                        // If request is good...
                        console.log(response.data);
                      })
                      .catch((error) => {
                        console.log('error 3 ' + error);
                      });

it’s return me cors and internal server errors.


#2

When you use async await you could just

async getProduct() {
  let product = await this.$axios.post('http://some.com/some/', ..data )
  console.log(product.data)
}

and no need to use .then() this wouldn’t solve your answer though but i still need some hint. you could reproduce your code on here codesandbox

I would suggest to check if there is any server issue or not.


#3

thanks but how here add headers and body ?

so if like that: im getting error:

let product = await this.$axios.post(url, headers: {"tboKen": "aVRmYOthfEGRmmaXDB7pow", "Content-Type": "application/json"},
                    data: {
                      page: 1
                    })
                    console.log(product.data)