Openlayers和NPM:无法加载本地.json文件

我尝试从本地URI在Openlayers中加载.json文件。按照建议,我正在使用npm。但是,该文件似乎没有加载。文件example.json存储在目录data中,该目录是main.js所在的子目录。根据jsonLINT,我正在使用的json文件有效。

var vectorLayer = new VectorLayer({
  source: new VectorSource({
    url: 'data/example.json',format: new GeoJSON()
  }),style: function(feature) {
    style.getText().setText(feature.get('name'));
    return style;
  }
});

var map = new Map({
  target: 'map',layers: [ vectorLayer],// ...
});

控制台中的消息:

XHR GEThttp://localhost:1234/data/example.json [HTTP/1.1 200 OK 4ms]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
XHR GEThttp://localhost:1234/data/example.json [HTTP/1.1 304 Not Modified 14ms]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

我想,文件根本不会加载。如果将地址更改为不在目录中的某个文件,则会收到相同的消息。重新加载后,我会两次收到304错误代码。如果我输入“ localhost:1234 / data / example.json”作为URL,它将检索我的主页(与localhost:1234一样),而不是我希望看到的json文件。

我是npm的初学者,所以我认为问题在于使用npm处理文件的方式。

zhenhao1986 回答:Openlayers和NPM:无法加载本地.json文件

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166360.html

大家都在问