解决vue php跨域问题 -凯发k8官方网
凯发k8官方网
收集整理的这篇文章主要介绍了
解决vue php跨域问题
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
vue php跨域问题
同源策略(cors策略)
为了保证用户的信息安全产生了请求时的同源策略,所谓同源是指,域名,协议,端口相同。它是浏览器的一种最核心最基本的安全策略。它对来至不同源的文档或这脚本对当前文档的读写操作做了限制。
访问同源的资源是被浏览器允许的,但是如果访问不同源的资源,浏览器默认是不允许的。访问不同源的资源那就是我们所说的跨域
入口文件(index.php)中添加以下header请求相关设置(技术框架 vue yii2.0)
header('access-control-allow-origin: *');//允许异步请求,可以把 * 设置为固定域名 header("access-control-allow-methods: head, get, post, put, patch, delete, options");//请求方式 header("access-control-allow-headers: x-api-key, origin, x-requested-with, content-type, accept, access-control-request-method,access-control-request-headers, authorization");//允许接收的header头参数 例如:需要传参userid,则需要这里配置一下 header('content-type: application/json'); $method = $_server['request_method']; if ($method == "options") {header('access-control-allow-origin: *');//与上面的同理header("access-control-allow-headers: x-api-key, origin, x-requested-with, content-type, accept, access-control-request-method,access-control-request-headers, authorization");//与上面的同理header("http/1.1 200 ok");die(); }总结
以上是凯发k8官方网为你收集整理的解决vue php跨域问题的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 软件测试基础篇
- 下一篇: php 快速解决跨域问题