javascript - 错误 : Only absolute URLs are supported in nextjs

标签 javascript node.js reactjs fetch next.js

你能帮我解决一个错误吗nextjs 只支持绝对 URL。我正在尝试从服务器获取数据

export async function getStaticProps({
  params,
  preview = false,
  previewData = {}
}) {
  console.log("-----");
  const res = await fetch("/api/basecss/");
  const stylesheet = await res.text(); // Converts response data to text
  return {
    revalidate: 200,
    props: {
      stylesheet
    }
  };
}

这是我的代码 https://codesandbox.io/s/naughty-platform-1xket?file=/pages/index.js:332-639

我收到这个错误 TypeError:仅支持绝对 URL

最佳答案

您在第 23 行传递给 fetch 的字符串是一个相对 URL(即它缺少协议(protocol)和域名,可能是 http://localhost:3000/api/basecss 或类似)

您需要将 API 端点引用为绝对 URL,包括该信息。这篇文章提供了一些关于如何做到这一点的信息

Next.js - Error: only absolute urls are supported

关于javascript - 错误 : Only absolute URLs are supported in nextjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65694384/

相关文章:

javascript - 在静态方法之间共享代码

JavaScript/Dynamics CRM : Need help finishing JavaScript event that counts how many records in a grid's "Id & Qualify" field ="Yes"

node.js - 如何停止 sails.js 中 id 的自动增量

node.js - Socket.IO 套接字保护,无需身份验证

javascript - 类型错误 : Cannot read property 'params' of undefined for updating categories

javascript - 尝试在浏览器中使用 React v0.14.3

javascript - 使用 plotly.js 时如何定义 x 轴悬停格式

javascript - 从文档就绪外部调用时未定义函数

javascript - 表达静态nodejs

ruby-on-rails - Rails 不生成真实性 token