项目是前后端分离,前端采用vue,后台用springboot2.x搭建
后台配置了跨域
@Configuration
public class MyCorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(ALL)
.allowedMethods(ALL)
.allowedHeaders(ALL)
.allowCredentials(true)
.maxAge(3600);
}
}
前台get请求的跨域是正常的,但是在进行post请求时报400错误
后台控制器
@PostMapping(value = "/login")
public ResponseObject login(@RequestParam String username, @RequestParam String password){
System.out.println(username);
System.out.println(password);
...
}
前台代码
this.$refs.loginFormRef.validate(async valid => {
if (!valid) return
const { data: res } = await this.$http.post('login', this.loginForm)
import axios from 'axios'
// 配置请求的跟路径
axios.defaults.baseURL = 'http://localhost:8088/'
// 在 request 拦截器中,展示进度条 NProgress.start()
axios.interceptors.request.use(config => {
NProgress.start()
return config
})
// 在 response 拦截器中,隐藏进度条 NProgress.done()
axios.interceptors.response.use(config => {
NProgress.done()
return config
})
post请求 参数在body里