同源策略和跨域資源共享(CROS)


同源策略

Same-origin policy - Web security | MDN

跨站資源共享

Cross-Origin Resource Sharing (CORS) - HTTP | MDN

這四種資源受 CORS 機制影響:

  1. Invocations of the XMLHttpRequest or Fetch APIs in a cross-site manner, as discussed above.
  2. 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.
  3. WebGL textures.
  4. Images/video frames drawn to a canvas using drawImage().

進行跨域請求

一般有這三種方法:

  1. 使用代理
  2. 響應頭設置Access-Control-Allow-Origin
  3. 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM