php ajax跨域问题
来源:互联网 发布:微信mac版 编辑:程序博客网 时间:2024/06/07 13:58
PHP Ajax 跨域问题最佳解决方案
分类 编程技术
本文通过设置Access-Control-Allow-Origin来实现跨域。
例如:客户端的域名是client.runoob.com,而请求的域名是server.runoob.com。
如果直接使用ajax访问,会有以下错误:
XMLHttpRequest cannot load http://server.runoob.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.
1、允许单个域名访问
指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:http://client.runoob.com');
2、允许多个域名访问
指定多个域名(http://client1.runoob.com、http://client2.runoob.com等)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; $allow_origin = array( 'http://client1.runoob.com', 'http://client2.runoob.com' ); if(in_array($origin, $allow_origin)){ header('Access-Control-Allow-Origin:'.$origin); }
3、允许所有域名访问
允许所有域名访问则只需在http://server.runoob.com/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:*');
0 0
- php ajax 跨域问题
- PHP Ajax 跨域问题
- php ajax跨域问题
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- PHP Ajax 跨域问题最佳解决方案
- (codeforces)C. Socks
- Linux开机启动流程详细步骤是什么
- 关于RecyclerViewHeader实现
- Android 热修复Tinker 在项目中的使用
- 对象序列化
- php ajax跨域问题
- eclipse的安装及tomcat的配置
- 796C
- Java实现分页数据获取CachedRowSet
- recyclerview历史
- 进程间通信方式总结——共享内存
- Django账号绑定邮箱时发送链接
- 解决为什么写博客,写什么样的博客的问题
- 如何准备BAT技术面试答案(上)——Java研发方向