我正在尝试向我的网站添加一个项目最新 github 版本的下载链接。例如链接https://github.com/mongodb/mongo/archive/r3.0.0-rc7.zip https://github.com/mongodb/mongo/archive/r3.0.0-rc7.zip确实链接到最新版本(截至今天),但我不想在网站上硬编码版本号。
我发现了关于这个问题的几个问题,answers https://stackoverflow.com/questions/24085978/github-url-for-latest-release-of-the-download-file使用卷曲,ajax https://stackoverflow.com/questions/24987542/is-there-a-link-to-github-for-downloading-a-file-in-the-latest-release-of-a-repo or php https://stackoverflow.com/questions/21439239/github-latest-release.
我尝试了使用 ajax 的解决方案,它使用 github 发布 API:
<!DOCTYPE html>
<HTML> <BODY>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
GetLatestReleaseInfo();
});
function GetLatestReleaseInfo() {
$.getJSON("https://github.com/mongodb/mongo/releases").done(function (json) {
var release = json[0];
var asset = release.assets[0];
var downloadURL = "https://github.com/mongodb/mongo/releases" + release.tag_name + "/" + asset.name;
$(".mongodb-download").attr("href", downloadURL);
});
}
</script>
<a href="GetLatestReleaseInfo();">Link</a>
<a href="" onclick="location.href=this.href+downloadURL;return false;">Link2</a>
<a href="" onclick="location.href=this.href+mongodb-download;return false;">Link3</a>
</BODY>
</HTML>
但我无法正确调用 javascript 函数,正如我上面的尝试(Link、Link2 和 Link3)所示。我对 javascript 或 ajax 不太熟悉,所以我很感激任何帮助;也许有更简单的方法,无需 Ajax?
您正在加载 html 页面而不是其 REST API。获取标签的正确网址是https://api.github.com/repos/mongodb/mongo/tags https://api.github.com/repos/mongodb/mongo/tags
您可能想在这里阅读有关 github api 的更多信息 -https://developer.github.com/v3/repos/ https://developer.github.com/v3/repos/
你的 html 可能看起来像这样:
<!DOCTYPE html>
<HTML> <BODY>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
GetLatestReleaseInfo();
});
function GetLatestReleaseInfo() {
$.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (json) {
var release = json[0];
var downloadURL = release.zipball_url;
$("#mongodb-download").attr("href", downloadURL);
});
}
</script>
<a id='mongodb-download' href="">Download latest mongo</a>
</BODY>
</HTML>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)