源(Origin)
Web 内容的源由用于访问它的 URL 的方案(协议)、主机名(域名)和端口定义。只有当协议、主机和端口都匹配时,两个对象才具有相同的源。
某些操作仅限于同源内容,但可以使用 CORS 解除这个限制。
示例
由于它们具有相同的协议(http)和主机名(example.com),以下两个地址是同源的。注意不同的文件路径与是否同源无关:
http://example.com/app1/index.htmlhttp://example.com/app2/index.html
服务器默认从 80 端口传送 HTTP 内容,所以以下两个地址同源:
http://Example.com:80http://example.com
由于协议不同,以下两个地址不属于同源:
http://example.com/app1https://example.com/app2
由于主机名不同,以下几个地址不属于同源:
http://example.comhttp://www.example.comhttp://myapp.example.com
由于端口不同,以下几个地址不属于同源:
http://example.comhttp://example.com:8080