Sveltekit 制作联系表单的端点问题
我正在制作联系表,但遇到了 发布http:// localhost:3000/contact.json 500(内部服务器错误)
at contactform.svelte:5
。 以下是contact.svelte
是:
<script>
const submitForm = async(data) => {
const formData = new FormData(data.currentTarget);
const res = await fetch("contact.json", {
method: "POST",
body: formData,
});
};
</script>
<form on:submit|preventDefault={submitForm}>
<div>
<label for="">
What's your name?
<input type="text" name="name">
</label>
</div>
<div>
<label for="">
What's your Email or Phone #?
<input type="email" name="email">
<input type="text" name="phone">
</label>
</div>
<div>
<label for="">
What would you like to tell us?
<input type="text" name="message">
</label>
</div>
<div>
<input type="submit">
</div>
</form>
在我的终端中,我还有另一个错误:
To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`. See https://github.com/sveltejs/kit/pull/3384 for details
Error: To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`. See https://github.com/sveltejs/kit/pull/3384 for details
at Object.get (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:2662:10)
at post (C:\Users\chian\Desktop\site\src\routes\contact.json.js:2:26)
at render_endpoint (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:157:25)
at resolve (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:2743:17)
at async respond (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:2688:20)
at async file:///C:/Users/chian/Desktop/site/node_modules/@sveltejs/kit/dist/chunks/index.js:299:24
contact.json.js:
export const post = async(request) => {
const name = request.body.get("name");
const email = request.body.get("email");
const phone = request.body.get("phone");
const message = request.body.get("message");
const res = await fetch(`https://docs.google.com/forms/d/e/1AFIpQLSfx-JOL3dNrjWL-raYmT4ay_TIT6xfFVBXaZ5R_m8RB-G_Ang/formResponse?usp=pp_url&entry.1204260556=${name}&entry.1586743587=${email}&entry.807821212=${phone}&entry.1059243301=${message}&submit=Submit`);
console.log(res);
};
我遵循了一个教程写作的教程,诚实地说,请不要在我''''''''时M New to Backend and Svelte总的来说,希望解决此问题可以使我对所有事情的运作方式有所了解。让我知道你们是否对如何解决这个问题有任何了解。
I'm making a contact form but am getting an error ofPOST http://localhost:3000/contact.json 500 (Internal Server Error)
at contactform.svelte:5
.
Here's what contactform.svelte
is:
<script>
const submitForm = async(data) => {
const formData = new FormData(data.currentTarget);
const res = await fetch("contact.json", {
method: "POST",
body: formData,
});
};
</script>
<form on:submit|preventDefault={submitForm}>
<div>
<label for="">
What's your name?
<input type="text" name="name">
</label>
</div>
<div>
<label for="">
What's your Email or Phone #?
<input type="email" name="email">
<input type="text" name="phone">
</label>
</div>
<div>
<label for="">
What would you like to tell us?
<input type="text" name="message">
</label>
</div>
<div>
<input type="submit">
</div>
</form>
In my terminal i have another error of:
To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`. See https://github.com/sveltejs/kit/pull/3384 for details
Error: To access the request body use the text/json/arrayBuffer/formData methods, e.g. `body = await request.json()`. See https://github.com/sveltejs/kit/pull/3384 for details
at Object.get (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:2662:10)
at post (C:\Users\chian\Desktop\site\src\routes\contact.json.js:2:26)
at render_endpoint (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:157:25)
at resolve (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:2743:17)
at async respond (file:///C:/Users/chian/Desktop/site/.svelte-kit/runtime/server/index.js:2688:20)
at async file:///C:/Users/chian/Desktop/site/node_modules/@sveltejs/kit/dist/chunks/index.js:299:24
Here's contact.json.js:
export const post = async(request) => {
const name = request.body.get("name");
const email = request.body.get("email");
const phone = request.body.get("phone");
const message = request.body.get("message");
const res = await fetch(`https://docs.google.com/forms/d/e/1AFIpQLSfx-JOL3dNrjWL-raYmT4ay_TIT6xfFVBXaZ5R_m8RB-G_Ang/formResponse?usp=pp_url&entry.1204260556=${name}&entry.1586743587=${email}&entry.807821212=${phone}&entry.1059243301=${message}&submit=Submit`);
console.log(res);
};
I followed a tutorial writing this and to be completely honest don't grasp everything yet as I'm new to backend and svelte in general and hope fixing this problem can get me some insight into how everything works. Let me know if y'all have any idea of how to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您要传递 formData,因此您应该使用正确的方法:
或者您可以从 formData 创建一个对象:
Since you are passing formData you should use the correct methods:
alternatively you can create an object from the formData: