简体   繁体   中英

AngularJs doesn't load JSON file from local “Maven Project”

I'm building Web application using AngularJs, trying to use angular-translate for my app. Here is Doc: http://www.ng-newsletter.com/posts/angular-translate.html

$translateProvider.useStaticFilesLoader({
  prefix: 'assets/resources/',
  suffix: '.json'
});

I have 2 json file in my project directory

webapp/assets/resources/locale-en_US.json
webapp/assets/resources/locale-ru_RU.json

When I Run my app in browser, I'm getting this error

Failed to load resource: the server responded with a status of 404 (Not Found) 
http://localhost:8080/assets/resources/locale-ru_RU.json

I tried to load .js file and also .jpg from the same directory where is located my json files

webapp/assets/resources/foo.jpg

everything works fine, I cant load only JSON files from local. I Tried to make $http request but no result.

$http.get('assets/resources/locale-en_US.json' ).success(function(data) {
   console.log(data);
});

Looks like my project ignores json files from local. I made researchs and fount some suggestions to add mapping to web.xml . No Result

<mime-mapping>
  <extension>json</extension>
  <mime-type>application/json</mime-type>
</mime-mapping>

Any Idea what to Do?

Regards, Gari.E

I had servlet-mapping in my web.xml

<servlet-mapping>
    <servlet-name>shemoGeServlet</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>

which was searching jsno files in WEB-INF directory, but my jsno files were in assets directory. Here is my servlet-config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven/>

    <mvc:resources location="/assets" mapping="/assets/**"/>

    <context:component-scan base-package="ge.shemo"/>

     <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="contentNegotiationManager">
            <bean class="org.springframework.web.accept.ContentNegotiationManager">
                <constructor-arg>
                    <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                        <constructor-arg>
                            <map>
                                <entry key="json" value="application/json"/>
                                <entry key="xml" value="application/xml"/>
                            </map>
                        </constructor-arg>
                    </bean>
                </constructor-arg>
            </bean>
        </property>


        <property name="defaultViews">
            <list>
                <!-- JSON View -->
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <constructor-arg>
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                            <property name="autodetectAnnotations" value="true" />
                        </bean>
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages"></bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/>

</beans>

That's why I was getting

Failed to load resource: the server responded with a status of 404 (Not Found) 

I removed servlet and the problem gone

<servlet-mapping>
    <servlet-name>shemoGeServlet</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM