寻找一个关于如何在浏览器中读取YAML文件的连贯示例.位于web服务器上的YAML文件



我的设置:VS代码+WSL2。文件都在同一个文件夹中(js-ayml.js、YAML文件和index.html(。我通过刷新引用它的页面来运行javascript代码。我使用GoLiveVS代码扩展名作为服务器

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Test js-yaml</title>
<script src="//code.jquery.com/jquery-2.1.0.js">
</script><script src='js-yaml.js'>
</script><script src='readYaml.js'>

</head>
<body>
<h1>Test js-yaml</h1>
<p><a href="https://github.com/nodeca/js-yaml">https://github.com/nodeca/js-yaml</a></p>
</body>
</html>

readYaml.js

(function () {
"use strict";
$(document).ready(function () {
$.get('/common/resources/LessonContentsLv01Ln01.yml')
.done(function (data) {
console.log('File load complete');
console.log(jsyaml.load(data));
var jsonString = JSON.stringify(data);
console.log(jsonString);
console.log($.parseJSON(jsonString));
});
});
}());

我已经发布了这个https://stackoverflow.com/questions/70916217/reading-yaml-from-javascript-in-a-browser?noredirect=1#comment125366890_70916217它被关闭了
我尝试过:
在Javascript中读取YAML文件(解决方案2(
以及此处显示的示例
https://github.com/shockey/js-yaml-browser这说明这个分叉是为浏览器优化的

它们都以相同的错误失败

js-yaml.js:9 Uncaught ReferenceError: require is not defined
at js-yaml.js:9:16

那条线是var fs = require('fs');据我所知,fs是一个node.js模块,它在浏览器中不起作用

我的问题是是否有一个JavaScript模块可以从web服务器打开和读取YAML文件,这个模块在浏览器中工作,没有任何后端

注意:我是一个初学者,我认为这将是一个基本的例子,加载一个文件并解析它

shockey/js-yaml-browser已损坏,并且已经好几年没有更新了。js-yaml通常有很多分叉。Manx7/js-yaml是有效的,并且是最新的。

.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.js"></script>
<script>
// Normally we'd use fetch to get the file. However, stackoverflow 
// snippets don't have a way to add another file, so we'll use a 
// string instead just to show how the library works.
/*
fetch("sample.yaml")
.then(response => response.text())
.then(text => {
// once the file has been loaded, we can parse it into an object.
const yaml = jsyaml.load(text);
console.log(yaml);
});
*/
const text = `
# Employee records
- martin:
name: Martin D'vloper
job: Developer
skills:
- python
- perl
- pascal
- tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- lisp
- fortran
- erlang
`;
const yaml = jsyaml.load(text);
console.log(yaml);
</script>

最新更新