脚本被解释为样式表,但使用MIME类型text / html进行传输

时间:2014-03-06 10:07:45

标签: javascript php html .htaccess mime-types

我在Chrome中收到两个控制台警告:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://domain/". domain/:11
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://domain/". domain/:11

在第11行,我有:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 

这是头部的所有代码:

<head>
<meta charset="UTF-8">
<title>Laoautod</title>

<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/base.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
<script type="text/javascript" src="js/center.js"></script>
<script type="text/javascript">
    function displayResult()
                    {
        var x=document.getElementById("checkbox").defaultChecked;
        alert(x);
                    }
</script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'accountID']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

HTML代码在index.php文件中,所以我在页面脚本的开头添加了header('Content-type: text/css');。我还在AddCharset utf-8 .css .js文件中添加了AddType text/css .css.htaccess,但没有运气丢失警告。

究竟是什么造成了这种情况,你如何摆脱警告?

2 个答案:

答案 0 :(得分:1)

好的,我发现了问题。

我收到警告的原因是因为我在base.css中使用@import来引入不在其位置的脚本。确切地说,我错过了两个脚本,所以大概这就是我收到2个警告的原因。

答案 1 :(得分:1)

值得注意的是;

  

基本上,每个请求都可能导致问题,包括   那些静态内容正在通过验证。

在我的情况下,我的应用程序是 Spring Boot Application 。所以我只是在我的安全配置中添加了排除项,以获取相关文件的路径......

注意 - 此解决方案基于SpringBoot ...您可能需要做的事情可能因您使用的编程语言和/或您正在使用的框架而有所不同

因此,让我们说一些导致错误的静态内容路径如下:

名为“plugins”的路径

  

http://localhost:8080/plugins/styles/css/file-1.css

     

http://localhost:8080/plugins/styles/css/file-2.css

     

http://localhost:8080/plugins/js/script-file.js

以及名为“pages”的路径

  

http://localhost:8080/pages/styles/css/style-1.css

     

http://localhost:8080/pages/styles/css/style-2.css

     

http://localhost:8080/pages/js/scripts.js

然后我只需在Spring Boot Security Config中添加以下排除项;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(<comma separated list of other permitted paths>, "/plugins/**", "/pages/**").permitAll()
            // other antMatchers can follow here
    }

}

<小时/> 从身份验证中排除这些路径"/plugins/**""/pages/**"会导致错误消失。

干杯!