首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用外部API的Wordpress gutenberg块

调用外部API的Wordpress gutenberg块
EN

Stack Overflow用户
提问于 2021-08-31 14:33:20
回答 1查看 259关注 0票数 1

我正在尝试创建一个Wordpress Gutenberg块,它将像这样获取外部API:

代码语言:javascript
复制
apiFetch( { path: '//gorest.co.in/public/v1/users' } )
  .then( (response) => {
    setState({users: response});
  } );

和endup with error:

代码语言:javascript
复制
{
  code: "rest_no_route"
  data: {status: 404}
  message: "No route was found matching the URL and request method."
}

当我尝试调用内部WP API时:

代码语言:javascript
复制
apiFetch( { path: '/wp/v2/users' } )
  .then( (response) => {
    setState({users: response});
  } );

有没有办法让Wordpress Gutenberg代码块调用外部API?

EN

回答 1

Stack Overflow用户

发布于 2021-08-31 21:46:31

您应该使用url而不是pathpath是API端点的简写版本。调用终结点时,会将path附加到根URL。这就是/wp/v2/users为你工作的原因。因为它为您提供了到端点的完整路径。

使用url将使用完整的URL,并允许连接到外部API。

代码语言:javascript
复制
apiFetch( { url: 'https://gorest.co.in/public/v1/users' } )
  .then( (response) => {
    setState({users: response});
  } );

Here is the documentation

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

https://stackoverflow.com/questions/69000599

复制
相关文章

相似问题

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