2017-10-15 68 views
0

我想在查詢具有過濾器時使用Ajax從elasticsearch檢索數據。 我正在使用GET。來自彈性搜索過濾查詢的Ajax 400錯誤請求

我的代碼如下:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script> 
    $(document).ready(function(){ 
     $.ajax({ 
      type: 'GET', 
      url : 'HOST/tomo2o/shop/_search', 
      crossDomain: true, 
      data: JSON.stringify({"query":{"match":{"catlev1":"Motors"}}}), 
      contentType:'application/json; charset=utf-8', 
      dataType: 'json', 
      success: function(data) { 
       console.log(data); 
      }, 
      error: function(e) { 
       console.log(e); 
      } 
     }); 
    }); 
</script> 

我做了三件事情的人建議做:

  1. 字符串化JSON的過濾器
  2. 設置數據類型爲JSON
  3. 集內容類型爲application/json

但我還是發現了以下錯誤:

GET HOST/tomo2o/shop/_search?{%22query%22:{%22match%22:{%22catlev1%22:%22Motors%22}}} 400 (Bad Request) 

回答

1

當你想通過你需要將它傳遞的參數source,並添加一個source_content_type參數指示內容類型

一個DSL query in the query string所以你可以這樣做:

$.ajax({ 
     type: 'GET', 
     url : 'HOST/tomo2o/shop/_search', 
     crossDomain: true, 
     data: { 
      source: JSON.stringify({"query":{"match":{"catlev1":"Motors"}}}), 
      source_content_type: "application/json" 
     }, 
     contentType:'application/json; charset=utf-8', 
     dataType: 'json', 
     success: function(data) { 
      console.log(data); 
     }, 
     error: function(e) { 
      console.log(e); 
     } 
    }); 
+0

很多感謝的人,按預期工作 – woshitom

+0

真棒,很高興它幫助! – Val