如何集成 SvelteKit 和 Google Forms?

发布于 2025-01-12 17:35:55 字数 1461 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-01-19 17:35:55

事实上,修复相对简单,只涉及对现有代码的微小更改。我必须访问 event.request (解构为 request),并根据 这个对类似问题的回答。因此,在那之后, contact.json.js 看起来像...

export const post = async ({ request }) => {
    const body = await request.formData();
    const name = body.get('name');
    const email = body.get('email');
    const message = body.get('message');

    const response = await fetch(`URL TO RELEVANT GOOGLE FORM GOES HERE`);

    if (response.status === 200) {
        return {
            status: 200,
            body: { message: 'success' }
        };
    } else {
        return {
            status: 404,
            body: { message: 'failed' }
        };
    }
};

(另请注意,整个表单基于 由 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 to request), and proceed from there, prompted by this answer to a similar question. So, after that, contact.json.js looks like...

export const post = async ({ request }) => {
    const body = await request.formData();
    const name = body.get('name');
    const email = body.get('email');
    const message = body.get('message');

    const response = await fetch(`URL TO RELEVANT GOOGLE FORM GOES HERE`);

    if (response.status === 200) {
        return {
            status: 200,
            body: { message: 'success' }
        };
    } else {
        return {
            status: 404,
            body: { message: 'failed' }
        };
    }
};

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文