如何集成 SvelteKit 和 Google Forms?
SvelteKit 于 1 月 19 日发生的重大变更(请参阅此处了解详细信息)意味着我的 Google 表单集成不再工作了。
首先让它工作是一个小困难,而且我无法将其更新 - 我反复收到错误消息,“要访问请求正文,请使用 text/json/arrayBuffer/formData 方法,例如 body = wait request.json()
”,以及 GitHub 对话的链接。
这是我的 Contact
组件……
<script>
let submitStatus;
const submitForm = async (data) => {
submitStatus = 'submitting';
const formData = new FormData(data.currentTarget);
const res = await fetch('contact.json', {
method: 'POST',
body: formData
});
const { message } = await res.json();
submitStatus = message;
};
const refreshForm = () => {
/* Trigger re-render of component */
submitStatus = undefined;
};
</script>
这是相应的 contact.json.js
:
export const post = async (request) => {
const name = request.body.get('name');
const email = request.body.get('email');
const message = request.body.get('message');
const res = await fetch(`URL TO RELEVANT GOOGLE FORM GOES HERE`);
if (res.status === 200) {
return {
status: 200,
body: { message: 'success' }
};
} else {
return {
status: 404,
body: { message: 'failed' }
};
}
};
任何帮助将不胜感激!
SvelteKit's breaking change of Jan 19 (see here for details) means that my Google Forms integration is no longer working.
It was a minor struggle to get it working in the first place, and I can't bring this up to date — I repeatedly get the error message, "To access the request body use the text/json/arrayBuffer/formData methods, e.g. body = await request.json()
", and a link to the GitHub conversation.
Here's my Contact
component...
<script>
let submitStatus;
const submitForm = async (data) => {
submitStatus = 'submitting';
const formData = new FormData(data.currentTarget);
const res = await fetch('contact.json', {
method: 'POST',
body: formData
});
const { message } = await res.json();
submitStatus = message;
};
const refreshForm = () => {
/* Trigger re-render of component */
submitStatus = undefined;
};
</script>
... and here's the corresponding contact.json.js
:
export const post = async (request) => {
const name = request.body.get('name');
const email = request.body.get('email');
const message = request.body.get('message');
const res = await fetch(`URL TO RELEVANT GOOGLE FORM GOES HERE`);
if (res.status === 200) {
return {
status: 200,
body: { message: 'success' }
};
} else {
return {
status: 404,
body: { message: 'failed' }
};
}
};
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,修复相对简单,只涉及对现有代码的微小更改。我必须访问
event.request
(解构为request
),并根据 这个对类似问题的回答。因此,在那之后,contact.json.js
看起来像...(另请注意,整个表单基于 此由 WebJeda 制作的视频,该视频现在不适用于最新的 SvelteKit 版本,但将通过这个简单的更改。)
The fix is, in fact, relatively simple, and involved only a tiny change to the existing code. I had to access
event.request
(destructured torequest
), and proceed from there, prompted by this answer to a similar question. So, after that,contact.json.js
looks like...(Note, too, that this whole form was based upon this video by WebJeda, which won't now work with the latest SvelteKit build, but will with this simple alteration.)