vue.js - 将 JSON 文件导入被 CORS 策略阻止的 Vue 项目
问题描述
在我开始构建的使用 CDN 中的 Vue 的 Vue 应用程序中,我无法通过控制台错误:
CORS 策略已阻止从源“null”访问“file:///C:/files/development/vue/reading_schedule/json/books.json”处的脚本:跨源请求仅支持协议方案:http , 数据, chrome, chrome-extension, chrome-untrusted, https.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<div>{{ title }}</div>
</div>
<script type="module">
import * as books from './json/books.json';
new Vue ({
el: "#app",
data() {
return {
title: 'Reading Schedule'
}
},
created() {
document.title = this.title;
// console.log(books);
},
methods: {
}
});
</script>
</body>
</html>
到目前为止,我的 Vue 应用程序开发经验一直是在 Vue CLI 中创建应用程序并将 JSON 导入到这些应用程序中,并且没有 CORS 策略控制台错误。
是否有可能让上面的代码以某种方式工作?
解决方案
您可以使用插件进行开发:
https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino
但建议您配置一个实际发送 cors 标头的 api 端点。看:
https://medium.com/js-dojo/how-to-deal-with-cors-error-on-vue-cli-3-d78c024ce8d3
推荐阅读
- c++ - 通用 `Is_enabled` SFINAE 结构
- java - 即使测试成功,数据也不会添加到表中
- r - Stringr 函数或 gsub() 查找 x 位字符串并提取前 x 位?
- mysql - 尝试通过 lambda 在 AWS-RDS 中通过 VPC 访问 mysql 的问题
- ios - NavigationBar 上的常用 BarButtonItems
- r - 优化在 tibble/data.frame 中查找范围的性能
- php - 解析POST发送的JSON数据时PHP中的奇怪对象输出
- javascript - 是 AWS-SDK 的 HTML 表单吗?
- c# - 如何在添加之前检查对象列表中是否已经存在具有相同 ID 的对象?
- python - 在 OpenGL 和 Pygame 中无法旋转 3D 立方体