我使用 jQuery 的 $.ajax()
将请求发送到 SOLR 服务器并获取 JSON 格式的结果,但它不起作用。我的代码是正确的,因此我无法弄清楚出了什么问题。如果我将 URL 粘贴到浏览器的地址栏中,我会得到正确的 JSON 输出。
我在 Google 上搜索并在 Stack Overflow 上发现了一些类似的问题:一个问题说可以使用 $.getJSON
代替,但没有给出任何理由,但这对我来说也不起作用。
现在,我知道有一个 AJAX-SOLR 框架并且我将使用它,但我想知道为什么 SOLR 和 jQuery 存在问题。
以下是我使用 $.getJSON
的尝试,以防万一这是我的错误。
$('#searchForm').on('submit', function(){
// get the user entered search query
var query = $('#queryBox').val();
// send the AJAX request to solr and get the results
$.getJSON('http://localhost:8983/solr/select/?q='+query+'&wt=json&indent=true', function(searchResult){
alert('success');
});
alert(query);
return false;
});
我没有收到“成功”警报。回调函数永远不会被执行。我该如何解决这个问题?
最佳答案
请确保将 json.wrf=?
参数添加到请求 URL,以便调用 success 方法。
json.wrf=function - adds a wrapper-function around the JSON response, useful in AJAX with dynamic script tags for specifying a JavaScript callback function.
您仍然可以直接使用 getJson 函数从 Solr 获取 json 响应。
关于javascript - 为什么 SOLR 不能与 jQuery 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17429790/