1、浏览器同源策略
浏览器出于安全考虑,规定了同源策略,不同源的客户端脚本在没有授权的情况下,不能读写对方资源;所谓同源,即协议一致、端口一致、域名一致,如果任一条不一致,既可能造成跨域;
例:http://test01.com作为请求客户端
被请求服务端 | 结果 | 原因 |
http://test01.com/user/get | 请求成功 | |
http://test01.com/user/add/check | 请求成功 | |
http://test02.com/user/get | 请求失败 | 域名不一致 |
http://test01.com/user/get:82 | 请求失败 | 端口不一致 |
https://test01.com/user/get | 请求失败 | 协议不一致 |
2、解决方式之一
宝塔站点管理
add_header Access-Control-Allow-Origin * always;//*可替换为某域名
add_header Access-Control-Allow-Methods GET,POST,DELETE;
add_header Access-Control-Allow-Header Content-Type,*;