我正在将我的个人搜索建议从 google 转移到 duckduckgo,但我缺少一些简单的方法来让它发挥作用。我正在使用jQuery-UI 的自动完成框架。
我的搜索表单
<form action="https://duckduckgo.com/?q=" method="post" id="search">
<input type="text" name="query" value="" autocomplete="off">
<button type="submit">Search</button>
</form>
我的查询
$( "#search input[type=text]" ).autocomplete(
{
delay: 0,
minLength: 1,
position: { my: "left top-3" },
source: function( request, response )
{
// var suggestURL = "https://www.google.com/complete/search?client=firefox&q=%QUERY";
var suggestURL = "https://duckduckgo.com/ac/?q=%QUERY&type=list";
suggestURL = suggestURL.replace( "%QUERY", request.term );
$.ajax({
method: "GET",
dataType: "jsonp",
jsonpCallback: "jsonCallback",
url: suggestURL,
success: function( data )
{
response( data[1] );
},
error: function( jqXHR, textStatus, errorThrown )
{
console.log( textStatus, errorThrown );
}
}
});
对 google 的查询返回:
https://suggestqueries.google.com/complete/search?client=firefox&q=foobar&callback=jsonCallback&_=1600956954436
jsonCallback && jsonCallback(["foobar",["foobar","foobar meaning","foobar google","foobar challenge","foobar2000 skins","foobar2k","foobar2000 themes","foobar2000 download","foobar2000 mac","foobar themes"],[],{"google:suggestsubtypes":[[433],[],[],[],[],[],[],[],[],[]]}])
对 duckduckgo 的查询返回:
https://ac.duckduckgo.com/ac/?q=foobar&type=list&callback=jsonCallback&_=1600956892202
["foobar",["foobar2000","foobar","foobar2000 download","foobar ape","foobar2000 layout","foobar2000 decoder","foobar2000 tak","foobar2000 dsp"]]
两者之间的区别似乎jsonCallback && jsonCallback([data])包含在谷歌查询中,我不明白为什么它们不同或如何解决。
编辑 1
向 js 添加一些错误处理后,我得到的错误是:
parsererror Error: jsonCallback was not called
编辑 2
在深入研究之后,我认为 DDG 的服务器不允许这样做。据我了解,他们的服务器需要发送适当的响应,但我认为他们没有这样做。
慕森王
MMTTMM
相关分类