想要对azure marketplace进行查询。 API验证是空用户+密码。返回应该是一个json对象。下面的代码将“[object Object]”返回给浏览器。我的错误在哪里?
<script>
$.ajax({
type: 'GET',
url: 'https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%keyword%27',
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth("", "myaccountkeyhere"));
},
success: function (data) {
JSON.stringify(data, null, 4);
document.write(data);
console.log(data);
}
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
答案 0 :(得分:0)
问题是你是document.writing JSON对象。
你需要:
var jsonString = JSON.stringify(data, null, 4); document.write(jsonString); console.log(jsonString);
我为此做了一个快速的方法。请注意,jsfiddle不允许document.write但是替换你的密钥,你应该看到它正常工作。
jsfiddle for loading bing search via json
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
function writeToDom(title, content) {
$("#results").append("<div class='header'>" + title + ":</div><div><pre>" + content + "</pre></div>");
}
function showResults(evnt) {
$.ajax({
type: 'GET',
url: 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27keyword%27',
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth("", "YOURKEYHERE"));
},
success: function (data) {
var search = JSON.stringify(data, null, 4);
writeToDom('Json', search);
alert(search);
console.log(search);
}
});
}
$(function() {
$(document).on("click", "#clickme", showResults);
});