0
0
Vueframework~20 mins

POST requests for form submission in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue POST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Vue component submits the form?

Consider this Vue 3 component using the Composition API. What will be the console output after submitting the form?

Vue
<template>
  <form @submit.prevent="submitForm">
    <input v-model="username" placeholder="Username" />
    <button type="submit">Submit</button>
  </form>
</template>

<script setup>
import { ref } from 'vue'

const username = ref('')

async function submitForm() {
  const response = await fetch('/api/login', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ username: username.value })
  })
  const data = await response.json()
  console.log(data.message)
}
</script>
ALogs undefined because username is not reactive
BThrows a syntax error because fetch is used incorrectly
CLogs the server response message if the POST is successful
DPrevents form submission but does not send any request
Attempts:
2 left
💡 Hint

Look at how username is used and how fetch is called with await.

📝 Syntax
intermediate
1:30remaining
Which option correctly sends a POST request with JSON body in Vue?

Choose the code snippet that correctly sends a POST request with JSON data inside a Vue method.

Afetch('/api/data', { method: 'POST', body: JSON.stringify({ name: 'Alice' }) })
Bfetch('/api/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Alice' }) })
Cfetch('/api/data', { method: 'POST', headers: { 'Content-Type': 'text/plain' }, body: { name: 'Alice' } })
Dfetch('/api/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: { name: 'Alice' } })
Attempts:
2 left
💡 Hint

Remember to set the correct Content-Type header and stringify the body.

🔧 Debug
advanced
2:30remaining
Why does this Vue POST request fail with a CORS error?

This Vue component sends a POST request to an external API but gets a CORS error in the browser. What is the most likely cause?

Vue
<script setup>
import { ref } from 'vue'
const data = ref('')
async function sendData() {
  const res = await fetch('https://external.api/submit', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ data: data.value })
  })
  console.log(await res.json())
}
</script>
AThe external API does not allow cross-origin POST requests from this domain
BThe fetch call is missing the 'mode' option set to 'cors'
CVue's reactivity system blocks cross-origin requests
DThe JSON body is not stringified properly
Attempts:
2 left
💡 Hint

Think about browser security rules for cross-origin requests.

state_output
advanced
2:00remaining
What is the value of 'status' after submitting this form?

In this Vue component, what will be the value of status after the form is submitted and the server responds with { success: true }?

Vue
<template>
  <form @submit.prevent="submit">
    <input v-model="input" />
    <button type="submit">Send</button>
  </form>
  <p>{{ status }}</p>
</template>

<script setup>
import { ref } from 'vue'
const input = ref('')
const status = ref('idle')

async function submit() {
  status.value = 'sending'
  const res = await fetch('/api/send', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ input: input.value })
  })
  const data = await res.json()
  if (data.success) {
    status.value = 'success'
  } else {
    status.value = 'error'
  }
}
</script>
A'idle'
B'sending'
C'error'
D'success'
Attempts:
2 left
💡 Hint

Look at how status changes after the fetch call.

🧠 Conceptual
expert
1:30remaining
Why use async/await with POST requests in Vue form submission?

Which is the best explanation for using async/await when sending POST requests in Vue form submission?

AIt allows writing asynchronous code in a readable way and waits for the server response before proceeding
BIt automatically retries the POST request if it fails
CIt blocks the UI thread until the request finishes, preventing user interaction
DIt converts the POST request into a synchronous call to improve performance
Attempts:
2 left
💡 Hint

Think about how async/await affects code flow and readability.