vue+springboot get请求能跨域,post无法跨域。

新手上路,请多包涵

如题
编程萌新,求懂行的朋友指点,万分感谢。

跨域在后端配置如下:
image.png

执行post请求后
后端报错:
image.png

前端状态码:
image.png

image.png
image.png
image.png

后端controller代码
image.png

之前在后端单独用dao的接口测试过没问题,但一到跨域,前端数据就是传不进去,求大佬指点迷津,万分感谢。

阅读 5.7k
4 个回答
✓ 已被采纳新手上路,请多包涵

问题解决:
后台实体类代码出错。。

这就不是跨域的问题,跨域就不会请求接口了,看错误信息,是没有解析json的httpmessageconverter,CrosConfig这个类,重写configureMessageConverters,加一个转换器再试试

@Configuration
public class CorsConfig {

    public CorsConfig(){

    }

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");

        //设置是否发送cookie信息
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}

后端的报错是content type不支持,说明传输数据的过程中content type不对,你前端调用ajax的时候指定下contentType:"application/json"

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题