I've set up Solr locally. It works fine using queries in browser. I'm tryng to acces JSON-queryresults in JQuery, but I'm not able.
When I run the following request in my browser i get the following resut: http://localhost:8983/solr/Products/select?q=HPI-850&fl=id%2CPris&&rows=2&wt=json&json.wrf=solrCallback
solrCallback({
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"q":"HPI-850",
"json.wrf":"solrCallback",
"fl":"id,Pris",
"rows":"2",
"wt":"json"}},
"response":{"numFound":3701,"start":0,"numFoundExact":true,"docs":[
{
"Pris":1195,
"id":"10440"},
{
"Pris":1195,
"id":"16656"}]
}})
Running the following code in my browser at localhost I get the JSONP response from the example I found at https://www.sitepoint.com/jsonp-examples/, but I get no response from my own URL. Why? I my code bad, or does Solr block the request in any way?
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>FRA TEST</h1>
<div id="Test_JSON"></div>
<br /><br />
<h1>FRA SOLR</h1>
<div id="SOLR_JSON"></div>
<script>
function jsonCallback(json) {
//alert(JSON.stringify(json));
$("#Test_JSON").html(JSON.stringify(json));
}
$.ajax({
url: "http://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json",
dataType: "jsonp"
});
function solrCallback(json) {
//alert(JSON.stringify(json));
$("#SOLR_JSON").html(JSON.stringify(json));
}
$.ajax({
url: "http://localhost:8983/solr/Products/select?q=HPI-850&wt=json&json.wrf=solrCallback",
dataType: "jsonp"
});
</script>
</body>
</html>
I don't know the details of why it did not work with JSONP, because as I understand, it should have worked. While searching for a solution I ended up enabling CORS in Solr.
I followed this guide: http://laurenthinoul.com/how-to-enable-cors-in-solr/
After doing so, I have no problem getting my Json from Solr by JQuery.