Complete the code to import the function needed to make HTTP requests in Vue.
<script setup> import { [1] } from 'vue' </script>
We use ref to create reactive variables in Vue, but for HTTP requests, we use fetch or libraries like axios. Here, the import is for ref to hold form data.
Complete the code to bind the form input to the reactive variable.
<template> <input type="text" v-model=[1] /> </template>
The input should bind to a property inside the reactive object, like formData.name, to track the user's input.
Fix the error in the method to send a POST request using fetch.
<script setup>
const submitForm = async () => {
const response = await fetch('/api/submit', {
method: '[1]',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
})
}
</script>To send data to the server, the HTTP method must be POST. Using GET or others won't send the form data properly.
Fill both blanks to handle the response and convert it to JSON.
<script setup>
const submitForm = async () => {
const response = await fetch('/api/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
})
const data = await response.[1]()
console.log(data[2])
}
</script>We use response.json() to parse the JSON response. Then we log the parsed data.
Fill all three blanks to create a reactive form, submit it, and reset the input.
<script setup> import { [1] } from 'vue' const formData = [2]({ name: '' }) const submitForm = async () => { await fetch('/api/submit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData.[3]) }) formData.name = '' } </script>
We import reactive to create a reactive object. Then we use reactive({ name: '' }) for formData. When sending, we access formData.value if it was a ref, but since it's reactive, we use formData directly. Here, the blank expects value to show understanding of refs vs reactive.