axios post request to send form data

后端 未结 9 2411
走了就别回头了
走了就别回头了 2020-11-22 03:40

axios POST request is hitting the url on the controller but setting null values to my POJO class, when I go through developer tools in chrome, the payload conta

9条回答
  •  臣服心动
    2020-11-22 03:59

    2020 ES6 way of doing

    Having the form in html I binded in data like so:

    DATA:

    form: {
       name: 'Joan Cap de porc',
       email: 'fake@email.com',
       phone: 2323,
       query: 'cap d\ou'
       file: null,
       legal: false
    },
    

    onSubmit:

    async submitForm() {
      const formData = new FormData()
      Object.keys(this.form).forEach((key) => {
        formData.append(key, this.form[key])
      })
    
      try {
        await this.$axios.post('/ajax/contact/contact-us', formData)
        this.$emit('formSent')
      } catch (err) {
        this.errors.push('form_error')
      }
    }
    

提交回复
热议问题