接口开发允许跨域

public @responseBody Map getData(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
	// 允许跨域
	// 指定允许其他域名访问
	response.setHeader("Access-Control-Allow-Origin", "*");
	// 响应类型
	response.setHeader("Access-Control-Allow-Methods", "*");
	// 响应头设置
	response.setHeader("Access-Control-Allow-Headers",
			"x-requested-with,content-type");
	/*ssssss*/
	Map map = new HashMap();
	return map;
}

springboot项目这样写:
新建XxxxxxConfig类 implements WebMvcConfigurer

@Override
	public void addCorsMappings(CorsRegistry registry) {
		// 指定允许其他域名访问
		registry.addMapping("/**").allowedOrigins("*");
		
		// 响应头设置
		registry.addMapping("/**").allowedHeaders("x-requested-with,content-type");
		
		// 响应类型
		registry.addMapping("/**").allowedMethods("*");
	}

你可能感兴趣的:(java基础,peter,经验分享)