update
I successfully created a new function based on the helpful answer provided below
export function getFormDataAsJson(e) {
const jsondata = {}
let fd= new FormData(e.target)
for (let key of fd.entries()) {
jsondata[key[0]]=key[1]
}
return jsondata
}
original issue
I encountered some difficulties while trying to extract data from a form and convert it into JSON format.
I noticed that the data is located in submitEvent.target
(as shown in the screenshot), but I kept getting undefined
or null
as the result.
<template>
<form @submit.prevent="submit">
<input id="aa"/>
<button type="submit">ok</button>
</form>
</template>
<script setup>
function submit(e) {
console.log(e)
let fd= new FormData(e.target)
console.log(fd)
console.log(e.formData)
console.log(fd.entries())
}
</script>
output
https://i.sstatic.net/2Q4S9.png
https://i.sstatic.net/ddXVo.png