zoukankan      html  css  js  c++  java
  • 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());

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

  • 相关阅读:
    想写点啥?
    网站优化记录
    freemarker了解
    spring理解
    mybatis了解
    struts机制
    SSM整合时初始化出现异常
    常用的正则表达式
    MySQL数据库安装配置步骤详解
    完全卸载MySQL数据库,实现重装
  • 原文地址:https://www.cnblogs.com/sonofdemon/p/12674767.html
Copyright © 2011-2022 走看看