简体   繁体   English

create-react-app Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

[英]create-react-app Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Trying to read data from json dynamically using Highcharts in React.尝试使用 React 中的 Highcharts 从 json 动态读取数据。 Throwing in the towel and appealing to SO for help.认输并呼吁SO寻求帮助。

Keep getting:不断获得:

"Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0"

no matter where I put the file.不管我把文件放在哪里。 Ejecting from create-react-app and changing the Webpack config was no help.从 create-react-app 中退出并更改 Webpack 配置并没有帮助。

Network tab shows a 200 response.网络选项卡显示 200 响应。

JSON is valid. JSON 有效。 Have tried to read json from both src and public folder.尝试从srcpublic文件夹中读取 json 。 Have tried using fetch with headers and without.曾尝试使用fetch与标头和不使用。 (Using with headers changes 200 to 404). (使用标题将 200 更改为 404)。

headers: {
  'Accept': 'application/json',
  'Content-Type': 'application/json'
  }
}

console.log(response.headers.get('Content-Type'))

returns text/html; charset=UTF-8返回text/html; charset=UTF-8 text/html; charset=UTF-8 . text/html; charset=UTF-8

Have tried the solutions here and here .这里这里尝试了解决方案。

Here's the component:这是组件:

Chart.js图表.js

import React, { Component } from 'react';
import HighchartsReact from 'highcharts-react-official';
import Highcharts from 'highcharts';

const endpoint = '../public/dummy.json'

export default class Chart extends Component {
  constructor(props) {
    super(props);

    this.state = {
      // To avoid unnecessary update keep all options in the state.
      chartOptions: {
        chart: {
          type: 'bar'
        },
        xAxis: {
          title: {
            text: 'X Axis'
          }
        },
        yAxis: {
          title: {
            text: 'Y Axis'
           },
          min: 0,
          max: 100
        },
        series: [
          { data: [6] }
        ],
        plotOptions: {
          series: {
            point: {
              events: {
                mouseOver: this.setHoverData.bind(this)
              }
            },
            name: 'Chart name'
          }
        }
      },
      hoverData: null
    };
  }

  setHoverData = (e) => {
    console.log(e)
    // The chart is not updated because `chartOptions` has not changed.
    this.setState({ hoverData: e.target.options.x })
  }

  updateSeries = () => {
    // The chart is updated only with new options.
    this.setState({
      chartOptions: {
        series: [
          { data: [Math.random() * 100]}
        ]
      }
    });
  }


    componentDidMount() {
      // fetch(endpoint, {
      //   headers: {
      //     'Accept': 'application/json',
      //     'Content-Type': 'application/json'
      //   }
      // })
      fetch(endpoint)         
      .then((response) => {
        response.json()
        console.log(response.headers.get('Content-Type'))
      })
      .then(data => {
        this.state.chartOptions.series[0].data = data;
        this.setState({ data: data });
      })
      // .catch(error => {
      //   console.log('Error! ', error)
      // })
    }


  render() {
    const { chartOptions, hoverData } = this.state;

    console.log('this.state: ', this.state)

    return (
      <div>
        <HighchartsReact
          highcharts={Highcharts}
          options={chartOptions}
        />
      <h3>Hovering over {hoverData}</h3>
      <button onClick={this.updateSeries.bind(this)}>Update Series</button>
      </div>
    )
  }
}

dummy.json dummy.json

[
    {
        "first_name": "John",
        "amount": 10
    }, 
    {
        "fist_name": "Tim",
        "amount": 8 
    }
]

index.js索引.js

import ReactDOM from 'react-dom';
import Chart from './Chart'

ReactDOM.render(<Chart />, document.getElementById('root'));

Short Answer简答

Change ../public/dummy.json to just ./dummy.json since you are relative to the the root level of the public folder as opposed to being inside a sibling folder to public like the src/ folder.将 ../public/dummy.json 更改为./dummy.json,因为您相对于公共文件夹的根级别,而不是像 src/ 文件夹一样位于同级文件夹中。

Long Explanation长说明

Make the path only ./dummy.json since only what is inside the public folder is getting served up.使路径仅为 ./dummy.json,因为只有公共文件夹中的内容才会被提供。 Your react components are bundled into the js files linked to the index.html in your public folder.你的反应组件被捆绑到链接到你公共文件夹中的 index.html 的 js 文件中。 Therefore, your fetch requests are bundles/"compiled" into JS files in your public folder.因此,您的提取请求被捆绑/“编译”到公共文件夹中的 JS 文件中。 Because of this, your relative file paths should be made with the assumption that you are within the public folder.因此,您的相对文件路径应该假设您位于公用文件夹中。

暂无
暂无

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

相关问题 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 (React/Redux App) - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App) 使用 Flask &quot;Uncaught SyntaxError: Unexpected token &lt;&quot; 为 create-react-app 提供服务 - Serving a create-react-app with Flask "Uncaught SyntaxError: Unexpected token <" React Js: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获(承诺)SyntaxError:JSON 中位置 0 的意外标记 &lt; - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 - Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0 - Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0 Django : Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - Django : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获的 Promise Rejection SyntaxError: Unexpected token u in JSON at position 0 - Uncaught Promise Rejection SyntaxError: Unexpected token u in JSON at position 0 如何修复“Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0”错误 - How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM