如何从localhost url获取respose完全是http:// localhost:8888 /

时间:2017-07-19 07:12:51

标签: ajax spring-mvc spring-boot

我有两个项目在其中设置Springboot,现在,我将在eclipse上运行这两个springboot,我为projectB设置了端口8888。 enter image description here 这是我的带有RequestMapping的projectB控制器。

@RequestMapping(value = "test", method = RequestMethod.GET)
public @ResponseBody String test() {
    return "testtesttest";
}

我需要从jquery方法调用url,url是从projectB中的控制器读取respose。

我该怎么称呼这个网址?  我试过" http://localhost:8888/test"和" http://127.0.0.1:8888/test"  但我没有从回应中得到任何结论。

错误显示为: enter image description here

请给我解决方案

非常感谢。

1 个答案:

答案 0 :(得分:0)

在应用程序的主类中添加以下代码。这会将您的web mvc cors映射配置为允许来自“*”

的请求
   @SpringBootApplication
    public class Application {

    @Configuration
        public class MyConfiguration {
            @Bean
            public WebMvcConfigurer corsConfigurer() {
                return new WebMvcConfigurerAdapter() {
                    @Override
                    public void addCorsMappings(CorsRegistry registry) {
                        logger.info("Added CORS config");
                        registry.addMapping("/**").allowedOrigins("*").maxAge(3600);
                    }
                };
            }
        }
}
相关问题