im试图使用node.js对lat和long的datasciencetool工具包进行api调用,方法是来自react.js。每当用户输入地址时,我都应该从api调用中获取数据,但是即时通讯在控制台中只会出现404错误。如果我用类似http://www.datasciencetoolkit.org/coordinates2statistics/37.769456%2c-122.429128?statistics=population_density这样的静态值替换url,那么它将起作用。
data.js
const fetch = require('node-fetch');
module.exports = (app) => {
app.post('/search-data', (req, res) => {
console.log(req);
let lat = req.body.param.lat;//before: req.query.lat it's wrong
let long = req.body.param.long;
console.log(lat);
const apiUrl = 'http://www.datasciencetoolkit.org/coordinates2statistics' + lat + '%2c' + long+'?statistics=population_density';
fetch(apiUrl).then(res=>res.json()).then(data=>{
console.log(data)
res.send({
data
});
})
.catch(err => {
res.redirect('/error');
});
})
}
home.js //反应
handleSelect = address => {
this.setState({
address,
});
console.log(this.state.address);
geocodeByAddress(address)
.then(res => getLatLng(res[0]))
.then(({ lat, lng }) => {
this.setState({
latitude: lat,
longitude: lng,
isGeocoding: false,
});
this.setState({ isLoaded: true });
})
.catch(error => {
this.setState({ isGeocoding: false });
console.log('error', error); // eslint-disable-line no-console
});
console.log(this.state.latitude);
console.log(this.state.longitude);
var param = {
lat: this.state.latitude,
long: this.state.longitude,
temp: 1,
};
axios
.post(`http://localhost:5000/search-data`, {
param,
})
.then(data => {
console.log(data);
});
};
经度和纬度会返回正确的值,因此无法确定发生了什么。任何帮助是极大的赞赏!
答案 0 :(得分:0)
您忘记了ordinates2statistics /
中的 / const apiUrl = 'http://www.datasciencetoolkit.org/coordinates2statistics/' + lat + '%2c' + long+'?statistics=population_density