Using JavaScript to read a JSON feed from Solr on a remote server- how is it actually done?
I cannot seem to get this to work, I have heard rumours 开发者_如何学Gothat you actually need a jsonp object.
Does anybody have a code snippet for reading a JSON feed from Solr on a remote server?
Solution:
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js'></script>
<script type='text/javascript'>
$.getJSON("http://remotehost:8080/solr/select/?q=jaberwocky&wt=json&json.wrf=?", function(result){
alert("hello" + result.response.docs[0].name);
});
</script>
The complicated bit is understanding nameless callbacks in this case json.wrf=?. Basically if you add json.wrf=? to your solr url it will start working
JSONP appears to be a red herring in this instance
The first result for "solr jsonp" gives Solr and JSONP. Does that work for you?
EDIT: To show this is just JSONP, and using the question mark replacement is optional (but convenient), you can just use getScript
and your own callback:
function my_callback(response)
{
}
$.getScript("http://remotehost:8080/solr/select/?q=jaberwocky&wt=json&json.wrf=my_callback");
You don't even need jQuery. You could do the same thing by manually creating and appending a <script>
element.
You can use JSONP for jQuery for jQuery JSONP requests. You can use it easily to read data from remote server.
精彩评论