Next13提取到本地主机:8000不起作用

mnemlml8  于 2023-01-30  发布在  其他
关注(0)|答案(1)|浏览(131)

不知何故,从next13获取的内容对localhost:8001上的Django不起作用。
出现以下错误:

TypeError: fetch failed
    at Object.processResponse (node:internal/deps/undici/undici:7188:34)
    at node:internal/deps/undici/undici:7516:42
    at node:internal/process/task_queues:140:7
    at AsyncResource.runInAsyncScope (node:async_hooks:202:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: [object Object]
      at makeNetworkError (node:internal/deps/undici/undici:6317:51)
      at httpNetworkFetch (node:internal/deps/undici/undici:7810:16)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async httpNetworkOrCacheFetch (node:internal/deps/undici/undici:7703:33)
      at async httpFetch (node:internal/deps/undici/undici:7557:37)
      at async schemeFetch (node:internal/deps/undici/undici:7489:18)
      at async node:internal/deps/undici/undici:7342:20
      at async mainFetch (node:internal/deps/undici/undici:7338:20) {
    [cause]: undefined
  }
}
TypeError: fetch failed
    at Object.processResponse (node:internal/deps/undici/undici:7188:34)
    at node:internal/deps/undici/undici:7516:42
    at node:internal/process/task_queues:140:7
    at AsyncResource.runInAsyncScope (node:async_hooks:202:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  digest: '1117663215'

使用此代码:

import './globals.css'

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  // const data = await fetch(`https://dummyjson.com/products/1`, { cache: 'force-cache' });
  const data = await fetch(`http://localhost:8001/board/`, {
    method: 'GET', headers: {
      'Accept': 'application/json',
      "Content-Type": "application/json"
    },
    redirect: 'follow',
    cache: 'force-cache'
  });
  console.log(data)
  return (
    <html lang="en">
      <head />
      <body>
        <h1></h1>

        <div>{children}</div>
      </body>
    </html>
  )
}

从任何其他公共api中获取都可以。CORS被启用,并且在Django www.example.com中设置了CORS_ORIGIN_ALLOW_ALL = Truesettings.py
你知道我该怎么补救吗?
谢谢

anauzrmj

anauzrmj1#

好吧我自己解决的
对于谁是运行到相同的错误这里是我的解决方案:您需要将localhost替换为127.0.0.1不知何故,nextjs不喜欢从localhost获取数据...
请在下面找到正确的代码:

import './globals.css'

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  // const data = await fetch(`https://dummyjson.com/products/1`, { cache: 'force-cache' });
  const data = await fetch(`http://127.0.0.1:8001/board/`, {
    method: 'GET', headers: {
      'Accept': 'application/json',
      "Content-Type": "application/json"
    },
    redirect: 'follow',
    cache: 'force-cache'
  });
  console.log(data)
  return (
    <html lang="en">
      <head />
      <body>
        <h1></h1>

        <div>{children}</div>
      </body>
    </html>
  )
}

相关问题