Sveltekit 制作联系表单的端点问题

发布于 2025-01-19 21:37:23 字数 2961 浏览 1 评论 0原文

我正在制作联系表,但遇到了 发布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 of
POST 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 技术交流群。

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

发布评论

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

评论(1

北城孤痞 2025-01-26 21:37:24

由于您要传递 formData,因此您应该使用正确的方法:

const data = await request.formData();
const name = data.get("name");

或者您可以从 formData 创建一个对象:

const data = Object.fromEntries(await request.formData());
console.log(data.name);

Since you are passing formData you should use the correct methods:

const data = await request.formData();
const name = data.get("name");

alternatively you can create an object from the formData:

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