0

I am trying to make a post request with AJAX to my elasticsearch index. The cURL result is:

[~]$ curl -XGET 'http://localhost:9200/firebase/_search?q=song:i%20am%20in'

{"took":172,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.82749283,"hits":[{"_index":"firebase","_type":"song","_id":"0001","_score":0.82749283,"_source":{"song":"i am in california","song_name":"hello","song_url":"https://s3.ap-south-1.amazonaws.com/songapp-dump/media/songs/Adele_-_Hello-_i_am_in_california.mp3"}},{"_index":"firebase","_type":"song","_id":"0002","_score":0.28582606,"_source":{"song":"i must have called a thousand times","song_name":"hello","song_url":"https://s3.ap-south-1.amazonaws.com/songapp-dump/media/songs/Adele_-_Hello-_i_must_have_called_a_thousand_times.mp3"}}]}} 

Browser result is:enter image description hereThis is also working correctly. Meaning the index has been created and cURL/ GET is able to get the result.

When I am trying to have an AJAX request do the same, I am struggling with the query format probably. I am not able to figure out.

Ajax.js

$(function() {
    $('#message').keyup(function() {
        // console.log(JSON.stringify());
        var data = {
                'song': $('#message').val()
            };
        console.log(JSON.stringify(data));
        $.ajax({
            type: "POST",
            url: "http://localhost:9200/firebase/_search",
            contentType: 'application/json',
            // data: {
            //     'q': $('#message').val()
            // },
            data: JSON.stringify(data),
            success: searchSuccess,
            dataType: 'jsonp'
        });

    });

});

The console logs the following error: enter image description here

Basically it's a 400 Bad Request error. I am not able to figure out if there is something wrong with my query or the way Ajax request is being created. Why am I having callback issues! Any help would be appreciated. I have scoured the web on this issue and have tried various combinations as well.

Manganese
  • 650
  • 5
  • 26

1 Answers1

1

Change the method to GET and dateType to json. Also the querystring requires a q parameter.

     var data = {
         'q': 'song:' + $('#message').val()
     };

     $.ajax({
        type: "GET",
        url: "http://localhost:9200/firebase/_search",
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: searchSuccess,
        dataType: 'json'
    });
styfle
  • 22,361
  • 27
  • 86
  • 128
  • I will tell you once thing mate, this is the fastest ever I have received a perfect answer. changed to GET, started off with q and Done! Care to explain in brief what was wrong. As I read from other posts, GET/ POST doesn't matter much here. and 'jsonp' was needed for cross-origin requests. How did these not matter? – Manganese Jun 17 '17 at 18:54
  • 1
    JSONP is only needed if the origin you are calling does not support cross-origin requests...its basically a hack. You can read more [here](https://stackoverflow.com/q/2067472/266535). – styfle Jun 17 '17 at 19:00
  • The elasticseach docs say to use [GET](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html) as well as your curl example so I didn't even have to look at the docs. – styfle Jun 17 '17 at 19:03
  • Ah, of course. As long as it gets the job done. Thanks. – Manganese Jun 17 '17 at 19:05