Reputation: 3200
I have a requirement in my website where the user can type in some "text" in a textbox and i have to show some search suggestions in a drop down. I have signed up for a Bing Search account and i got an account key. But i am not able to see any example code how to implement this feature using Jquery.
My website is written in Asp.Net MVC and it would be great if some one could point to an example code.
Thanks !
Upvotes: 0
Views: 1316
Reputation: 3215
I wrote an easy library to do this for my own needs back in June: jquery-bingsearch (GitHub source and web page). I realize you probably already solved your issue but I wanted to leave this here for future Google juice.
I'm using it on my personal web page.
Get an app key by following step 1 on this page.
<script type="text/javascript" src="js/jquery.bingsearch-min.js"></script>
Note: At least one of beforeSearchResults
, afterSearchResults
, or searchResultInterator
must be passed in, or the plugin will do nothing (since there's nothing to which it can return
the results. This is in addition to the required fields listed below.
$.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
}
});
Upvotes: 0
Reputation: 3362
http://api.bing.net/xml.aspx?Appid=<AppID>&sources=spell&query=cofee
Source: http://www.bing.com/developers/s/APIBasics.html
All you need to do is to post the REST API request via XHR (POST) and get the data as text. You can parse the repsonse with JSON.parse().
Upvotes: 1