Performance: POST requests for form submission
This affects page load speed and interaction responsiveness when submitting forms.
methods: { async submitForm() { try { this.isLoading = true; const response = await fetch('/api/submit', { method: 'POST', body: JSON.stringify(this.formData), headers: { 'Content-Type': 'application/json' } }); const data = await response.json(); this.isLoading = false; this.successMessage = 'Submitted successfully!'; } catch { this.isLoading = false; this.errorMessage = 'Submission failed.'; } } }methods: { submitForm() { fetch('/api/submit', { method: 'POST', body: JSON.stringify(this.formData), headers: { 'Content-Type': 'application/json' } }).then(response => response.json()).then(data => { alert('Submitted!'); }).catch(() => { alert('Error'); }); } }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Async fetch with alert | Minimal DOM changes | 0 reflows but blocks input | Blocks paint during alert | [X] Bad |
| Async fetch with reactive state | Updates loading and message nodes | 1 reflow for message update | Paint cost low and non-blocking | [OK] Good |
.prevent to stop default browser actions.submit, so @submit.prevent stops the default submit and allows custom handling.body property as a string, so use JSON.stringify(data).async submitForm() {
const response = await fetch('/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(this.formData)
});
if (response.ok) {
console.log('Success');
} else {
console.log('Failed');
}
}response.ok is true.response.ok is true, it logs 'Success'. Otherwise, it logs 'Failed'.submit() {
fetch('/api', {
method: 'POST',
body: this.formData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
}this.formData must be stringified.JSON.stringify(this.formData), the server may not receive data correctly.submit() {
fetch('/submit', { method: 'POST', body: JSON.stringify(this.form) })
.then(res => res.json())
.then(data => { if (data.success) alert('Submitted!'); });
} uses promise chaining correctly without async/await, which is valid. async submit() {
const res = await fetch('/submit', { method: 'POST', body: JSON.stringify(this.form) });
const data = await res.json();
if (data.success) alert('Submitted!');
} uses async/await but misses headers for JSON content.submit() {
fetch('/submit', { method: 'POST', body: JSON.stringify(this.form) })
.then(res => res.json())
.then(data => { if (data.success) alert('Submitted!'); });
} is the best because it correctly stringifies and processes JSON response; headers are optional but recommended.Pass the test and this pattern moves unknown