我的网站中有一个要求,用户可以在文本框中输入一些“文本”,我必须在下拉列表中显示一些搜索建议。我已注册Bing搜索帐户,但我获得了一个帐户密钥。但我无法看到任何示例代码如何使用Jquery实现此功能。
我的网站是用Asp.Net MVC编写的,如果有人可以指出一个示例代码,那将会很棒。
谢谢!
答案 0 :(得分:1)
http://api.bing.net/xml.aspx?Appid=<AppID>&sources=spell&query=cofee
来源:http://www.bing.com/developers/s/APIBasics.html
您需要做的就是通过XHR(POST)发布REST API请求并将数据作为文本获取。您可以使用JSON.parse()解析repsonse。
答案 1 :(得分:0)
我在6月份写了一个简单的库来满足我自己的需求:jquery-bingsearch(GitHub源代码和网页)。我意识到你可能已经解决了你的问题,但我想把它留在这里以备将来的谷歌。
我在personal web page上使用它。
按照this page上的第1步获取应用密钥。
<script type="text/javascript" src="js/jquery.bingsearch-min.js"></script>
注意:至少有一个beforeSearchResults
,afterSearchResults
或searchResultInterator
必须传入,否则插件将什么都不做(因为它没有任何东西可以返回
结果。这是下面列出的必填字段的补充。
$.bingSearch({
// Required: query text
query: 'query text here',
// Required (unless you use urlBase) by Bing Search API
appKey: 'Put your Windows Azure Marketplace Bing Search API Primary Account Key here'
// Optional (defaults to the Bing Search API Web Results Query).
// Additional information: This feature allows you to proxy through a server-side
// script in order to hide your API key, which is exposed to the
// world if you set it client-side in appKey. An example PHP
// script is included (searchproxy.php).
urlBase: 'searchproxy.php',
// Optional (defaults to 1): Page Number
pageNumber: parseInt($('#pageNumber').val()),
// Optional (defaults to 10): Page Size
pageSize: 10,
// Optional (defaults to null): Limit to site. Shortcut to adding "site:example.org " to query
limitToSite: 'example.org',
// Optional (defaults to false): Print console logging information about search results
debug: false,
// Optional: Function is called after search results are retrieved, but before the interator is called
beforeSearchResults: function(data) {
// Use data.hasMore, data.resultBatchCount
},
// Optional: Function is called once per result in the current batch
searchResultIterator: function(data) {
// Use data.ID, data.Title, data.Description, data.Url, data.DisplayUrl, data.Metadata.Type (check for undefined)
},
// Optional: Function is called after search results are retrieved and after all instances of the interator are called
afterSearchResults: function(data) {
// Use data.hasMore, data.resultBatchCount
},
// Optional: Called when there is an error retrieving results
fail: function(data) {
// data contains an error message
}
});