2

I create a form using GET from API. After I've created that form, I want to POST the form data back to the API, encoded as JSON. How do I do that?

I have this simple text field:

<template v-if="questions.question_type == 1">
 <div class="title-1"><b>{{questions.question_title}} -</b> {{questions.question_desc}}
 </div>
 <v-text-field required :v-model="questions.question_tag" color="green darken-1" clearable></v-text-field>
</template>

How do I validate and POST the data?

3
  • The actual POST doesn't seem to be related to vue or vuetify but javascript in general. For the validation you need to be a little more specific, for example: What kind of validation do you want to do? It would really help if you added your <script> </script> block too Commented Mar 10, 2020 at 12:47
  • ok I want to POST data first Commented Mar 10, 2020 at 13:16
  • Sidenote: "According to the HTML5 specification, the <b> tag should be used as a LAST resort when no other tag is more appropriate." (w3schools) Not adopting these as a habit, even in draft code, might be a worthwhile idea. Commented Jun 29, 2023 at 11:16

1 Answer 1

3

Since I don't know what you're using to call your API I just assume you're using Axios.

Easiest would be to create a method and in that method you create a POST.

data() {
  return {
    questions: {
      question_tag: "" // Your value in your v-text-field
    }
  }
},
methods: {
  sendData() {
    axios.post('https://yourServer/your/api/endpoint', {
      questionTags: this.questions.question_tag
    })
    .then(response => {
      console.log(response);
    })
    .catch(error => {
      console.log(error);
    });
  }
}

To handle validation we need more information on what you're trying to validate. Empty textboxes? Only specific inputs such as numbers? Check for a maximum character count?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I use Axios. Thanks. But my form get data by API too, so how to select question with answer together?
I'm really sorry but I don't understand what you're asking of. Can you maybe provide a codesandbox or codepen as an example?
In line 14 you set this.survey, but survey was never defined in the data(). I suggest you check out more simple vue examples before diving into APIs. Adressing everything in the codepen would be too much for a comment here, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.