ajax 允许跨域访问最全的说明

admin 2018-4-13 487

分以下步骤进行排查

1,从程序方面

以php代码为例:


header('content-type:application:json;charset=utf8');  
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';  
$allow_origin = array(  
    'http://xiuno.laoshiji.cc',  
);  
if(in_array($origin, $allow_origin)){  
    header('Access-Control-Allow-Origin:'.$origin);  
    header('Access-Control-Allow-Methods:GET');  
    header('Access-Control-Allow-Headers:x-requested-with,content-type');  
}


2,服务器配置方面

如果是nginx


server {
  location  / {
      ……		
      add_header 'Access-Control-Allow-Origin' 'http://xiuno.laoshiji.cc';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  }	
}


重启nginx


如果是apache

首先编辑httpd.conf

找到这行

#LoadModule headers_module modules/mod_headers.so

把#注释符去掉

LoadModule headers_module modules/mod_headers.so

开启apache头信息自定义模块

然后在虚拟主机

<VirtualHost>
...
Header add Access-Control-Allow-Origin http://xiuno.laoshiji.cc
Header add Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header add Access-Control-Allow-Headers "Content-Type"
</VirtualHost>


重启apache


最新回复 (0)
全部楼主
返回
发新帖
老师机论坛