前端:

需要在JS中加入
当然下面的URL需要修改,

  created() {
    const _this = this;
    axios.get('http://localhost:8181/book/findAll/').then(function (resp){
      _this.books = resp.data;
      console.log("我是回调"+resp.data);
    })
  }

后端:

创建CorsConfig类并添加一下代码

package com.sadhu.springbootTest.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CrossConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
        registry.addMapping("/**")
//                .allowedOrigins( "*")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD" ,"POST","PUT" , "DELETE")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");

    }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐