zoukankan      html  css  js  c++  java
  • 服务端解决浏览器跨域问题

    1 简单跨域GET,POST 在方法中加上  response.addHeader("Access-Control-Allow-Origin","http://localhost:8080"); 第二个参数是允许跨域的服务器

    eg:

    @GetMapping("/stock/getIpAndPort")
    public String getIpAndPort(HttpServletResponse response) {
    response.addHeader("Access-Control-Allow-Origin","http://localhost:8080");
    return "127.0.0.1:9999";
    }

    2 对于Spring boot 项目 可以在启动类添加 bean
    eg:

    //    复杂跨域问题(两次握手)
    @Bean
    public WebMvcConfigurer corsConConfigurer(){
    return new WebMvcConfigurer() {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/greeting-javaconfig").allowedOrigins("http://localhost:9000");
              //allowedOrigins里面的参数为允许访问的ip地址
                }
    };
    }



  • 相关阅读:
    qt install (1)
    learning rewind func
    learning strrchr func
    learning memchr func
    git lfs setpu(4)
    大端与小端
    git branch/meger step(3)
    git log/show/HEAD step(2)
    调用外部EXE文件
    获取计算机上的所有进程
  • 原文地址:https://www.cnblogs.com/pengtaotao/p/13303758.html
Copyright © 2011-2022 走看看