Javascript 在textarea中使用公共GitHub的内容
我有一个html页面,用户应该在其中以输入形式输入公共GitHub的url(下面的id“url”);然后,我想在页面的文本区域(下面的id“program”)中显示url指向的页面中的代码 该代码段生成错误“跨源请求仅支持HTTP”。我理解这是出于安全原因。有人能建议正确的方法吗 干杯/JCJavascript 在textarea中使用公共GitHub的内容,javascript,github-api,Javascript,Github Api,我有一个html页面,用户应该在其中以输入形式输入公共GitHub的url(下面的id“url”);然后,我想在页面的文本区域(下面的id“program”)中显示url指向的页面中的代码 该代码段生成错误“跨源请求仅支持HTTP”。我理解这是出于安全原因。有人能建议正确的方法吗 干杯/JC function urlButtonClicked(){ var xhttp = new XMLHttpRequest(); var theUrl; xhttp.onreadyst
function urlButtonClicked(){
var xhttp = new XMLHttpRequest();
var theUrl;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
theUrl = document.getElementById("url").value;
document.getElementById("program").value = xhttp.responseText;
}
};
xhttp.open("GET", theUrl, true);
xhttp.send();
}
编辑
没关系。我将上面的代码更改为:
function urlButtonClicked(){
var theUrl = document.getElementById("url").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("program").value = xhttp.responseText;
}
};
xhttp.open("GET", theUrl, true);
xhttp.send();
}
是的,这是可能的。将此记录在注释中:
fetch('https://api.github.com/repos/webdev23/source_control_sentry/readme')
.then(v=>v.json()).then((函数(v){
//控制台日志(v)
out.innerHTML=atob(v['content'])
})
)