【发布时间】:2020-02-04 22:55:39
【问题描述】:
获取汽车列表的方法:
const express = require('express')
const app = express()
app.use(express.static('public'))
app.get('/cars', (req, res) => {
res.status(200).json([{
name : 'a',
color : 'red'
},{
name : 'b',
color : 'blue'
}])
})
module.exports = app
我正在尝试将列表加载到我的 html 页面中=>
点击id为load的按钮时,请求服务器上cars的列表;带有red 颜色的汽车被加载到带有main id 和tr 的表中
每辆车。
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>A simple app</title>
<script>
async function load(){
try{
let url ='http://localhost:8080/cars'
let response = await fetch(url)
let data = await response.json()
let table = document.getElementById('main')
table.innerHTML = ''
for (let e of data){
let rowContent = `
<tr>
<td>
${e.name}
</td>
<td>
${e.color}
</td>
</tr>`
let row = document.createElement('tr')
row.innerHTML = rowContent
//row.dataset.id = e.id
table.append(row)
}
catch(err){
console.warn(err)
}
}
}
</script>
</head>
<body>
A simple app
<table id=main></table>
<input type="button" value="add" onClick={load()} id="load"/>
</body>
</html>
【问题讨论】:
-
你看到了什么错误?
-
● 本地 › 应在单击时加载错误:找不到匹配选择器“#main tr:first-child td:first-child”的元素 23 |等待 page.click('#load') 24 |等待 page.waitForSelector('#main') > 25 | const first = await page.$eval("#main tr:first-child td:first-child", e => e.textContent)
-
首先,你要改
onClick={load()} -
那么您可能会遇到 cors 问题,具体取决于您的本地设置
-
我想这是 cors 问题
标签: javascript express server single-page-application