浏览器/跨域
来自康健生活
< 浏览器
引言
跨域指浏览器禁止当前页面的所在的源去请求另一个源的数据。
| 协议跨域 | http://foo.baidu.com -> https://foo.baidu.com |
| 端口跨域 | http://foo.baidu.com:80 -> http://foo.baidu.com:3000 |
| 子域名跨域 | https://foo.baidu.com -> https://bar.baidu.com |
| 域名跨域 | https://baidu.com -> https://alibaba.com |
同源策略
npm install egg-cors --save
参考
CORS
跨域资源共享
允许浏览器向跨源的后端服务器发出ajax请求
参考
jsonp
跨域标签
Access-Control-Allow-Origin
如果前端发送的请求附带身份验证 withCredentials:true
Access-Control-Allow-Origin: * 时 cookie 不会出现在http的请求头里。换句话说就是如果前端请求如果携带 Cookie 信息,就不能设置 Access-Control-Allow-Origin: *
with-credentials
with-credentials 是用来设置是否支持发送cookie凭证信息的字段。如果 with-credentials:true 则不能设置 Access-Control-Allow-Origin : *
XSS
OPTIONS
OPTIONS 请求无法通过。
OPTIONS 请求是因为前端发送了“非简单请求”,比如 Content-Type: 'application/json',如果后端失误(或者根本不知道有OPTIONS这种请求),就有可能把OPTIONS挡住,导致无法通过。
在不修改后端的情况下解决这个问题的办法,就是调整 Content-Type: 'application/x-www-form-urlencoded',同时将传输的JSON进行一次序列化或者传参的时候将参数 JSON.stringify
error
- Access to font at 'http://192.168.20.121:4044/****' from origin 'http://192.168.1.109:4043' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

No categories