将 prop 值作为查询字符串传递到 sveltekit 端点
我正在尝试将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我重新阅读文档,我需要将邮政编码作为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