vue.js - Vue + axios:已被 CORS 策略阻止
问题描述
我有使用 axios 的 vue 应用程序,首先将 axios 设为默认值: import axios from 'axios'
const token ='xxxxxxxxx'
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
axios.defaults.baseURL = 'http://xxxxxxxxx/api/v1/'
然后在一个页面中我调用 axios :
<script>
export default {
data : function () {
return {
games : []
}
},
created: async function(){
await this.axios('games/this',{withCredentials: true,}).then((res)=> this.games=res.json)
}
}
</script>
我收到这个错误from origin 'http://localhost:3001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
我从本网站的帖子中尝试了许多解决方案,但都不起作用
解决方案
CORS 问题应该在后端解决。如果你使用 Lumen 作为后端,请确保你安装了 Laravel-Cors https://github.com/spatie/laravel-cors 然后在 config/cors.php 文件中设置 allowed_origins 为 *。
...
'allowed_origins' => ['*'],
...
推荐阅读
- sql - 如果给定位置 ID,是否可以检索部门数量和在该部门工作的员工数量
- javascript - 我的 console.log 在错误的地方,但我不明白为什么
- ansible - 如何使用 brew 在 mac os 上安装 ansible-galaxy?
- java - 使用 Java 将 Java Web 项目转换为 Angular
- parsing - idea 语法工具包 recoverWhile 对列表中的第一个元素进行解析
- go - “for true {}”发生“函数结束时缺少返回”
- javascript - ES6 迭代包含 const
- google-chrome - 如何从本机 Mac 应用程序(无扩展程序)读取 Firefox/Chrome 选项卡?
- excel - 包含多个数据透视表的 VBA Excel 文件大小很大
- css - rezise时如何将div的位置更改为另一个div