zoukankan      html  css  js  c++  java
  • springboot+angular4前后端分离 跨域问题

    springboot中新增一个过滤器如下:

    package com.rtpksps.kss.config;
    import org.springframework.stereotype.Component;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * @author Administrator
    * @title: OriginFilter
    * @projectName rtpksps-kss
    * @description: TODO
    * @date 2019/9/29 0029下午 15:52
    */
    @Component
    public class OriginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
    filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {


    }

    @Override
    public void destroy() {

    }
    }


    在接口中指定需要的方法类型

    angular页面发送http请求:

    reqData(){ // 向后台请求数据
               var url = "http://192.168.100.100:8888/cmsRole/show";//视频教程中的测试用请求地址
               var _that = this;
               this.http.get(url).subscribe(function(data){//请求成功的回调函数
              console.log(data);
               },function(err){// 请求失败的回调函数
                 console.log(err);
               })
             }





    生下来活下去, HELLO WORLD! 请多指教
  • 相关阅读:
    view如何被添加到window上并显示出来
    事件分发机制
    绘制机制
    setContentView
    消息机制——handler
    布局文件是如何被解析的?
    Xamarin.ios引用第三方SDK
    Xamarin.ios——First APP
    UITextView 文本垂直居中
    从NavigationController 下的UITableView中移除 header
  • 原文地址:https://www.cnblogs.com/lideqiang0909/p/11609284.html
Copyright © 2011-2022 走看看