0

I'm trying to build a simple search feature, but I can't figure out why my code is not working.

This is the action that I have built to search:

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
        });
      }
    });
}

I have a button on my main page with the ID of search. I want to make it so that whenever someone clicks my search button, it queries the database and returns the results at localhost:1337/model/search. So far, I've tried sending an ajax request to that controller action with the two variables (criteria, value) but it doesn't work.

This is the ajax call that I am submitting:

$('#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!');
        }
    });
});

And this is the corresponding view:

<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>
4

1 回答 1

1

问题 #1:当您像这样搜索模型时:Human.find().where({ criteria: value })实际上是按名为“条件”的字段搜索,而不是按字段搜索,该名称保存在criteria变量中。尝试像这样创建搜索对象:

var searchObj = {};
searchObj[criteria] = value;
// and then search like you did before
Human.find().where(searchObj).done(function(err, humans) {
  if(err) {
    console.log('Error:' + err);
    // you should return some response here:
    res.send(err, 500);
  }else{
    res.view({
      title: 'Search',
      humans: humans
    });
  }
});

问题#2:为什么你做ajax请求然后重定向到同一个url?
首先,您发出 POST 请求,尽管 GET 请求更适合搜索对象。创建资源时通常使用 POST 。
其次,在 ajax 成功处理程序中,当您收到包含找到的人类模型的视图后,您只需将浏览器重定向到http://localhost:1337/human/searchurl而不传递任何参数,因此您的控制器将尝试按空值和条件进行搜索Human.find().where({ "": "" })。所以你不会看到预期的结果。

不清楚您是想通过 ajax 获取数据,还是只是在新的 HTML 页面中显示它?

编辑:如果您不想使用 ajax,让 HTML 表单为您完成工作:

  <form action="human/search">
      <input name="criteria" value="foo">
      <input name="value" value="bar">
      <button type="submit" id="search">Search</button>
  </form>

搜索按钮单击将提交表单并在 GET 请求的查询字符串中传递所有表单数据:http://localhost:1337/human/search?criteria=foo&value=bar

当然,您可以使用 javascript 手动构建查询字符串,而无需使用表单,并将浏览器重定向到该 url。结果将是相同的。

于 2013-08-20T09:33:12.003 回答