SpringMVC Resource interpreted as Script but transferred with MIME type application/octet-stream:

本文探讨了SpringMVC项目中JS文件加载时出现的MIME类型错误问题,通过调整mvc-servlet.xml文件中静态资源配置的位置解决了Chrome浏览器提示的问题。

Spring MVC 页面加载 js文件的时候,Chrome 浏览器的console提示

意思是说,本来浏览器请求的是Script文件,即js文件,但是服务器返回的是文本文件

 

 Resource interpreted as Script but transferred with MIME type application/octet-stream: 

 

具体截图

尽管不影响功能,但是有这个提示总感觉别扭。

 

然后还有一个规律,就是第一次访问的时候会提示,刷新页面也就是第二次访问这个提示就没有了。清除浏览器缓存访问则再次出现。

 

最后比对了一个没有这个提示的项目,发现这个和SpringMVC 中对静态文件设置的代码段的位置有关。

 

mvc-servlet.xml

 

开始是如下(注意,“处理静态资源”这个注释下代码段的位置)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:orm="http://www.springframework.org/schema/orm" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
						http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
						http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd
						http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
	">
	<!-- 处理静态资源 -->
	<!-- <mvc:resources mapping="/resources/**" location="/resources/"/> -->
	<mvc:resources mapping="/js/**" location="/js/"/>
	<!-- 配置注解驱动 -->
	<mvc:annotation-driven/>
	
	<!-- 定义默认访问 -->
	<mvc:view-controller path="/" view-name="forward:home"/>
	
	<!-- 视图解析器注册bean -jsp视图解析 http://tool.oschina.net/apidocs/apidoc?api=Spring-3.1.1-->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
   		<property name="contentType" value="text/html"/>      
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 扫描bean -->
	<context:component-scan base-package="com.bestcxx.mavenstu.mvc.controller"/>
</beans>


之后改为如下(注意,“处理静态资源”这个注释下代码段的位置)

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:orm="http://www.springframework.org/schema/orm" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
						http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
						http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd
						http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
	">
	
	<!-- 配置注解驱动 -->
	<mvc:annotation-driven/>
	
	<!-- 定义默认访问 -->
	<mvc:view-controller path="/" view-name="forward:home"/>
	
	<!-- 视图解析器注册bean -jsp视图解析 http://tool.oschina.net/apidocs/apidoc?api=Spring-3.1.1-->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
   		<property name="contentType" value="text/html"/>      
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 扫描bean -->
	<context:component-scan base-package="com.bestcxx.mavenstu.mvc.controller"/>
	<!-- 处理静态资源 -->
	<!-- <mvc:resources mapping="/resources/**" location="/resources/"/> -->
	<mvc:resources mapping="/js/**" location="/js/"/>
</beans>


就是,静态js文件的声明放到的底部浏览器就可以正常访问了,具体原因还是不得而知,但是这说明配置文件中是有先后顺序之分的。

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值