JSON api适用于本地,但不适用于公共部署

时间:2018-06-10 15:05:52

标签: json reactjs

我有一个使用JSON的天气API,可以在我的本地机器上运行,但是当我尝试将它部署到GithubPages时,它会归零并且API无法提取。它是用js写的reactjs页面。

Here is the working site that is on localhost:3000

Not working website hosted on GitHub pages

Deployed Website

state = {
    current: 'none'
}

fetchWeather = () => {
return fetch(`https://api.aerisapi.com/observations/75248client_id=${accessId}&client_secret=${APIkey}`)
.then((res) => res.json())
}

componentDidMount = () => {
this.fetchWeather()
.then((json) => {
  json.success ? this.setState({ current: json.response.ob }) : null
})
}

formatCurrentWeather = (currentWeather) => {
let current = ''
if (currentWeather.toLowerCase().charAt(currentWeather.length-1) === 'y') {
  current = currentWeather.toLowerCase() + ' '
} else if (currentWeather.toLowerCase() === 'clear') {
  current = 'cloudless '
} else if (currentWeather.toLowerCase() === 'freezing fog') {
  current = 'freezing '
} else if (currentWeather.toLowerCase().charAt(currentWeather.length-1) === 'g') {
  current = currentWeather.toLowerCase() + 'gy '
} else {
  current = currentWeather.toLowerCase() + 'y '
}
return current
}

importAll = (r) => {
let icons = {};
r.keys().map((item, index) => { icons[item.replace('./', '')] = r(item); });
return icons;
}

render() {
console.log(this.state.current)
const icons = this.importAll(require.context('../images/icons', false, /\.(png|jpe?g|svg)$/));
let currentWeather = ''
this.state.current === 'none' ? null : currentWeather = this.formatCurrentWeather(this.state.current.weatherPrimary)

0 个答案:

没有答案