我正在使用JavaScript编写一个小程序。基本上,我想使用Promise和fetch从两个文本文件中提取文本。但是,我不知道如何从文件中获取实际的文本。这是我当前的代码。
sample.txt
this is
a sample
text file.
Run Code Online (Sandbox Code Playgroud)
sample2.txt
this is
the second
sample file.
Run Code Online (Sandbox Code Playgroud)
index.js
function getSampleText() {
Promise.all([
fetch('sample.txt'),
fetch('sample2.txt')
]).then(allResp => {
let sampleResp = allResp[0];
let sample2Resp = allResp[1];
console.log(sampleResp);
console.log(sample2Resp);
})
}
Run Code Online (Sandbox Code Playgroud)
这是应许...我该如何从中得到建议?
我正在尝试从我的 GitHub 存储库中读取一些源代码(C 语言)以在我的网页中显示为文本。我可以通过https://raw.github.com以原始模式访问代码。
我正在使用 jQuery GET 函数来读取数据,但它不起作用。问题与 XMLHttpRequest 和 Access-Control-Allow-Origin 有关,但是,尽管我在 stackoverflow 上发现了一些相关问题(XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin),但它对我不起作用。我尝试了一切。
我的jQuery代码:
<script type="text/javascript">
$(document).ready(function() {
var url = 'https://raw.github.com/raysan5/raylib/master/examples/ex01_basic_window.c';
$.get(url, function(data) {
$('#code').text(data);
}, 'text');
});
</script>
Run Code Online (Sandbox Code Playgroud)
请问有人可以帮我解决这个问题吗?非常感谢!