【问题标题】:Fetch in React.js not working在 React.js 中获取不起作用
【发布时间】:2017-12-14 07:19:56
【问题描述】:

我正在尝试在 react.js 中执行 fetch 请求,但我似乎没有获取任何内容。我觉得这是一个愚蠢的错误,我没有注意到它。任何帮助将不胜感激。

import React, {Component} from 'react';
import './App.css';
import TimeSearch from './TimeSearch.jsx';
import CurrentTime from './CurrentTime.jsx';

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

  handleFormSubmit(item) {
    var url = 'https://api.aladhan.com/timingsByCity?city=' + item + '&country=US&method=2';

    fetch(url)
      .then(response => {
        return response.json();
      })
      .then(json => {
        this.setState({
          Fajir: json.Fajir,
          Sunrise: json.Sunrise,
          Dhuhr: json.Dhuhr
        });
      });
  }

  render() {
    return (
      <div className="App">
        <h1>Welcome to Prayer Times!</h1>
        <h6>Search for times in any city in the United States</h6>
        <TimeSearch
          handleFormSubmit={item => {
            this.handleFormSubmit(item);
          }}
        />
        <CurrentTime Fajir={this.state.Fajir} />
      </div>
    );
  }
}

export default App;

// Time Search Component

import React, {Component} from 'react';

class TimeSearch extends Component {
  render() {
    return (
      <ul>
        <form onSubmit={e => this.handleFormSubmit(e)}>
          <input type="text" id="lookUp" placeholder="Search by City" ref="seachBox" />
          <button type="submit" id="searchButtons">
            {' '}
            Search{' '}
          </button>
        </form>{' '}
      </ul>
    );
  }
  handleFormSubmit(e) {
    e.preventDefault();
    var item = this.refs.searchBox.value;
    this.props.handleFormSubmit(item);
  }
}

export default TimeSearch;

// Current Time Component

import React, {Component} from 'react';

class CurrentTime extends Component {
  render() {
    return (
      <div id="time">
        <h1 id="fajir"> {this.props.Fajir} </h1>
      </div>
    );
  }
}

export default CurrentTime;

更新 https://i.stack.imgur.com/YJx9T.png

当我登录时什么都没有显示,这让我感到困惑,我尝试使用 Postman Windows 应用程序发出 API 请求,它工作 100% 正常,所以我不知道现在发生了什么

【问题讨论】:

    标签: javascript html css reactjs web-deployment


    【解决方案1】:

    我认为您的 fetch 代码看起来不错,控制台中是否抛出任何错误?可能是您的处理程序未绑定到正确的上下文。如果没有正确绑定函数,this.setState 将导致错误,因为this 不是正确的上下文。你可能想在构造函数中绑定它,或者更好的是,使用这样的初始化器:

    handleFormSubmit = (item) => {
      const url = `https://api.aladhan.com/timingsByCity?city=${item}&country=US&method=2`;
    
      fetch(url)
        .then((response) => {
         return response.json();
        })
        .then((json) => {
          this.setState({
            Fajir: json.Fajir,
            Sunrise: json.Sunrise,
            Dhuhr: json.Dhuhr
          });
        });
    }
    

    要做的一件事是检查浏览器的网络开发人员选项卡并确保请求正确通过。

    更新

    看起来它现在可以工作了,请参阅下面的获取代码:

    const item = 'Chicago';
        var url = 'https://api.aladhan.com/timingsByCity?city=' + item + '&country=US&method=2';
    
    fetch(url)
      .then((response) => {
        return response.json();
      })
      .then((json) => {
        console.log(json);
      });

    【讨论】:

    • 嘿,所以我确实检查了网络选项卡,似乎提取请求不在其中,它没有显示任何内容。这意味着没有获取任何内容
    • @ZubairAmjad 似乎现在可以工作,我认为https 可能可以跨域工作,但http 没有
    • 我正在尝试记录它,但由于某种原因它不允许我登录我的浏览器,并且浏览器中没有记录任何内容
    • 当我创建一个反应应用程序时,它在某处搞砸了,它确实有效!
    【解决方案2】:

    嘿试试这个小提琴它正在工作只需打开控制台它正在注销响应。 Fiddle Link

    HTML

    <div id="container"></div>
    

    JS

    class Hello extends React.Component {
    handleFormSubmit = (item) => {
      const url = 'https://api.aladhan.com/timingsByCity?city='+item+'&country=US&method=2';
      fetch(url)
        .then((response) => {
            console.log(response);
         return response.json();
        })
        .then((json) => {
          this.setState({
            Fajir: json.Fajir,
            Sunrise: json.Sunrise,
            Dhuhr: json.Dhuhr
          });
        });
    }
      render() {
        return <button onClick={() => this.handleFormSubmit(2)}>Click Me</button>;
      }
    }
    
    ReactDOM.render(
      <Hello name="World" />,
      document.getElementById('container')
    );
    

    【讨论】:

      【解决方案3】:

      您的 fetch 语句看起来不错。

      在黑暗中拍摄:

      在表单上附加事件处理程序时,请确保绑定上下文:

      <form onSubmit={this.handleFormSubmit.bind(this)}>
      

      或者在构造函数中

      this.handleFormSubmit.bind(this)
      

      请向我们展示更多组件...

      【讨论】:

      • 嘿菲利普,我更新了我的帖子!它会有组件!
      • 看起来没问题,从开发工具中提取确实有效。我注意到您需要以不同的方式处理 json:json.data.timings 包含您想要的属性。
      • 嘿 Phillipp,所以应该是 json.data.timings.Fajir 吧?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 2019-12-22
      • 2022-10-04
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多