我可以使用JS fetch()API从Formspark获取表单数据吗?

发布于 2025-01-31 09:50:25 字数 878 浏览 0 评论 0 原文

当我从 formspark文档我们可以使用 get get formspark中的方法。如果我正确理解,则 get 方法在JS fetch()API中表示我们要从某个地方检索数据。因此,我编写了此代码以获取使用JS Fetch方法的数据(从Formspark中提交的数据之前创建的数据:

async function fetchText() {
    let response = await fetch('https://submit-form.com/my-id', {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        }
    })
    let data = await response.json();
    console.log(data);
    return data;
}

fetchText().then(r => console.log(r));

但这给了我一个空的对象作为响应数据!虽然我的帐户中有六个提交的内容。有人可以帮我,我的代码中的错误吗?

As I read from formspark documentation we can use GET methods in formspark. If I understood correctly, the GET method in JS fetch() API means that we want to retrieve data from somewhere. So I wrote this code to get my data (data that were created before from submissions in formspark) with JS fetch method:

async function fetchText() {
    let response = await fetch('https://submit-form.com/my-id', {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        }
    })
    let data = await response.json();
    console.log(data);
    return data;
}

fetchText().then(r => console.log(r));

But it gives me an empty object as a response data! while I have six submissions in my account now. Could someone please help me that what is wrong in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

东京女 2025-02-07 09:50:25

后端返回以下消息此表单不再存在

如果我对文档中的URL样本进行相同的操作。它给我以下错误

“另一个服务器错误”

因此,您需要做什么是用URL中的表单ID替换 my-id (例如 https:// submit-form.com/22 )。

这是应用更改的片段

async function fetchText(formId) {
    let response = await fetch(`https://submit-form.com/${formId}`, {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        }
    })
    let data = await response.json();
    console.log(data);
    return data;
}

const targetFormId = 441; // Here should be a valid actual form ID

fetchText(targetFormId).then(r => console.log(r));

The backend returns the following message This form does not exist anymore.

Server error

If I do the same with the URL sample from the documentation. It gives me the following error

Another server error

So what do you need to do is to replace the my-id with your form ID in the URL (e.g https://submit-form.com/22).

Here is the snippet with the change applied

async function fetchText(formId) {
    let response = await fetch(`https://submit-form.com/${formId}`, {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        }
    })
    let data = await response.json();
    console.log(data);
    return data;
}

const targetFormId = 441; // Here should be a valid actual form ID

fetchText(targetFormId).then(r => console.log(r));

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