【问题标题】:How to do a HTTP head request in next.js如何在 next.js 中执行 HTTP 头请求
【发布时间】:2022-01-26 19:18:18
【问题描述】:

我想在 next.js 中实现相同的操作 here

【问题讨论】:

    标签: reactjs axios next.js


    【解决方案1】:

    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 fetchingaxios documentation

    祝你有美好的一天,我希望你的项目取得成功

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 2021-04-27
      • 2011-07-29
      相关资源
      最近更新 更多