最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fetch api data with create-react-app - Stack Overflow

programmeradmin1浏览0评论

I'm new to reactjs, and I'm using create-react-app to get started, but I can't understand how make an api call to fetch data. Here is my code:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
// import { URL, KEY, city, countryCode } from './config.json';


const KEY = "d7ba7d7818dd3cec9ace78f9ad55722e";
const URL = "api.openweathermap/data/2.5";
const CITY = "Paris";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {}
    };
  }

  ponentDidMount() {
    var url = `${URL}/weather?q=${CITY}&APPID=${KEY}&units=metric`;
    console.log(url);
    fetch(url).then(
      response => response.json()
    ).then(
      json => {
        console.log(json);
        this.setState({data: json});
      }
    );
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Wele to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

The render method is just the default render from create-react-app, I didn't change it. I only added the constructor and ponentDidMount methods. I try to fetch some data from the OpenWeatherMap API, add it to the state and log it to the console.

The request works perfectly in postman, but raises this Error in my app: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Can anyone help me ?

I'm new to reactjs, and I'm using create-react-app to get started, but I can't understand how make an api call to fetch data. Here is my code:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
// import { URL, KEY, city, countryCode } from './config.json';


const KEY = "d7ba7d7818dd3cec9ace78f9ad55722e";
const URL = "api.openweathermap/data/2.5";
const CITY = "Paris";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {}
    };
  }

  ponentDidMount() {
    var url = `${URL}/weather?q=${CITY}&APPID=${KEY}&units=metric`;
    console.log(url);
    fetch(url).then(
      response => response.json()
    ).then(
      json => {
        console.log(json);
        this.setState({data: json});
      }
    );
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Wele to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

The render method is just the default render from create-react-app, I didn't change it. I only added the constructor and ponentDidMount methods. I try to fetch some data from the OpenWeatherMap API, add it to the state and log it to the console.

The request works perfectly in postman, but raises this Error in my app: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Can anyone help me ?

Share Improve this question edited Mar 16, 2022 at 14:20 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Jun 28, 2018 at 10:35 ZumaZuma 8461 gold badge7 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Include protocol with URL to resolve the problem. Response of fetch request is not success so when you try to parse response as JSON it throws exception because response it not valid JSON.

const KEY = "d7ba7d7818dd3cec9ace78f9ad55722e";
// Made change here
const URL = "https://api.openweathermap/data/2.5";
const CITY = "Paris";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {}
    };
  }

  ponentDidMount() {
    var url = `${URL}/weather?q=${CITY}&APPID=${KEY}&units=metric`;
    console.log(url);
    fetch(url).then(
      response => response.json()
    ).then(
      json => {
        console.log(json);
        this.setState({data: json});
      }
    ).catch(error => console.log(error));
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <h1 className="App-title">Wele to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('example'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="example"></div>

发布评论

评论列表(0)

  1. 暂无评论