gpt4 book ai didi

javascript - Freebase - Javascript 中的 MQL

转载 作者:行者123 更新时间:2023-11-30 18:24:33 26 4
gpt4 key购买 nike

freebase 网站上的例子

http://wiki.freebase.com/wiki/MQL_Manual_Javascript_Example

使用http://api.freebase.com/api/service/mqlread

<script>                                
function listAlbums(band) { // Display albums by the specified band.
var envelope = { // The mqlread query envelope
query : { // The MQL query
type: "/music/artist", // Find a band
name: band, // With the specified name
album: [{ // We want to know about albums
name:null, // Return album names
release_date:null, // And release dates
sort: "release_date", // Order by release date
"release_type!=":"single" // Don't include singles
}]
}
};

var output = $("#output"); // Output goes here
output.html("<h1>Albums by " + band + "</h1>"); // Display a title

// Invoke mqlread and call the function below when it is done.
// Adding callback=? to the URL makes jQuery do JSONP instead of XHR.
jQuery.getJSON("http://api.freebase.com/api/service/mqlread?callback=?",
{query: JSON.stringify(envelope)}, // URL parameters
displayResults); // Callback function

// This function is invoked when we get the result of our MQL query
function displayResults(response) {
if (response.code == "/api/status/ok" &&
response.result && response.result.album) { // Check for success...
var list = $("<ul>"); // Make <ul> tag.
output.append(list.hide()) // Keep it hidden
var albums = response.result.album; // Get albums.
jQuery.each(albums, function() { // Loop through albums.
list.append($("<li>").html(this.name)); // Make <li> for each.
});
list.show("normal"); // Reveal the list
}
else { // On failure...
output.append("Unknown band: " + band); // Display message.
}
}
}
</script>

这显然已被弃用;是否有使用新 https://www.googleapis.com/freebase/v1/mqlread 的代码示例应用程序接口(interface)?似乎在网上找不到太多东西(我还在摸索)。

最佳答案

新的 API 发生了一些变化。您可以使用新的 API 在 Javascript 中运行相同的查询,如下所示:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script>
var API_KEY = 'YOUR-API-KEY-GOES-HERE';
var service_url = 'https://www.googleapis.com/freebase/v1/mqlread';
var band = "The Rolling Stones";
var query = {
type: "/music/artist",
name: band,
album: [{
name:null,
release_date: null,
sort: "release_date",
"release_type!=":"single"
}]
};
var params = {
'key': API_KEY,
'query': JSON.stringify(query)
};
$.getJSON(service_url + '?callback=?', params, function(response) {
$.each(response.result.album, function(i, album) {
$('<div>', {text:album['name']}).appendTo(document.body);
});
});
</script>
</body>
</html>

关于javascript - Freebase - Javascript 中的 MQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11228095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com