解决方式一:
前端直接解决,使用jsonp的方式,发送请求:
this.$http.jsonp('https://api.douban.com//v2/movie/top250', {},
{
headers: {},
emulateJSON: true }).then((response) => {
this.movie = response.data;
console.log(this.movie);
});
解决方式二:
后端写一个自定义的配置解决浏览器跨域问题:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 允许跨域访问的路径
.allowedOrigins("*") // 旅行的域源
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") // 允许请求方法名
.allowedHeaders("*") // 允许头部设置
.allowCredentials(true); // 是否发送cookie
}
}