我知道这可能是个愚蠢的问题,但我想不通:
我有一个输入表单:
<form @submit.prevent="customSubmit">
<label>Name</label>
<input type="text" v-model="newUser.name" id="name" placeholder="Your Name">
<label>E-mail:</label>
<input type="email" v-model="newUser.email" id="email" placeholder="Your Email-Address">
<label>Mobile Number</label>
<input type="number" v-model="newUser.number" id="number" placeholder="Your mobile number" @keyup.enter="customSubmit">
</form>
<button type="button" class=buttonSignup @click="customSubmit">Submit</button>数据经过处理并通过以下函数被提取到一个小的nodeJS-Server中:
customSubmit(){
//Check ob alle Felder gefüllt sind
if(document.getElementById('name').value === ''){return}
if(document.getElementById('email').value === ''){return}
if(document.getElementById('number').value === ''){return}
//POST to API
const user = {
method: "POST",
headers: { "Content-Type": "application/json"},
body: JSON.stringify({newUser: this.newUser})
};
fetch("http://localhost:3080/api/user", user)
.then(response => response.json())
.then(data => console.log(data));
this.pushFunction(); //GET-Request to view the data
this.clearForm();
}但是,为了能够发送DELETE-Request,我需要为customSubmit()-function中的每个条目分配唯一的ID。
有人能告诉我怎样才能做到这一点吗?
提前感谢!
另外,请忽略代码片段中的德语注释。
发布于 2021-05-02 08:02:54
您应该让您的节点服务器创建一个条目,并发送回一个惟一的id作为对您发送的POST请求的响应。
ID可以是数据库发出的ID(取决于您使用的数据库),然后它将成为从fetch返回的data对象的一部分。
https://stackoverflow.com/questions/67351561
复制相似问题