Hi,
I've test it at my side, actually the code is used to show how to send Ajax request to Bing search API, if you need the code work immediately, i suggest you change the code like this:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js" type="text/javascript"> </script>
<script type="text/javascript">
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', 'Basic <:Key>');
}
var fetchIndex = 0;
function GetBing() {
//Build up the URL for the request
var requestStr = "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query=%27hi%27&$top=50&$format=Json";
//Return the promise from making an XMLHttpRequest to the server
$.ajax({ url: requestStr, beforeSend: setHeader
})
.done(
//Callback for success
function (request) {
var results = [], count;
// Works for FireFox
var obj = JSON.parse(request);
// Wokrs for IE 10 and Chrome
var obj = request;
// Verify if the service has returned images
if (obj.d !== undefined) {
var items = obj.d.results;
// Data adapter results needs an array of items of the shape:
for (var i = 0, itemsLength = items.length; i < itemsLength; i++) {
var dataItem = items[i];
results.push({
key: (fetchIndex + i).toString(),
data: {
title: dataItem.Title,
thumbnail: dataItem.Thumbnail.MediaUrl,
width: dataItem.Width,
height: dataItem.Height,
linkurl: dataItem.MediaUrl
}
});
}
document.write(results[1].data.title);
}
else {
}
});
}
</script>
Here i need clarify something, notice these code in code block:
// Works for FireFox
var obj = JSON.parse(request);
// Wokrs for IE 10 and Chrome
var obj = request;
FireFox (I use FireFox 14 at my side) can not Parse Json string variable without JSON.parse method, so here you need a code logic to judge browser type to make the code works for these 3 browsers.
And the JQuery code is also not works for IE 8 & IE 9 becuase cross-domain origin is not supported, so in IE 8 & IE 9 environment, the recommended way is use Server-side code (HttpWebRequest).
Hope his helps.
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact
msdnmg@microsoft.com
Microsoft One Code Framework