【发布时间】:2018-07-25 19:39:25
【问题描述】:
在亚马逊和维基百科自动完成 API 的实施中,我目前失败了。根据搜索参数,应使用另一个自动完成服务。不幸的是,添加 Wikipedia (? Search = 5) 后,这些服务都不起作用。在 Amazon API (?search=4) 我只能看到输入了几次的字母。我认为两者都是自动显示的URL参数“?callback = jSONXXXXXXXXX”的责备。
你能帮帮我吗?谷歌自动完成功能有效,但前提是不包括亚马逊或维基百科等其他服务。
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel='stylesheet prefetch' href='https://rsms.me/inter/inter-ui.css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" type="text/css" />
<script src="//code.jquery.com/jquery-2.1.4.js" type="text/javascript"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>
<script>
if (window.location.search.indexOf('search=2') > -1) {
jQuery(function() {
jQuery( "#hyv-search" ).autocomplete({
source: function( request, response ) {
//console.log(request.term);
var sqValue = [];
jQuery.ajax({
type: "POST",
url: "http://suggestqueries.google.com/complete/search?hl=de&ds=yt&client=youtube&hjson=t&cp=1",
dataType: 'jsonp',
data: jQuery.extend({
q: request.term
}, { }),
success: function(data){
console.log(data[1]);
obj = data[1];
jQuery.each( obj, function( key, value ) {
sqValue.push(value[0]);
});
response( sqValue);
}
});
}
});
});
} else if (window.location.search.indexOf('search=3') > -1){
jQuery(function() {
jQuery( "#hyv-search" ).autocomplete({
source: function( request, response ) {
//console.log(request.term);
var sqValue = [];
jQuery.ajax({
type: "POST",
url: "http://suggestqueries.google.com/complete/search?hl=de&ds=sh&client=youtube&hjson=t&cp=1",
dataType: 'jsonp',
data: jQuery.extend({
q: request.term
}, { }),
success: function(data){
console.log(data[1]);
obj = data[1];
jQuery.each( obj, function( key, value ) {
sqValue.push(value[0]);
});
response( sqValue);
}
});
}
});
});
} else if (window.location.search.indexOf('search=4') > -1){
jQuery(function() {
jQuery( "#hyv-search" ).autocomplete({
source: function( request, response ) {
//console.log(request.term);
var sqValue = [];
jQuery.ajax({
type: "POST",
url: "https://completion.amazon.co.uk/search/complete?method=completion&mkt=4&p=Gateway&l=de_DE&b2b=0&fresh=0&sv=desktop&client=amazon-search-ui&x=String&search-alias=aps&ks=84",
dataType: 'jsonp',
data: jQuery.extend({
q: request.term
}, { }),
success: function(data){
console.log(data[1]);
obj = data[1];
jQuery.each( obj, function( key, value ) {
sqValue.push(value[0]);
});
response( sqValue);
}
});
}
});
});
} else if (window.location.search.indexOf('search=5') > -1){
jQuery(function() {
jQuery( "#hyv-search" ).autocomplete({
source: function( request, response ) {
//console.log(request.term);
var sqValue = [];
jQuery.ajax({
type: "POST",
url: "https://de.wikipedia.org/w/api.php",
jsonp : false,
jsonpCallback: 'jsonCallback',
// contentType: 'application/json', -- you can't set content type for a <script> tag, this option does nothing for jsonp | KevinB
cache: 'true',
dataType : 'jsonp'
data: jQuery.extend({
action: 'opensearch',
search: request.term
}, { }),
success: function(data){
console.log(data[1]);
obj = data[1];
jQuery.each( obj, function( key, value ) {
sqValue.push(value[0]);
});
response( sqValue);
}
});
}
});
});
} else {
jQuery(function() {
jQuery( "#hyv-search" ).autocomplete({
source: function( request, response ) {
//console.log(request.term);
var sqValue = [];
jQuery.ajax({
type: "POST",
url: "http://suggestqueries.google.com/complete/search?hl=de&client=youtube&hjson=t&cp=1",
dataType: 'jsonp',
data: jQuery.extend({
q: request.term
}, { }),
success: function(data){
console.log(data[1]);
obj = data[1];
jQuery.each( obj, function( key, value ) {
sqValue.push(value[0]);
});
response( sqValue);
}
});
}
});
});
}
</script>
和 HTML 输入...
<input id="hyv-search" class="ui-autocomplete-input" type="text" name="suche">
来自德国的问候,感谢您的帮助!
【问题讨论】:
-
您可以添加html以获得更多说明吗?如果可能的话,然后添加堆栈代码 sn-p
-
完成。感谢您的帮助!
-
检查浏览器 console.log.. 你遇到什么样的错误?
-
jquery-2.1.4.js 文件应该放在 bootstrap.min.js 之前。
-
控制台中没有任何内容。我不明白为什么...
标签: javascript php jquery ajax