Axios 访问本地文件出现错误(跨域问题)
本地文件:
{
"name": "秦始皇",
"url": "https://www.baidu.com",
"page": 1,
"isNonProfit": true,
"address": {
"street": "苏雅路",
"city": "江苏苏州",
"country": "中国"
},
"links": [
{
"name": "bilibili",
"url": "https://space.bilibili.com"
},
{
"name": "大江",
"url": "https://www.baidu.com"
},
{
"name": "淘宝",
"url": "https://www.taobao.com"
}
]
}
HTML文件:
<!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">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.js"></script>
<script src="../node_modules/axios/dist/axios.js"></script>
</head>
<body>
<div id="app">
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#app',
mounted() { //钩子函数 ===> 可以插到中间去 ! ! !
//————链式编程
axios.get("data.json").then(response=>( console.log(response.data) ));
}
});
</script>
</body>
</html>
option + B 打开浏览器查看:
解决方案:
#在VSCode中下载一个插件,Live Server , 如下图所示:
//然后再次打开这个html文件的时候用下图所示的方式打开 , 即可:
成功解决:
再次打开浏览器 , 发现成功解决了之前的问题: