【发布时间】:2019-04-27 00:26:04
【问题描述】:
跟进我之前的两个问题: get data from async function 和 add id to checkbox using loop
我现在正在尝试使用 nodejs 访问选中的复选框,并在我单击结果页面中的按钮时在表格中表达 但尝试记录 req.body 只会给我空字符串或未定义。
我添加了一个文本输入,只是为了查看一切是否按预期工作,是的,当我单击按钮时,我可以在结果页面上记录它的内容,所以问题肯定来自表格和复选框。
到目前为止,这是我的代码:
服务器:
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const TorrentSearchApi = require('torrent-search-api')
TorrentSearchApi.enableProvider('Yggtorrent','login', 'password')
//TorrentSearchApi.enableProvider('1337x');
app.use(express.static('public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.set('view engine', 'ejs')
var torrentsG = []; //empty global variable to store the torrents
const search = async query => {
const loweredQuery = query.toLowerCase()
const torrents = await TorrentSearchApi.search(loweredQuery, 'All', 5)
return JSON.stringify(torrents)
}
app.get('/', (_, res) => res.render('index'))
app.post('/', async (req, res) => {
const torrents = await search(req.body.torrent)
torrentsG = JSON.parse(torrents);
//console.log(torrentsG);
res.redirect('/results');
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
app.get('/results',(_ ,res) => res.render('results', {torrents:torrentsG}))
app.post('/results',function (req,res){
console.log(req.body);
})
客户
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="/css/table.css">
</head>
<body align="left">
<div class="container">
<form action="/results" method="post">
<input name="torrent" type="text" class="ghost-input" placeholder="Search a Torrent" required>
<table name="tableau" class="table" align="left">
<thead>
<tr>
<th>Download </th>
<th>Title</th>
<th>time </th>
<th>seeds </th>
<th>size </th>
<th>provider </th>
<th>link </th>
</thead>
<tbody name="TBODY">
<% for (var i = 0; i < torrents.length; i++) { %>
<tr>
<td> <input type="checkbox" name="checkbox" id="checkbox" Class="checkbox"> </td>
<td> <%= torrents[i].title%> </td>
<td> <%= torrents[i].time%> </td>
<td> <%= torrents[i].seeds%> </td>
<td> <%= torrents[i].size%> </td>
<td> <%= torrents[i].provider%> </td>
<td> <%= torrents[i].link%> </td>
</tr>
<% }%>
</tbody>
</table>
<input type="submit" class="ghost-button" value="Download selected torrents to seedbox">
</form>
</div>
</body>
</html>
如您所见,我尝试为复选框的父元素命名,以便它们至少出现在 req.body() 中而没有任何成功。 另一方面,“torrent”文本输入被正确记录在 req.body
提前感谢您的回答,我希望这将是关于这个“项目”的最后一个
【问题讨论】:
标签: javascript node.js express ejs