通过设置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)跨域访问,则只需在server.php文件头部添加如下代码:

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

// 指定允许其他域名访问

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

// 响应类型

//header('Access-Control-Allow-Methods:POST');

// 响应头设置

//header('Access-Control-Allow-Headers:x-requested-with,content-type');

2、允许多个域名访问

指定多个域名(http://client1.xxx.com、http://client2.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、允许所有域名访问

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

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

access-control-allow-origin php,PHP通过Access-Control-Allow-Origin 跨域相关推荐

  1. Blocked a frame with origin XXX from accessing a cross-origin 。iframe跨域问题

    在前端开发的过程中,我们常常会用到iframe去在我们的页面中引用一个子页面,而父子页面又常常会有交互.在同域情况下,子页面如果想要访问父页面中的window对象中的方法的话,直接在当前页面中使用wi ...

  2. 关于跨域 Response to preflight request doesn‘t pass access control check

    做项目的时候由于访问了不同的服务器,然后导致了跨域问题,报错情况为: has been blocked by CORS policy: Response to preflight request do ...

  3. 解决前端跨域的问题.Access to XMLHttpRequest at http://xxx.xxx from origin 'http://localhost:8000' has been bl

    1.前端浏览器报错如下: Access to XMLHttpRequest at http://xxx.xxx from origin 'http://localhost:8000' has been ...

  4. 【跨域报错解决方案】Access to XMLHttpRequest at ‘http://xxx.com/xxx‘ from origin ‘null‘ has been blocked by

    错误背景描述: 在使用ajax调用api接口的时候:发生错误如下 Access to XMLHttpRequest at 'http://xxxx.com/xxx' from origin 'null ...

  5. 解决浏览器跨域加载本地文件报错 Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy

    报错: Failed to load resource: net::ERR_FILE_NOT_FOUND Access to script at 'xxx' from origin 'null' ha ...

  6. Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin跨域问题

    出现问题:在本地运行,有配置vue.config.js中的proxy,所以解决了跨域问题.但将项目打包放到服务器上去,在打包的过程中,proxy是不会被打包进去的,也就是说代理所配置的跨域就已经失效了 ...

  7. Access to XMLHttpRequest at ‘file:///D:/xx/xxx.json‘ from origin ‘null‘ has been blocked by CORS问题解决

    问题: 在Chrome中的控制台输出错误的页面如下: 错误信息: Access to XMLHttpRequest at 'file:///D:/xxxx/xxx.json' from origin ...

  8. (跨域问题)Access to XMLHttpRequest at xxx from origin xxx

    问题:Access to XMLHttpRequest at 'http://localhost:8181/book/findAll' from origin 'http://localhost:80 ...

  9. 跨域问题:Access to XMLHttpRequest at ‘http://localhost:3001/re‘ from origin ‘http://127.0.0.1:8848‘

    1.问题描述: 在本地用express构建服务器端,前端访问数据. 报错:Access to XMLHttpRequest at 'http://localhost:3001/responseData ...

  10. ajax跨域请求问题:Access to XMLHttpRequest at……has been blocked by CORS policy: Cross origin requests

    背景:本地代码调试时,加载本地资源正常,jquery的AJAX请求服务端资源报错. Access to XMLHttpRequest at '***** ' from origin 'null' ha ...

最新文章

  1. 设计模式:设计模式七大原则之迪米特法则
  2. Java中使用SQLite数据库
  3. 【PAT乙级】1048 数字加密 (20 分)
  4. 路由器POS接入方式
  5. 实验6.2 定义一个基类BaseClass,观察构造函数和析构函数的执行情况。
  6. Learn OpenGL(三)——顶点着色器(Vertext Shader)
  7. duilib学习 --- 360demo 学习
  8. unity编译器安装路径_1.安装Unity
  9. Jruby On Rails 的安装及部署实践
  10. linux bridge 抓包,Linux bridge hairpin mode
  11. 微软bi报表服务器,升级 Power BI 报表服务器
  12. 亲测有效 debian出现does not have a Release file. N: Updating from such a repository can‘t be done securely
  13. 嵌入式知识-ARM裸机-学习笔记(2):利用GPIO来控制LED(附mkv210_image.c文件解析)
  14. python相关性分析模型,相关性分析原理及Python实战
  15. Java生成“年月日“+流水号
  16. 【每日最爱一句】2013.07.16
  17. win10查看电池损耗(win10查看电池损耗无法启动服务)
  18. sql server出生年月减去退休年月 计算年龄
  19. SQL Server 2008 R2 中英文 开发版/企业版/标准版 下载
  20. 怎样去掉超级链接的下划线和颜色.

热门文章

  1. EXCHANGE2O10用户设置外出助理失效
  2. [转]浅谈:国内软件公司为何无法做大做强
  3. Windows 7 几个小问题的解决方法(二)
  4. 第一次有人把“分布式事务”讲的这么简单明了
  5. Nginx+Supervisor安装部署.NET Core项目
  6. 与ISP合作需要了解哪些?
  7. node升级版本后vue报错
  8. 20155338 《网络攻防》 Exp7 网络欺诈防范
  9. 05构建之法阅读笔记之三
  10. properties文件不能输入中文