首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从Astro文件(服务器端)访问url查询参数?

如何从Astro文件(服务器端)访问url查询参数?
EN

Stack Overflow用户
提问于 2022-08-17 04:49:15
回答 1查看 591关注 0票数 2

我无法访问astro文件的查询参数。

给定url http://localhost:3000/example?hello=meow

如何从{hello: "meow"}文件中访问example.astro

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-18 15:10:14

如果要编写API路由,则需要使用.ts.js文件[来源]

在这种情况下,您可以编写如下所示的get方法:

代码语言:javascript
运行
复制
export async function get({request}) {
  // process the URL into a more usable format
  const url = new URL(request.url)
  const params = new URLSearchParams(url.search)

  // set up a response object
  const data = {
    hello: params.get('hello'),
  };

  // this will yield { hello: 'meow' } on your Astro server console
  console.log(data)
  
  // return the response
  return new Response(JSON.stringify(data), {
    status: 200
  }); }

我认为,从一个.astro文件中,您可以通过const { url } = Astro.request;来访问url。对于静态页面,可以通过const url = Astro.url; [来源]完成。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73382984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档