我正在尝试创建一个Wordpress Gutenberg块,它将像这样获取外部API:
apiFetch( { path: '//gorest.co.in/public/v1/users' } )
.then( (response) => {
setState({users: response});
} );和endup with error:
{
code: "rest_no_route"
data: {status: 404}
message: "No route was found matching the URL and request method."
}当我尝试调用内部WP API时:
apiFetch( { path: '/wp/v2/users' } )
.then( (response) => {
setState({users: response});
} );有没有办法让Wordpress Gutenberg代码块调用外部API?
发布于 2021-08-31 21:46:31
您应该使用url而不是path。path是API端点的简写版本。调用终结点时,会将path附加到根URL。这就是/wp/v2/users为你工作的原因。因为它为您提供了到端点的完整路径。
使用url将使用完整的URL,并允许连接到外部API。
apiFetch( { url: 'https://gorest.co.in/public/v1/users' } )
.then( (response) => {
setState({users: response});
} );https://stackoverflow.com/questions/69000599
复制相似问题