V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ithelloworld
V2EX  ›  问与答

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

  •  
  •   ithelloworld · 2014-08-29 13:31:43 +08:00 · 4847 次点击
    这是一个创建于 3520 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题,程序在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 条回复    2014-08-29 17:56:51 +08:00
    hicdn
        1
    hicdn  
       2014-08-29 15:15:37 +08:00
    错误信息很清楚了,MIME type 错了,你设置的 MIME type 是 text/html,需要改成 application/x-javascript
    ithelloworld
        2
    ithelloworld  
    OP
       2014-08-29 17:56:51 +08:00
    @hicdn 谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3669 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:38 · PVG 18:38 · LAX 03:38 · JFK 06:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.