[英]React : How to render API response data in fromat of data: Array(i) to react front end?
[英]How do I display response data in the front end?
我已经向github API发出了GET
请求:
axios.get('https://api.github.com/users/roadtocode822')
.then(function (response) {
console.log(response.data);
})
我得到响应数据。 该函数位于app.js
文件中。
以下代码也位于app.js
文件中:
app.get('/', function(req, res){
Article.find({}, function(err, articles){
if(err){
console.log(err);
} else {
res.render('index', {
title: "Articles",
articles: articles
});
}
});
});
我能够通过Article.js猫鼬模型从mongodb数据库中查询数据,并将数据发送到我的index.pug
文件。
我希望能够获取GITHUB响应数据并将其呈现在我的哈巴狗视图文件之一中。 我觉得我在Javascript中缺少某种概念,使我无法实现这一目标。
提前致谢。
要将Github响应作为JSON获取,只需使用JSON.parse()
。 但是,您将无法在前端使用.pug模板。 该模板在服务器端进行解释,并作为普通的旧HTML从服务器发送到客户端。 如果您对前端模板感兴趣,请查看诸如handlebars.js之类的东西。
axios.get('https://api.github.com/users/roadtocode822')
.then(function (response) {
console.log(response.data);
})
从上面的代码中, response.data
将是html内容,因为您的服务器返回了res.render
。
在前端,您应该使用a tag
和form post
而不是像这样的ajax call
<a href="https://api.github.com/users/roadtocode822">Click</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.