惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - dnddn

axios输出图片显示 ElementUI时间选择控件提交的时间为UTC时间 pypi清华镜像 JS引用传值问题 解决mac下mysql libssl.1.0.0.dylib报错问题 Google App Engine初探 SQL2005的阀值设定 移动硬盘识别不了的问题 网球术语英汉对照 【丰富词汇量,喜欢网球的路过说声。。】 几个小问题 - dnddn - 博客园 SilverLight,有多少人关心呢? be my friend MSSQL中的随机函数 检讨 简单的就是最好的 这个五一 紧张非封闭式开发中 静静的看twittervision SQL Server 2005 数据类型
通过Access-Control-Allow-Origin解决跨域问题
dnddn · 2020-09-17 · via 博客园 - dnddn

本文通过设置Access-Control-Allow-Origin来实现跨域。

例如:客户端的域名是client.xxx.com,而请求的域名是server.xxx.com。

如果直接使用ajax访问,会有以下错误:

XMLHttpRequest cannot load http://server.xxx.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.xxx.com' is therefore not allowed access.

1、允许单个域名访问
指定某域名( http://client.xxx.com )跨域访问,则只需在http://server.xxx.com/server.php文件头部添加如下代码:

header('Access-Control-Allow-Origin:http://client.xxx.com');

2、允许多个域名访问
指定多个域名( http://client1.xxx.comhttp://client2.xxx.com等)跨域访问,则只需在http://server.xxx.com/server.php文件头部添加如下代码:

$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';  
  
$allow_origin = array(  
    'http://client1.xxx.com',  
    'http://client2.xxx.com'  
);  
  
if(in_array($origin, $allow_origin)){  
    header('Access-Control-Allow-Origin:'.$origin);       
} 

3、允许所有域名访问
允许所有域名访问则只需在http://server.xxx.com/server.php文件头部添加如下代码:

header('Access-Control-Allow-Origin:*');