【发布时间】: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