【发布时间】:2022-01-26 19:18:18
【问题描述】:
我想在 next.js 中实现相同的操作 here。
【问题讨论】:
我想在 next.js 中实现相同的操作 here。
【问题讨论】:
Next.js 提供了从服务器获取数据并将其用作道具的方法。我不知道您是否想在呈现页面之前使用它(例如 getServerSideProps),或者例如在单击按钮时使用它,但我想这是第一种情况。
我在做请求时个人偏好是axios,所以我将在这个例子中使用它:
export async function getServerSideProps({ req, res }) {
// Here is where we make the request
const result = await axios({
method: 'HEAD', // here is where we declare that we want to use the HEAD method
url: "your server url", // this is the url where we want to send the request
headers: {} // if you want to add custom headers, you can do it here
})
// Here we are logging the result of the request
console.log(result)
}
您可以参考next.js documentation on data fetching和axios documentation
祝你有美好的一天,我希望你的项目取得成功
【讨论】: