axios解决跨域问题
只需要在JS中加入当然下面的URL需要修改,created() {const _this = this;axios.get('http://localhost:8181/book/findAll/').then(function (resp){_this.books = resp.data;console.log("我是回调"+resp.data);})}...
·
前端:
需要在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("*");
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)