ithelloworld
V2EX  ›  问与答

如何解决 Chrome,Opera 浏览器的 Refused to execute script from 问题?

  •  
  •   ithelloworld · Aug 29, 2014 · 5760 views
    This topic created in 4337 days ago, the information mentioned may be changed or developed.
    如题,程序在Chrome,Opera中执行不了JavaScript,出现错误:

    Refused to execute script from 'http://example.com/info?no=31&magic=1184' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

    在Firefox,IE,Safari中没问题。

    查了一下,貌似是 http header 的 X-XSS-Protection 设置问题。改了一下代码:

    def info
    response.headers['X-XSS-Protection'] = '0'
    # Other logic
    end

    再次运行,Chrome中的header是:

    X-XSS-Protection:1; mode=block
    X-XSS-Protection:0

    Firefox中的header是:

    X-XSS-Protection 0, 1; mode=block

    X-XSS-Protection:1; mode=block 是Nginx中的设置,即使拿掉,在Chrome中还是不能正常执行脚本代码,什么原因呢?
    2 replies    2014-08-29 17:56:51 +08:00
    hicdn
        1
    hicdn  
       Aug 29, 2014
    错误信息很清楚了,MIME type 错了,你设置的 MIME type 是 text/html,需要改成 application/x-javascript
    ithelloworld
        2
    ithelloworld  
    OP
       Aug 29, 2014
    @hicdn 谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   989 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:56 · PVG 03:56 · LAX 12:56 · JFK 15:56
    ♥ Do have faith in what you're doing.