Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple jQuery based sample to show Bing Search v5 API in action
<!DOCTYPE html>
<html>
<head>
<title>Bing Search v5 - Simple, show fewer results</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
// more info: http://mvark.blogspot.com/2016/06/how-to-use-bing-search-v5-api-with.html
var total;
var results = '';
// In this sample, I'm trying to find the pages of my blog that Bing has indexed with the "site:" operator with the "q" parameter
//For more info on the other querystring parameters refer to the documentation - https://bingapis.portal.azure-api.net/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d
$(function() {
var params = {
// Request parameters
"q": "site:mvark.blogspot.com",
"count": "50",
"offset": "0",
"mkt": "en-us",
//"safesearch": "Moderate",
"freshness":"Week",
};
$.ajax({
url: "https://bingapis.azure-api.net/api/v5/search/?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","abc123"); //replace value with your own key
},
type: "GET",
// Request body
//data: "{body}",
})
.done(function(data) {
len = data.webPages.value.length
for (i=0; i<len; i++ )
{
results += "<p><a href='" + data.webPages.value[i].url + "'>" + data.webPages.value[i].name + "</a>: " + data.webPages.value[i].snippet + "</p>";
}
$('#content').html(results);
})
.fail(function() {
alert("error");
});
});
</script>
Results:
<div id="content">Fetching...</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment