Ajax和Elasticsearch

My code is below:

       var query = {
            query: {
                query_string: {
                    query: "RM"
                }
            }
        };
        $.ajax({
            url:   "http://localhost:9200/lelongoct4/product/132827698/_search",
            type: 'POST',
            dataType: 'json',
            crossDomain: true,
            data: JSON.stringify(query),
            success: function (data) {
                alert("Success");

            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);

            },
           });

The same query works fine in console. But in ajax i am having some trouble to implementing it. It is going to the error message with status 0. Need some help pls.

There are two issues in your code:

  1. You need to use the jsonp data type instead
  2. Your URL needs to be like this: "http://localhost:9200/lelongoct4/product/_search", i.e. without the product id 132827698

Then in order for JSONP to work, you need to enable CORS by setting http.cors.enabled: true in your elasticsearch.yml configuration file and restart ES.

Then it will work.