您的位置 首页 php

apache如何解决跨域资源访问

理解跨域首先必须要了解同源策略。同源策略是浏览器上为安全性考虑实施的非常重要的安全策略。 何谓同源: URL由协议、域名、端口和路径组成,如果两个URL的协议、域名和端口相同,则表示他们同源。 同源策略:浏览器的同源策略,限制了来自不同源的” document “或脚本,对当前”document”读取或设置某些属性。

其实很简单,只要在静态资源服务器上,增加一个头信息:

Access-Control-Allow-Origin *

本文就 apache 进行操作, nginx 大同小异

首先编辑httpd.conf

找到这行

#LoadModule headers_module modules/mod_headers.so

把# 注释符 去掉

LoadModule headers_module modules/mod_headers.so

目的是开启apache头信息自定义模块

然后在独立资源域名的虚拟主机添加一行

Header set Access-Control-Allow-Origin *

意思是对这个域名的资源进行访问时,添加一个头信息

重启apache

再访问,OK!

<VirtualHost *:80>

DocumentRoot “D:/htdocs/erp/public”

ServerName erp.test. com

ServerAlias

AddHandler fcgid-script .php

Header set Access-Control-Allow-Origin *

<Directory “D:/htdocs/erp/public”>

Options FollowSymLinks ExecCGI

AllowOverride All

Order allow,deny

Allow from all

Require all granted

DirectoryIndex index.php index.html error/index.html

</Directory>

</VirtualHost>

文章来源:智云一二三科技

文章标题:apache如何解决跨域资源访问

文章地址:https://www.zhihuclub.com/152254.shtml

关于作者: 智云科技

热门文章

网站地图