将 prop 值作为查询字符串传递到 sveltekit 端点

发布于 2025-01-20 02:02:13 字数 1302 浏览 1 评论 0原文

我正在尝试将Prop值从文本输入字段传递到Sveltekit端点,但是我不知道如何通过它。

我的OnSubmit运行此功能,

const handleSubmit = () => {
        promise = getData();
    }

该功能调用一个函数,通过Sveltekit端点从外部API获取数据:

 async function getData() {
        const response = await self.fetch('api/address.json');
        const addressdata = await response.json()

        if (response.ok) {
        return addressdata;
            
        } 
}

终点看起来像这样,

export async function get({ }) {
    const baseurl = 'https://myaddressapi/?postcode=xxxxxx'
    const res = await fetch(baseurl, {
        method: 'GET',
        headers: {'Accept': 'application/json',
                'APIKey': 'mykey'}
        })
    const data = await res.json()

    if (res.ok) {
        return {
            status: 200,
            body: data
        }
    }

    return {
        status: res.status,
        error: new Error('This is an error')
    }
}

我希望能够做类似的事情:

const response = await self.fetch('api/address.json', {props:{searchInput}});

然后在端点上捕获它:

export async function get(request, props)
const baseurl = 'https://myaddressapi/?postcode='
const url = `${baseurl}${props}`

我尝试了看看文档,但我似乎无法正常工作。有指针吗?

I'm trying to pass a prop value from a text input field to a Sveltekit endpoint, but I can't figure out how I pass it through.

My onsubmit runs this function

const handleSubmit = () => {
        promise = getData();
    }

Which calls a function to get data from an external API via a sveltekit endpoint:

 async function getData() {
        const response = await self.fetch('api/address.json');
        const addressdata = await response.json()

        if (response.ok) {
        return addressdata;
            
        } 
}

The end point looks like this

export async function get({ }) {
    const baseurl = 'https://myaddressapi/?postcode=xxxxxx'
    const res = await fetch(baseurl, {
        method: 'GET',
        headers: {'Accept': 'application/json',
                'APIKey': 'mykey'}
        })
    const data = await res.json()

    if (res.ok) {
        return {
            status: 200,
            body: data
        }
    }

    return {
        status: res.status,
        error: new Error('This is an error')
    }
}

I want to be able to do something like:

const response = await self.fetch('api/address.json', {props:{searchInput}});

and then capture it on the endpoint:

export async function get(request, props)
const baseurl = 'https://myaddressapi/?postcode='
const url = `${baseurl}${props}`

I've tried to look at the documentation, but I can't seem to get it to work. Any pointers, please?

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

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

发布评论

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

评论(1

韵柒 2025-01-27 02:02:13

我重新阅读文档,我需要将邮政编码作为ID传递到现在正在工作的端点。

更多信息在这里 - https://kit.svelte.svelte.dev/docs/docs/routing#endpoints

I re-read the docs, and I needed to pass the postcode as an ID into the endpoint, which is now working.

More information here - https://kit.svelte.dev/docs/routing#endpoints

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