【问题标题】:create-react-app Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0create-react-app Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
【发布时间】:2020-04-18 08:40:46
【问题描述】:

尝试在 React 中使用 Highcharts 从 json 动态读取数据。认输并向 SO 寻求帮助。

不断获得:

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

无论我把文件放在哪里。从 create-react-app 中弹出并更改 Webpack 配置没有任何帮助。

Network 标签显示 200 响应。

JSON 有效。已尝试从 srcpublic 文件夹中读取 json。尝试使用带有标题和不带有标题的fetch。 (与标头一起使用将 200 更改为 404)。

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

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

返回text/html; charset=UTF-8

已尝试herehere 的解决方案。

这是组件:

Chart.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

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

index.js

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

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

【问题讨论】:

    标签: javascript json reactjs highcharts


    【解决方案1】:

    简答

    将 ../public/dummy.json 更改为 ./dummy.json 因为您是相对于公用文件夹的根级别,而不是在同级文件夹中以公开喜欢src/ 文件夹。

    详细说明

    仅将路径设为 ./dummy.json,因为只有公用文件夹中的内容才会被提供。您的反应组件被捆绑到链接到公共文件夹中 index.html 的 js 文件中。因此,您的提取请求被捆绑/“编译”到您的公共文件夹中的 JS 文件中。因此,您的相对文件路径应假设您位于公用文件夹中。

    【讨论】:

    • "将 ../public/dummy.json 更改为 ./dummy.json 因为您是相对于公用文件夹的根级别,而不是像src/文件夹。”工作。这应该是公认的答案。
    猜你喜欢
    • 2021-02-15
    • 2017-12-15
    • 2019-08-14
    • 2021-03-02
    • 1970-01-01
    • 2020-03-26
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多