简体   繁体   English

Next.js:FetchError: invalid json 响应正文 意外的令牌 < 在 JSON 在 Z4757FE07FD492A8BE0EA673A

[英]Next.js:FetchError: invalid json response body Unexpected token < in JSON at position 0

I am trying to use getStaticProps to simply make a request and then pass that data from it to a component:我正在尝试使用getStaticProps来简单地发出请求,然后将该数据从它传递给组件:

But I'm getting this error:但我收到了这个错误:

FetchError: invalid json response body at https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR reason: Unexpected token < in JSON at position 0 FetchError: invalid json response body at https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR reason: 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,
    },
  };
}

I tried to Stringify it, but that didn't work: Also I am kind of confused by the comments:我试图对其进行字符串化,但这没有用:我也对评论感到困惑:

This function gets called at build time on server-side.这个 function 在构建时在服务器端被调用。 It won't be called on client-side, so you can even do direct database queries.它不会在客户端调用,因此您甚至可以直接进行数据库查询。 See the "Technical details" section.请参阅“技术细节”部分。

And then as you can see there is a comment that states:然后如您所见,有一条评论指出:

Call an external API endpoint to get posts.调用外部 API 端点以获取帖子。

But have a whole section regarding API routes in their docs但是在他们的文档中有一个关于 API 路由的完整部分

Anyone can help me what is the matter?任何人都可以帮助我这是怎么回事?

Update更新

Alexey contributed some great insight, but like I said to him I can't find in the axios docs to change the user-agent! Alexey 提供了一些深刻的见解,但就像我对他说的那样,我在 axios 文档中找不到更改用户代理!

I think the endpoint you're referring to is for some reason sensitive to "User-Agent".我认为您所指的端点出于某种原因对“用户代理”敏感。

When I tried fetching it with CURL like this, it returned some HTML response (which is not valid JSON ofcourse)当我尝试像这样使用 CURL 获取它时,它返回了一些 HTML 响应(当然这是无效的 JSON)

curl https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR

But this way it did work and returned JSON, just like if reached via browser:但这样它确实工作并返回 JSON,就像通过浏览器访问一样:

curl -H "User-Agent: some browser" https://www.ajmadison.com/product3.0/packages.index.json.php?sku=RF28R7351SR  

TLDR: try adding "user-agent" header to your request. TLDR:尝试将“用户代理”header 添加到您的请求中。

Alexey got me on the right track!阿列克谢让我走上了正轨! Thanks my friend!朋友,谢谢!

Wound up you have to do this:结束了你必须这样做:

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,
    },
  };
}

this being adding the headers:这是添加标题:

  headers: {
    Accept: 'application/json, text/plain, */*',
    'User-Agent': '*',
  },

And * for any User-Agent*对于任何User-Agent

Instead of using res.status(200).json(data) in the api file , use res.status(200).json(JSON.stringify(data)) .不要在api file中使用 res.status(200).json(data res.status(200).json(data) ,而是使用res.status(200).json(JSON.stringify(data)) This will eliminate JSON error in the console.这将消除控制台中的 JSON 错误。 This worked for me.这对我有用。

For me it was .json() called for HTTP errors returning HTML in response body对我来说,.json .json()调用了 HTTP 错误,在响应正文中返回 HTML

i think the data is not been fetch from the end that why.我认为数据不是从最后获取的,这是为什么。 i face same problems i restarted my system and network and it works perfectly back我遇到了同样的问题我重新启动了我的系统和网络,它可以完美地恢复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node FetchError:无效的JSON响应正文-JSON中的意外令牌&lt; - Node FetchError: invalid json response body - unexpected token < in JSON UnhandledPromiseRejectionWarning: FetchError: invalid json 响应正文 - UnhandledPromiseRejectionWarning: FetchError: invalid json response body at 节点获取帮助 - 获取 FetchError: invalid json response &gt; body at<url> 原因: &gt; JSON 输入意外结束</url> - node-fetch help - getting FetchError: invalid json response > body at <url> reason: > Unexpected end of JSON input Express.js FetchError: https://api.twitter.Z4D236D9A250D102C5ZFEEC6AD1 处的无效 json 响应正文 - Express.js FetchError: invalid json response body at https://api.twitter.com Next.js 中的 JSON 输入意外结束 - Unexpected end of JSON input in Next.js 下一个 js 在 position 0 的 JSON 中运行 dev 意外令牌 u - next js run dev Unexpected token u in JSON at position 0 Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json JSON 中的意外标记 u 在位置 0 JS 中的错误 - Unexpected token u in JSON at position 0 Error in JS 反应 JS:意外的令牌 &lt; 在 JSON 在 position 0 - React JS: Unexpected token < in JSON at position 0 “SyntaxError: Unexpected token &lt; in JSON at position 0”在反应 js - "SyntaxError: Unexpected token < in JSON at position 0" in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM