Solr and JSONP

Need to send an Ajax request to a Solr server that’s on a different domain? You will, of course, need to use JSONP instead of an ordinary JSON request, due to JavaScript’s cross-domain security restrictions. To get a properly padded response from Solr, add the json.wrf parameter to your query string, giving it the name of your callback function. In jQuery:

jQuery.ajax({
  url: mySolrUrl,
  data: myQueryParameters,
  success: mySuccessCallback,
  dataType: 'jsonp',
  jsonp: 'json.wrf'
});

Of course, for this to work the Solr server you’re accessing needs to be publicly accessible, which probably isn’t ideal for security.

6 Responses to Solr and JSONP

  1. unexpand says:

    FWIW, you could simple secure your solr installation via IP tables in this case, granting access only to sites within specified IP ranges.

  2. guest says:

    Very thanks your simple case. It helps me.

  3. Amit says:

    Can you please also give an example of indexing solr using jsonp.
    I am trying in similar way, but it fails.
    The code below doesn’t work.

    $.ajax({
    url: “http://192.168.10.113:8080/solr/update/json?commit=true”,
    type: “POST”,
    data: { “add”: { “doc”: { “id”: “222222”, “name”: “Ruby on Trails”} } },
    dataType: ‘jsonp’,
    crossDomain: true,
    jsonp: ‘json.wrf’
    });

  4. Andrew says:

    I love you. Thanks you.

  5. Andrew says:

    *thank you

  6. Denis says:

    You made my day. My weekend, actually. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">