简体   繁体   中英

Nuxt.JS: How to get post parameter data in a vue file

I am integrating paytm payment gateway in my nuxt js project. Buy I am unable to get post data with which paytm is redirecting to my callback url.

Since asyncData gives you access to req and res (see https://nuxtjs.org/guide/async-data/#use-reqres-objects ), you can parse the body to get POST parameters. I found this solution out there, so I didn't try it out. In fact, it should be better to use a Middleware to parse this and load the middleware in that page.

<script>
export default {
  asyncData ({ req, res }) {
    if (process.server) {
      const qs = require('querystring')
      let body = ''
      let temp = ''
      let paymentData = ''

      while(temp = req.read()) {
        body += temp
      }

      paymentData = qs.parse(body)

      return { data: paymentData }
    }
  },

  data () {
    return {
      paymentData: '',
    }
  },

  mounted () {
    console.log(this.paymentData)
  }
</script>

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