七度&光 2017-03-29 21:35 采纳率: 22.2%
浏览 71

elasticsearch ajax调用错误

I try to use elasticsearch with an ajax call in my jquery webpage but I fail to make it work...

here is my ajax query :

var data = {
                query:{match:{_all:10}},
                fields: 'DEPET'
                };
            $.ajax({
                type : 'POST', // envoi des données en GET ou POST
                url : 'http://localhost:9200/_search' , // url du fichier de traitement
                crossDomain: true,  
              async: false,
              data: JSON.stringify(data),
              dataType : 'json',
                beforeSend : function() { // traitements JS à faire AVANT l'envoi
                    $('#ajax-loader2').append('<img src="/dev/wp-content/themes/codium-dn/images/ripple.gif" alt="loader" id="ajax-loader" />');
                },
                success : function(data){ // traitements JS à faire APRES le retour d'ajax-search.php
                    $('#ajax-loader').remove(); // on enleve le loader
                    $('#results').html(data); // affichage des résultats dans le bloc
                    console.log(data);
                },
                error: function (data, error) {
                    console.log(arguments);
                    //alert(" Can't do because: " + error);
                }
            })

I also put this in my elasticsearch.yml

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-credentials: true
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE

And this is what the console chrome dev give me :

(index):451 (3) [Object, "error", DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:9200/_s…, callee: function, Symbol(Symbol.iterator): function]

Request URL:http://localhost:9200/_search
Referrer Policy:no-referrer-when-downgrade
Request Headers
Provisional headers are shown
Accept:application/json, text/javascript, */*; q=0.01
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:https://domain.ovh
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.33 Safari/537.36
Form Data
view source
view URL encoded
{"query":{"match":{"_all":10}},"fields":"DEPET"}:

Thanks for your inputs

  • 写回答

1条回答 默认 最新

  • weixin_33725270 2017-03-30 06:54
    关注

    Try out this

    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "http://localhost:9200/_search",
      "method": "POST",
      "headers": {
        "cache-control": "no-cache",
        "postman-token": "c24112d5-9dc4-0230-7954-01064b61be5c"
      },
      "data": "{
      \"query\": {
        \"match_all\": {}
      }
    }
    "
    }
    
    $.ajax(settings).done(function (response) {
      console.log(response);
    });
    

    I have used postman tool to generate this code.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)