0

簡単な検索機能を構築しようとしていますが、コードが機能しない理由がわかりません。

これは、検索するために作成したアクションです。

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 に返すようにしたいと考えています。これまでのところ、2 つの変数 (基準、値) を使用してそのコントローラー アクションに 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>
4

1 に答える 1