springboot写个配置类
package com.gdk.musicService.config; import org.springframework.boot.SpringBootConfiguration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @SpringBootConfiguration public class MyWebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowCredentials(true) .allowedOrigins("http://localhost:8080") .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") .allowedHeaders("*") .maxAge(3600); } } 直接用后端“监听” vue服务端口8080, vue访问就直接 ,vue端就不用配置跨域
- this.$axios.post('/login', {
- username: this.ruleForm.name,
- password: this.ruleForm.pass
- }).then(res => {
- if (res.data.code === 200) {
- this.$message({
- type: 'success',
- message: res.data.message
- });
- this.$store.commit('login', res.data.data);
- this.$router.push("/index");
- }
- if (res.data.code === 400) {
- this.$message({
- type: 'error',
- message: res.data.message
- });
- }
- }).catch(error => {
- this.$message({
- type: 'error',
- message: '出现异常!'
- });
- console.log(error)
- });