使用Gridsome / Vuejs的Netlify表单

时间:2019-04-12 21:01:54

标签: vue.js netlify gridsome

因此,我尝试使用netlify表单处理表单请求。一切正常,除了数据不随表格发送。

这是表格

<template>
    <form @submit.prevent="handleSubmit" name="Free Assessment Form" method="post" data-netlify-honeypot="bot-field">
      <span class="slogan">{{ slogan }}</span>
      <input type="hidden" name="form-name" value="contact"  class="hidden"/>
      <input @input="ev => formData.business_name = ev.target.value" type="text" name="Business Name" placeholder="Business Name" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.first_name = ev.target.value" type="text" name="First Name" placeholder="First Name" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.last_name = ev.target.value" type="text" name="Last Name" placeholder="Last Name" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.email = ev.target.value" type="email" name="Email" placeholder="Email" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.phone = ev.target.value" type="text" name="Phone Number" placeholder="Phone Number" :class="{'input-error': error}" @change="error = false">
      <button type="submit" class="form-btn">Submit</button>
    </form>
</template>

<script>
export default {
  name: 'Form',
  props: ['slogan'],
    data() {
    return {
      formData: {},
      error: false
    }
  },
  methods: {
    encode(data) {
    return Object.keys(data)
      .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
      .join('&')
    },
    handleSubmit(e) {
      fetch('/', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: this.encode({
          'form-name': e.target.getAttribute('name'),
              ...this.formData
        }),
      })
      .then(() => {
        this.formData = ""
        this.$router.push('/')
        alert('Form Submitted!')
      })
      .catch(error => alert(error))
    }
  }
}
</script>

现在,一旦提交表单,我将按预期收到我的电子邮件,但是,我仅获得表单密钥。我没有收到表单键值。

在我的本地开发中,当我console.log传递到encoding方法中的data时,就已经存在了,所以我不明白为什么它不起作用。

0 个答案:

没有答案