axios请求到了数据但then返回不到数据,这是由于vue前端访问地址出现的跨域问题。
1、如果你是自己写的后端,可以添加配置类来避免跨域问题(建议使用)
package com.ftest.springboot.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 CrosConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowCredentials(true)
.allowedHeaders("*")
.maxAge(3600);
这样就不用担心跨域了!
2、更改前端并不是太好,主要用于前端开发时的测试数据
进入terminal终端,安装node模块
npm i koa2-cors -D
js加上代码:
const cors = require('koa2-cors')
app.use(cors())
就可以正常请求到了数据了!