我正在尝试使用google / bing“自动建议”功能创建一个搜索框。搜索了该站点后,我找到的最接近的站点是:Custom box with autocomplete from Google/Bing. Is there any way to read the received json file?
jsfiddle.net上的脚本很好用,但是当我尝试复制代码时,它不起作用。
这是我的代码:
<html>
<head>
<script>
$(function () {
$("#hulk").autocomplete({
source: function (request, response) {
console.log("source");
$.ajax({
url: "http://api.bing.com/osjson.aspx?Query=" + encodeURIComponent(request.term) + "&JsonType=callback&JsonCallback=?",
dataType: "jsonp",
/*data: {
"Query": request.term,
"JsonType": "callback",
"JsonCallback" : "?"
},*/
success: function (data) {
console.log("success!");
var suggestions = [];
$.each(data, function (i, val) {
console.log("suggestion: " + val);
suggestions.push(val);
});
response(suggestions);
}
});
}
});
});
</script>
</head>
<body>
<input type="text" id="hulk" />
</body>
</html>
我不是编码专家(主要使用php),不胜感激可以帮助您完成这项工作:)
提前感谢!