我想构建一个简单的搜索功能,但我找不出为什么我的代码不工作。实现搜索功能sailsjs
这是我建立了搜索行动:我有我的主网页上的按钮
search: function(req, res) {
var criteria = req.param('criteria');
var value = req.param('value');
Human.find().where({ criteria: value }).done(function(err, humans) {
if(err) {
return console.log('Error:' + err);
}else{
res.view({
title: 'Search',
humans: humans
});
}
});
}
与搜索的ID。我想这样做只要有人点击我的搜索按钮,它就会查询数据库并在localhost:1337/model/search处返回结果。到目前为止,我已经尝试使用两个变量(条件,值)向该控制器操作发送ajax请求,但它不起作用。
这是AJAX调用,我提出:
$('#search').on('click', function() {
var criteria = $('#filter').val();
var value = $('#value').val();
$.ajax({
type: 'POST',
url: 'http://localhost:1337/human/search',
cache: false,
data: {
criteria: criteria,
value: value
},
success: function(data) {
console.log('SUCCESS!');
window.location.href = 'http://localhost:1337/human/search';
},
error: function(err) {
console.log('ERROR!');
}
});
});
这是对应的视图:
<table>
<thead>
<tr>
<th>ID</th>
<th width="150">First Name</th>
<th width="150">Last Name</th>
<th width="150">Contact</th>
<th width="150">E-Mail</th>
<th>View</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<% _.each(humans, function(model) { %>
<tr>
<td> <%= model.id %> </td>
<td> <%= model.firstName %> </td>
<td> <%= model.lastName %> </td>
<td> <%= model.contact %> </td>
<td> <%= model.email %> </td>
<td><a href="/human/view/<%= model.id %>" class="tiny button">VIEW</a></td>
<td><a href="/human/edit/<%= model.id %>" class="tiny button">EDIT</a></td>
</tr>
<% }) %>
</tbody>
</table>
Criteria =>属性;值=>该属性的值;基本上,我试图写:Human.find()。其中({id:2})(例如:criteria = id; value = 2;) –
我试过了你提供的代码,但它仍然没有工作。也许我错过了一些东西。我会尽力更好地解释这个过程。我有一个搜索ID(使用Zurb Foundation Framework)的页面上的按钮:Search当有人点击这个按钮时,它会触发我发布并导航到localhost:1337/human/search的点击事件;它返回一个空视图; –
检查编辑的答案 – ataman