【发布时间】:2016-06-28 05:53:36
【问题描述】:
我们正在构建一个 react 应用程序(没有 redux;但使用 webpack),它使用 axios 向服务器发出 GET 请求,服务器接收请求(即我们看到我们的 console.log 语句)但对请求永远不会被服务,因为除了我们的 console.log 之外我们没有得到任何数据。服务器似乎对我们的 response.end/response.send 语句没有做任何事情。
以前有没有人处理过这个问题?有人有任何提示吗?请在下面查看我们的代码。
//Within our react component file
componentWillMount (){
console.log("inside of componentWillMount!");
return axios.get('/api/test')
.then(function(resp){
return resp.data;
console.log('axios response: ', resp);
})
.catch(function(resp) {
console.log('axios catch response ', resp);
});
}
//From our server.js file
// additional middleware to set headers that we need on each request
app.use(function(req, res, next) {
// disable caching so we'll always get the latest activities
res.setHeader('Cache-Control', 'no-cache');
next();
});
app.get('/api/test', function(request, response, err) {
//mongoose find all here
console.log("We're in the server!!!");
response.end("ennnndddd");
if(err){
console.log("ERROR!", err);
}
});
app.listen(port, function () {
console.log('Proxy listening on port 3000!');
});
【问题讨论】:
标签: api http express reactjs get