PHP Ajax 跨域问题
来源:互联网 发布:windows虚拟机软件 编辑:程序博客网 时间:2024/05/18 22:53
当使用ajax提交数据进行post或get请求时,如果目标网页不在同一域名下则会出现跨域问题。需要在目的页设置权限:
本文通过设置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 跨域问题最佳解决方案
- Java对象的深复制和浅复制
- scrollview设置view的可见性导致布局错误
- oracle创建用户
- MySQL——InnoDB锁问题(六)
- Spark Yarn-cluster 与 Yarn-client
- PHP Ajax 跨域问题
- uva11722 Joining with Friend
- 基于dialog的可移动的自定义键盘
- 分词IKAnalyze
- intent详解(二)
- k-近邻算法(Python实现)
- 使用JAVA向微软消息队列(MSMQ)发送和接收消息
- 使用nginx+tomcat负载均衡
- Java 编程题自动评分技术的研究与实现(一)