跨域:协议,域名,端口不一样就是跨域。
比如:你的服务器域名是www.test.com,而去请求的域名是www.demo.com,就是跨域。
注意127.0.0.1与localhost也是两个不同域名。
如果跨域,直接使用ajax访问,会有下面报错:
XMLHttpRequest cannot load http://localhost/xdl/lamp/a.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access.
解决方法
允许单个与单个域名访问,例如
指定某域名(http://127.0.0.1)跨域访问,只需要在http://localhost/study/lamp/a.php
文件头添加如下代码:
header('Access-Control-Allow-Origin:http://127.0.0.1');
允许所有域名访问
只需要在对应php文件中,加上:
header('Access-Control-Allow-Origin:*');
比如:你的服务器域名是www.test.com,而去请求的域名是www.demo.com,就是跨域。
注意127.0.0.1与localhost也是两个不同域名。
如果跨域,直接使用ajax访问,会有下面报错:
XMLHttpRequest cannot load http://localhost/xdl/lamp/a.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access.
解决方法
允许单个与单个域名访问,例如
指定某域名(http://127.0.0.1)跨域访问,只需要在http://localhost/study/lamp/a.php
文件头添加如下代码:
header('Access-Control-Allow-Origin:http://127.0.0.1');
允许所有域名访问
只需要在对应php文件中,加上:
header('Access-Control-Allow-Origin:*');