同源策略
Same-origin policy - Web security | MDN
跨站資源共享
Cross-Origin Resource Sharing (CORS) - HTTP | MDN
這四種資源受 CORS 機制影響:
- Invocations of the XMLHttpRequest or Fetch APIs in a cross-site manner, as discussed above.
- Web Fonts (for cross-domain font usage in @font-face within CSS), so that servers can deploy TrueType fonts that can only be cross-site loaded and used by web sites that are permitted to do so.
- WebGL textures.
- Images/video frames drawn to a canvas using drawImage().
進行跨域請求
一般有這三種方法:
- 使用代理
- 響應頭設置
Access-Control-Allow-Origin
- JSONP:只支持 GET,用 script 標簽的特性請求一個地址執行返回的代碼
Firefox 使用 pdf 時遇到的有關同源策略的坑
同源問題真是時不時就得坑一把啊,剛剛 iframe 的 src 填了一個相對路徑的 pdf 文件結果發現無法訪問其 document 對象,報錯SecurityError: Permission denied to access property "document" on cross-origin object
(用的相對路徑居然報跨域的錯。。),進一步測試發現 Chrome 沒有問題,應該是 FF 的 bug 或者 feature 吧。。
剛剛查了下這個是 14 年就有人提出的問題,問題在於 pdf.js 修改了 domain,但一直沒有修復Unable to Print PDF when loaded in iFrame · Issue #5397 · mozilla/pdf.js