所以我试图使用 axios 来提取一个 api 以及 HTML5Geolocation。
我将它放在 componentDidMount 中,并希望它至少可以在 console.log 使用异步记录结果并最终将状态设置为国家/地区。
componentDidMount(){
async onLoad() {
if (window.navigator.geolocation) {
window.navigator.geolocation.getCurrentPosition(
position => {
const response = axios.get('http://ws.geonames.org/countryCode' , {
lat: position.coords.latitude,
lng: position.coords.longitude,
type: 'JSON'
});
console.log('here is the response >>>>', response.countryName);
this.setState({ country: response.countryName })
});
}
}
}
但是,这目前不起作用。我正在为我的 API使用Geonames
知道我在代码上做错了什么吗?