springboot和vue通信解决跨域问题

springboot中添加跨域配置

@Configuration
public class Crosconfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}

修改配置

main.js中写好请求前缀

Vue.prototype.$http = axios
axios.defaults.baseURL = 'http://localhost:8081/'

axios发请求

methods: {
    getUserList () {
      this.$http.get('/user/1').then(response => {
        console.log(response)
      })
    }
  }
posted @ 2021-11-20 11:33  iiiiiiiivan  阅读(147)  评论(0)    收藏  举报