明明是 HTTPS,怎么就 Mixed Content 了


頁面通過https://your.domain.com/path訪問,其中頁面資源使用自適應協議加載//your.domain.com/assets/xxx,但瀏覽器卻未加載資源,報錯為Mixed Content: xxx

排查發現,頁面中存在個base標簽,來看下base標簽的說明:

The base element allows authors to specify the document base URL for the purposes of resolving relative URLs, and the name of the default browsing context for the purposes of following hyperlinks.

可以點擊resolving relative URLs看看 HTML5 規定解析 URL 的步驟,其中可以看到,若存在base標簽,則解析相對路徑的時候是會用到base標簽中設置的 URL 的信息。

自適應協議路徑是被當做相對路徑解析,解析的時候會引用base標簽中設置的 URL 的scheme,而這個scheme在這里是 HTTP,所以通過 HTTPS 訪問的時候就會出現Mixed Content錯誤。

知道了原因,解決就很簡單了,去掉base標簽或是寫完整路徑https://your.domain.com/assets/xxx,建議都用 HTTPS,大勢所趨。

附:
HTML5 Resolving URLs

  1. Let url be the URL being resolved.

  2. Let encoding be determined as follows:
    If the URL had a character encoding defined when the URL was created or defined or when this algorithm was invoked
    The URL character encoding is as defined.
    If the URL came from a script (e.g. as an argument to a method)
    The URL character encoding is the API URL character encoding specified by the script's settings object.
    If the URL came from a DOM node (e.g. from an element)
    The node has a Document, and the URL character encoding is the document's character encoding.

  3. If encoding is a UTF-16 encoding, then change the value of encoding to UTF-8.

  4. If the algorithm was invoked with an absolute URL to use as the base URL, let base be that absolute URL.
    Otherwise, let base be the element's base URL.

  5. Apply the URL parser to url, with base as the base URL, with encoding as the encoding.

  6. If this returns failure, then abort these steps with an error.

  7. Let parsed URL be the result of the URL parser.

  8. Let serialized URL be the result of apply the URL serializer to parsed URL.

  9. Return serialized URL as the resulting absolute URL and parsed URL as the resulting parsed URL.


作者:Buff
出處:https://buff.cnblogs.com
本文以學習、研究和分享為主,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。


免責聲明!

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



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