【问题标题】:How to do instant search with mongoosastic + AJAX?如何使用 mongoosastic + AJAX 进行即时搜索?
【发布时间】:2016-02-28 01:44:15
【问题描述】:

我已经成功配置了 mongoosastic,我尝试搜索,它工作正常,但是在前端我不确定如何实现这一点,我尝试了很多方法但无法来找到一个好的解决方案。

这是代码。

// For the Search API
    router.post('/api/search/', function(req, res, next) {
      Job.search(
        {
          query_string:
          { query: req.body.search }
        } , function(err, results) {
            if (err) return next(err);
            res.json(results);
        });
    });

所以每当我搜索与“工程师”相关的东西时,我都会得到一个 json 数据

所以后端运行良好。

但是,当涉及到 jquery 和 ajax 时,我不断收到错误的请求

逻辑:无论何时插入某些内容,然后发布该内容并找到该结果。

这是前端 jquery 代码。

  $('#search').keyup(function() {

    var search_term = $(this).val();


    $.ajax({
      type: "POST",
      url: "/api/search",
      success: function(){
        $('search_results').html(search_term);
      },
      error: function(data){
        alert('Error', data);
      }
    });

  });

HTML

<input type="text" class="form-control input-lg" id="search" name="search" placeholder="Search for part-time..." />



 <div id="search_results">


    </div>

如何将json结果插入search_results

【问题讨论】:

    标签: jquery ajax node.js elasticsearch mongoosastic


    【解决方案1】:

    你需要做的是

    1. 在每次击键时,获取输入值
    2. 通过 Ajax 将其发送到您的后端
    3. 使用成功后返回的 JSON(这里我们只是在 div 中显示“标题:薪水”)

    所以你的前端代码应该是这样的:

    $('#search').keyup(function() {
    
      // 1. grab the search term from the input field
      var search_term = $(this).val();
    
      // 2. send it to your back-end via ajax in the body 
      $.ajax({
        method: "POST",
        url: "/api/search",            // <-- your back-end endpoint
        data: "search=" + search_term  // <-- what you're sending
        dataType: "json",              // <-- what you're expecting back
        success: function(json){       // <-- do something with the JSON you get
          // 3. parse the JSON and display the results
          var res = json.hits.hits.map(function(hit) {
              return hit._source.title + ": " + hit._source.salary;
          });
          $('search_results').html(res.join("<br />"));
        },
        error: function(data){
          alert('Error', data);
        }
      });
    });
    

    【讨论】:

    【解决方案2】:

    我现在正在研究与此非常相似的东西。首先,看起来您在初始 ajax 请求中缺少参数,因此您的节点服务器没有获得任何信息:

    $('#search').keyup(function() {
    //Perform the ajax request
    var search_term = $(this).val();
    $.ajax({
      type: "POST",
      url: "/api/search",
      data: {"search": search_term},
      success: function(data) {
         //Loop over all the data and output it, appending data to element
         //with a line break
         for(var i=0; i < data.hits.length; i++)
        $('search_results').append(data.hits[i]._source.title+'<br />');
      },
      error: function(data){
        alert('Error', data);
      }
    });
    
    });
    

    在调试这些问题时,如果您可以访问我的节点服务器中的 console.log() 以查看它实际获取的数据,这对我非常有帮助:

    // For the Search API
    router.post('/api/search/', function(req, res, next) {
      console.log("%j", req.body.search)
      Job.search(
        {
          query_string:
          { query: req.body.search }
        } , function(err, results) {
            if (err) return next(err);
            res.json(results);
        });
    });
    

    日志将非常清楚地报告未定义或您期望的正确字符串,以便您查看参数是否正确

    您可能还想检查您的节点服务器地址和端口(作为健全性检查)。对于我的 ajax 请求,我必须提出

    url: "http://localhost:9200/api/search" 
    

    在我的查询前面只是因为我的服务器在不同的端口上

    这里有一些关于 jQuery post 函数的更多信息供您参考:

    http://api.jquery.com/jquery.post/

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 2014-02-05
      相关资源
      最近更新 更多