【发布时间】:2021-02-22 10:29:56
【问题描述】:
使用 sapper 模板,我在 netlify 上部署了我的应用程序。我想将netlify表单与sapper集成,所以我使用了docs表单结构。
<form name="contact" method="POST" data-netlify="true">
<p>
<label>Name<input type="text" bind:value={$user.name} /></label>
</p>
<p>
<label>E-mail<input type="text" bind:value={$user.email} /></label>
</p>
<p>
<label>Telephone<input type="text" bind:value={$user.phone} /></label>
</p>
<input type="hidden" name="form-name" value="contact" />
<button on:submit|preventDefault={submitForm} type="submit">Reach out</button>
</form>
就这样,表单在提交时被发送到 netlify,但它是空的。因此,以文档中的示例为例,我尝试创建自己的获取发布请求
let submitForm =(event)=>{
let formdata = new FormData();
formdata.append('name',$user.name);
formdata.append('email',$user.email);
formdata.append('telephone',$user.telephone);
fetch("/contact/", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: formdata,
})
.then(() => alert("Success!"))
.catch(error => alert(error));
event.preventDefault();
}
同样,在我的路由中,我添加了带有导出 POST 功能的 js 文件。
export async function post(req, res, next) {
/* Initializes */
res.setHeader('Content-Type', 'application/json')
/* Retrieves the data */
var data = req.body
/* Returns the result */
return res.end(JSON.stringify(data))
}
无论我做什么,我总是在尝试提交表单时立即收到 404。我在 fetch 中尝试了不同的 URL,我尝试在 js 文件中进行实际发布,什么都没有。互联网上似乎没有其他人在 sapper 上遇到此问题,请帮助!谢谢!
【问题讨论】:
标签: forms http svelte netlify sapper