简体   繁体   English

我如何从 api 端点获取数据

[英]How do i fetch data from api endpoint

I'm running nextjs for my front end (port 3000), trying to fetch api endpoints from strapi (port 1337).我正在为我的前端(端口 3000)运行 nextjs,试图从 Strapi(端口 1337)获取 api 端点。 How do I go about using my frontend application in order to fetch data from my backend.我如何使用我的前端应用程序来从我的后端获取数据。 Is there a way that both ports can run simultaneously, or am I doing something wrong?有没有办法让两个端口同时运行,或者我做错了什么?

If I run on port 3000 I receive a GET request error ERR_CONNETION_REFUSED, obviously - because the data on port 1337 isn't available because port 1337 isn't running.如果我在端口 3000 上运行,我会收到一个 GET 请求错误 ERR_CONNETION_REFUSED,显然 - 因为端口 1337 上的数据不可用,因为端口 1337 没有运行。

My file structure for the project is the following:我的项目文件结构如下:

my_app我的应用程序

--backend folder(strapi files and runs on on port 1337) --backend 文件夹(strapi 文件并在端口 1337 上运行)

--frontend folder(contains all nextjs and runs on port 3000) --frontend 文件夹(包含所有 nextjs 并在端口 3000 上运行)

Code :代码

import Header from '../components/Header'
import useFetch from '../public/hooks/useFetch'

export default function Home() {

  const { loading, error, data } = useFetch('http://localhost:1337/reviews')

  return (
    
    <>

    <Header/>

    HOME


    </>
  )
}

To make your life easier, I do recommend you to install "Documentation" on the marketplace.为了让您的生活更轻松,我建议您在市场上安装“文档”。 After that, you will have access to "Documentation in plugins", there you will be able to check your API, and use "Retrieve your JWT token" to obtain a test token to test all your API.之后,您将可以访问“插件中的文档”,在那里您将能够检查您的 API,并使用“检索您的 JWT 令牌”来获取测试令牌以测试您的所有 API。

From there you will have access to Swagger App, and you will be able to check all the API functionality.从那里您将可以访问 Swagger 应用程序,并且您将能够检查所有 API 功能。

在此处输入图片说明

Now, you can access all your API from the server URL ie: GET http://localhost:1337/helpers现在,您可以从服务器 URL 访问所有 API,即: GET http://localhost:1337/helpers

Note: Be sure to make your APIs accessible to your roles on Settings/Roles on the Admin panel.注意:请务必让您的 API 可供您在管理面板上的设置/角色上访问。 From there, you can also check the URI you will be using.从那里,您还可以检查将使用的 URI。

In your case, to access http://localhost:1337/reviews be sure to make reviews accessible on the public or private side respectably.在您的情况下,要访问http://localhost:1337/reviews确保在公共或私人方面可访问评论。

在此处输入图片说明

After that, you will access your strapi endpoints from the strapi URL as //localhost:1337/your_endpoint之后,您将从trapi URL访问您的trapi端点//localhost:1337/your_endpoint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次单击按钮时如何从端点获取数据? 在反应中 - How do I fetch data from an endpoint everytime I click a button? In React Javascript:如何从API提取特定数据? - Javascript: How do I fetch specific data from an API? 如何从HTML CSS和JS中的API获取数据 - How do I fetch data from an API in HTML CSS and JS 如何从 React Native 项目中从 WordPress API 端点获取的数据中删除 HTML 字符? - How do I remove HTML characters from the data fetched from a WordPress API endpoint in a React Native project? 如何在每个请求仅支持一个 ID 的 API 端点中获取多个 ID? - How do I fetch more than one ID in an API endpoint that only supports one ID per request? 如何在React组件中从Express端点获取数据? - How do I get data from Express endpoint in a React component? 如何使用公司自己的API从Angular JS应用程序中获取数据? - How do I fetch data from a Angular JS application using a company's own API? 如何在调用渲染方法之前从 API 获取数据? (反应,护照,快递) - How do I fetch data from API before render method called? (react, passport, express) 如何使用香草 JS 从表单提交的公共 API 获取数据 - How do I fetch data from a public API on form submit using vanilla JS 在对 API 进行 fetch 调用后,如何将数据存储到 localStorage? - How do I store data into localStorage after a fetch call to an API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM