Uncaught (in promise) Error: Network Error at createError(axios跨域问题)


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());

就可以正常请求到了数据了!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM