Apache如何解决跨域资源访问

Apache如何解决跨域资源访问_第1张图片

  1. 找到配置文件 httpd.conf
    #LoadModule headers_module modules/mod_headers.so
    去掉#注释(开启apache头信息自定义模块)
  2. 配置文件 httpd.conf
    大概295行,改为 Require all granted

    Require all granted
  3. 如果项目里有.htaccess文件
    放入这段代码

    Header set Access-Control-Allow-Origin: "*"
    Header set Access-Control-Allow-Methods: "GET,POST,PUT,DELETE,OPTIONS"
    Header set Access-Control-Allow-Headers: "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"

或者配置域名里加 Header set Access-Control-Allow-Origin “*”


   DocumentRoot /var/www/host.example.com
   ServerName host.example.com
   JkMount /webapp/* jkworker
   Header set Access-Control-Allow-Origin "*"
   RewriteEngine on
   RewriteRule   ^/otherhost  http://otherhost.example.com/webapp [R,L]

或者在httpd.conf最下面添加配置(全局配置):


    Header set Access-Control-Allow-Origin: "*"
    Header set Access-Control-Allow-Methods: "GET,POST,PUT,DELETE,OPTIONS"
    Header set Access-Control-Allow-Headers: "Content-Type"

  1. 重启apache,强刷页面,搞定!

你可能感兴趣的:(PHP)