gpt4 book ai didi

javascript - Next.js:FetchError: invalid json response body Unexpected token < in JSON at position 0

转载 作者:行者123 更新时间:2023-12-04 12:34:09 26 4
gpt4 key购买 nike

我正在尝试使用 getStaticProps简单地发出一个请求,然后将该数据从它传递给一个组件:
但我收到了这个错误:

FetchError: invalid json response body athttps://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SRreason: Unexpected token < in JSON at position 0

import AppliancePackage from '../components/AppliancePackage.jsx';

function HomePage({ data }) {
return (
<>
<AppliancePackage appliances={data} />
</>
);
}

export default HomePage;

// This function gets called at build time on server-side.
// It won't be called on client-side, so you can even do
// direct database queries. See the "Technical details" section.

export async function getStaticProps() {
// Call an external API endpoint to get data.
// You can use any data fetching library

var res = await fetch(
'https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR'
);

var json = await res.json();

data = JSON.stringify(json);
console.log('data ', data);

return {
props: {
data: json,
},
};
}
我试图对其进行字符串化,但这没有用!我也对评论感到困惑:

This function gets called at build time on server-side.It won't be called on client-side, so you can even dodirect database queries. See the "Technical details" section.


然后如您所见,有一条评论指出:

Call an external API endpoint to get posts.


但是在他们的 docs 中有一个关于 API 路由的完整部分。
任何人都可以帮助我这是怎么回事?
更新
Alexey 提供了一些很棒的见解,但就像我对他说的那样,我在 axios 文档中找不到更改用户代理!

最佳答案

阿列克谢让我走上了正轨! friend ,谢谢!
结束了你必须这样做:

export async function getStaticProps() {
// Call an external API endpoint to get posts.
// You can use any data fetching library

var res = await axios.get(
'https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR',
{
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': '*',
},
}
);
var res = JSON.stringify(res.data);
console.log('res ', res);

// By returning { props: posts }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
data: res,
},
};
}
这是添加标题:
  headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': '*',
},
*对于任何 User-Agent

关于javascript - Next.js:FetchError: invalid json response body Unexpected token < in JSON at position 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62960214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com