-1

I am trying to get result from URL called by AJAX but even if I am calling the page by the script the success method doesn't show me the result:

<script type="text/javascript">
    function searchES() {

    var term = $('input[name=q]').val(), 
         url = "http://localhost:9200/_search",
      result = "";

        //alert( term );
        //alert( url );

            $.ajax({
            url: url,
            type:'get',
            data: {q: term},
            dataType: 'json',
                success: function(response) { $('#result').empty().append("Done is : " + response ); }
                });

            //event.preventDefault();
  }
</script>

By the Firefox Inspect element I can see that the own "get" has been spawned.

11:00:21.900 GET XHR http://localhost:9200/_search?q=*

Can you please advise what I overlooked?

Many thanks in advance, Regards, Reddy


Updated:

So the problem seems to be with CORS. Anyway, I have added the filtering into web.xml of the tomcat webapps application I am working on:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

and also added parameter

crossDomain:true,

into the ajax code but still getting error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9200/_search?q=*. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Elastic search has the CORS disabled by default. Hence the problem seems to be on my Tomcat webapps.

Any other ideas?

Reddy SK
  • 1,334
  • 4
  • 19
  • 27
  • 1
    first make sure that it is getting in the `success` event by making it to log to console with `console.log(response);` – Tolga Evcimen Dec 30 '15 at 10:11
  • May be problem with `q=*` – Parth Trivedi Dec 30 '15 at 10:11
  • 3
    Does the request you see being logged result in a `200 - OK` response, or some other code, potentially an error? It may be worth adding a handler for `error` as well as `success`. – James Thorpe Dec 30 '15 at 10:12
  • May be your URL is too large. Try using POST method instead – ikken Dec 30 '15 at 10:16
  • 1
    First call function like `searchES()` i can't see anywhere it is called. – Parth Trivedi Dec 30 '15 at 10:18
  • Why is there ,(comma) instead of ; (semi colon) in 1st 2 lines of searchES function? – Ankit Dec 30 '15 at 10:30
  • @Ankit because it's var statement. – jcubic Dec 30 '15 at 10:31
  • 1
    are you using CORS to get the result from a different location? It seems to me that your setup is correctly blocking your cross-site scipting. Have a look at CORS on how to fix it. Take care because a different port on the same domain is considered as another site – Lelio Faieta Dec 30 '15 at 10:37
  • @Lelio, that's the problem probably. I am going to investigate by this path. – Reddy SK Dec 30 '15 at 11:42
  • You have two servers. `http://localhost:9200` and another one which the HTML document is hosted on. Which of them did you configure to supply the Access-Control-Allow-Origin header? – Quentin Dec 30 '15 at 12:24
  • http://localhost:8080 is my Tomcat server where is running the page with the JavaScipt above. http://localhost:9200 is Elasticsearch from where I am trying to get the result – Reddy SK Dec 30 '15 at 12:43
  • BTW, when tried to use **dataType: 'jsonp'** then it generated URI http://localhost:9200/_search?callback=jQuery111309179810477755785_1451479264899&q=*&_=1451479264900 and the error is `SyntaxError: missing ; before statement` even if the jsonlint validated the output successfully. – Reddy SK Dec 30 '15 at 12:48
  • @ReddySK — Where did you configure CORS? On `:8080` or on `:9200`? – Quentin Dec 30 '15 at 13:08
  • @ReddySK — Obviously telling jQuery to parse the JSON as JSONP threw an error. JSON is not JSONP. – Quentin Dec 30 '15 at 13:08
  • @Quentin - on 8080. But 9200 has it disabled by default as well. – Reddy SK Dec 30 '15 at 13:23
  • @ReddySK — `8080` cannot give itself permission to read the data `9200` will send to the user's browser. `9200` needs CORS to be **enabled** (which it isn't, or at least not correctly, because otherwise the browser wouldn't tell you that the header was missing). – Quentin Dec 30 '15 at 13:23
  • @Quentin --- I am referring to elastic search where based on documentation is this behavior: _ http.cors.enabled Enable or disable cross-origin resource sharing, i.e. whether a browser on another origin can do requests to Elasticsearch. Defaults to false._ – Reddy SK Dec 30 '15 at 13:43
  • @ReddySK — It defaults to false? So it defaults to CORS **not granting permission** for JavaScript from other sites to read the data it will sent to the browser, so resources are **not shared**. – Quentin Dec 30 '15 at 13:48
  • @Quentin - I found the issue. there was missing option http.cors.allow-origin: "/.*/". Thank you so much for your time – Reddy SK Dec 30 '15 at 13:52

3 Answers3

0

Probably you're not getting a successful response. Try catching the error and the complete methods and checking what are the responses in those cases.

It will give a better insight about what's wrong with the call.

Check http://api.jquery.com/jquery.ajax/ for more info.

  • 1
    It seems that the probelm is not with the code but security restrictions: 12:27:40.983 Cross-Origin Request Blocked: **The Same Origin Policy disallows reading the remote resource** at http://localhost:9200/_search?q=*. (Reason: CORS header 'Access-Control-Allow-Origin' missing). – Reddy SK Dec 30 '15 at 11:30
  • @ReddySK read the answer on [link](http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) and see if it helps. – Ivo Pereira Dec 30 '15 at 13:53
0

After while and many attempts the problem was with Elasticsearch configuration.

There was missing option

http.cors.allow-origin: "/.*/"

Hence the proper complete configuration is

http.cors.allow-origin: "/.*/"
http.cors.enabled: true

and in Ajax has to call

crossDomain: true,

With this option I am getting the proper result.

Thank you all for your comments and hints.

Cheers, Reddy

Reddy SK
  • 1,334
  • 4
  • 19
  • 27
-2

First, check the value of term, then check the response you are getting using console.log() in the success() of ajax call.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Maddy
  • 105
  • 7