Javascript 如何请求API并将其发送到前端
我是node.js新手,我想练习一下。我找到了这个 我使主页很好,但当我点击任何国家时,我不能显示其信息,我的意思是我可以请求我点击的国家的数据,但我不能将其发送到我呈现的页面以显示这些信息 我希望它看起来像这样 这是节点Javascript 如何请求API并将其发送到前端,javascript,node.js,rest,express,routes,Javascript,Node.js,Rest,Express,Routes,我是node.js新手,我想练习一下。我找到了这个 我使主页很好,但当我点击任何国家时,我不能显示其信息,我的意思是我可以请求我点击的国家的数据,但我不能将其发送到我呈现的页面以显示这些信息 我希望它看起来像这样 这是节点 const express = require("express") const app = express() const https = require("https") var bodyParser = require('body-pa
const express = require("express")
const app = express()
const https = require("https")
var bodyParser = require('body-parser')
app.set("view engine", "ejs")
app.use(express.static("./public"))
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.get("/" , (req, res) =>{
res.sendFile(__dirname + "/index.html")
})
app.get("/:thename" , (req, res) =>{
res.render("countryinfo")
})
app.post("/:thename" , urlencodedParser,(req, res) =>{
let theName = `https://restcountries.eu/rest/v2/name/${req.params.thename}`;
https.get(`https://restcountries.eu/rest/v2/name/${req.params.thename}`, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
res.json(JSON.parse(data));
});
})
app.listen("3000")
这是javascript文件
countries.forEach(nation=>{
nation.addEventListener("click", getInfo)
function getInfo () {
var theName = nation.children[0].getAttribute("href")
$.ajax({
type: 'GET',
url: theName,
success: function(){
}
});
$.ajax({
type: 'POST',
url: theName,
data: {name: theName},
success: function(data){//updated data
}
});
}
})
如何将数据发送到呈现页面?
我的目录是这样的:您可能需要在接收数据的成功函数中更新前端 这就是我不能做的