从React发送请求到Spring mvc时出现跨域错误

问题描述 投票:0回答:1

我正在使用 Spring mvc 和 React Js,但无法解决 Cors 的问题。当我发布或想要从 spring mvc 控制器获取数据时,它会给出错误 Cross origion not allowed。我已在 applicationcontext.xml 文件中添加了这些行但未能构建并运行应用程序

<bean id="corsFilter" class="org.springframework.web.filter.CorsFilter">
        <constructor-arg>
            <bean class="org.springframework.web.cors.CorsConfiguration">
                <property name="allowedOrigins" value="http://localhost:8081" />
                <property name="allowedMethods" value="GET,POST,PUT,DELETE,OPTIONS" />
                <property name="allowedHeaders" value="*" />
                <property name="allowCredentials" value="true" />
            </bean>
        </constructor-arg>
    </bean>

    <!-- Register CorsFilter bean with the servlet context -->
    <bean class="org.springframework.web.servlet.DispatcherServlet" name="dispatcherServlet">
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </bean>

我想得到所有请求的结果都已成功处理,我从 React 发送到 Spring MVC 控制器

java spring-mvc spring-data-jpa
1个回答
0
投票

我认为属性“allowedOrigins”值应该有反应网址,类似

<property name="allowedOrigins" value="http://localhost:3000" />
© www.soinside.com 2019 - 2024. All rights reserved.