[英]how to request API and send it to front
I am new to node.js and I want to practice.我是 node.js 的新手,我想练习。 I found this example .
我找到了这个例子。
I made the home page good home page but when I click any country I can't display its information, I mean I can request the data for the country I clicked but I can't send it to the page that I rendered to display these information I want it looks like this countryinfo我将主页设置为很好的主页,但是当我单击任何国家/地区时,我无法显示其信息,我的意思是我可以请求我单击的国家/地区的数据,但我无法将其发送到我呈现的页面以显示这些我想要的信息看起来像这个countryinfo
this is the node这是节点
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")
and this is the javascript file这是 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
}
});
}
})
what I should do to send data to rendered page?我应该怎么做才能将数据发送到呈现的页面? this how my directories looks like: directories
这是我的目录的样子:目录
You may want to update your front in the success function where data has been received.你可能想更新你前面的成功 function 那里已经收到数据了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.