vue.js - Vue. js CORS issues, backend data blocked by CORS policy
问题描述
I am using Vuejs with CLI 3.6.3 and i am facing CORS issue so i can not get the backend api data.
The error message is :
Access to XMLHttpRequest at 'http://...'
from origin 'http://localhost:8080'
has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
I do create a vue.config.js file :
module.exports = {
devServer: {
proxy: 'https://my/site/myapi/myindex.php',
}
}
import axios and declare crossDomain equals true:
import axios from "axios";
axios.defaults.crossDomain = true;
And at the backend i do declare:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
How can i access the backend api data without facing CORS issues ?
解决方案
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: *');
这应该是 api 接受请求的地方
推荐阅读
- c# - 如何以编程方式(VBA/NetOffice)插入位图 Excel 对象?
- encoding - 鲁棒性在编码上下文中意味着什么?
- gremlin - ScriptEvalutionError:对于输入字符串“”:提供字符串作为 ID 时
- android - Firebase Recycler ValueEventListener 仅返回最后一个孩子
- angular - Angular8:新用户注册时的电子邮件验证
- azure - 处理 URL handler 中未处理的异常,不要写在 APP 洞察中
- ios - iOS ARM64 系统调用
- postgresql - 在 Postgresql 中归档旧数据
- c++ - Http C++ 客户端无法通过 Internet 连接到 C++ 服务器
- android - 将暂停和播放等控件添加到媒体播放器