zoukankan      html  css  js  c++  java
  • tomcat允许跨域请求:

    在springmvc-servlet.xml中配置
    <mvc:interceptors>

    <bean class="com.read.api.pc.interceptor.CrossDomainInterceptor" />

    <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->

    </mvc:interceptors>

    com.read.api.pc.interceptor新建CrossDomainInterceptor.java

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;

    public class CrossDomainInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest req, HttpServletResponse reponse, Object arg2, Exception arg3) throws Exception {

    reponse.setHeader("Access-Control-Allow-Origin", "*");
    }

    @Override
    public void postHandle(HttpServletRequest req, HttpServletResponse reponse, Object arg2, ModelAndView arg3) throws Exception {

    reponse.setHeader("Access-Control-Allow-Origin", "*");
    }

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
    // TODO Auto-generated method stub
    arg1.setHeader("Access-Control-Allow-Origin", "*");
    return true;
    }

    }
    如果只允许某一个接口支持跨域请求:
    @RequestMapping("doorDesign/index/add.do")
    @ResponseBody
    public String add(HttpServletRequest req, HttpServletResponse rsp,
    DoorDesignEntity doorDesign, String name,String phone, String province,
    String city ){
    rsp.setHeader("Access-Control-Allow-Origin", "*");//允许跨域
    return service.add(req, rsp, doorDesign, name, phone, province, city);
    }

  • 相关阅读:
    decode函数
    自我介绍
    语法》第六章 数组
    语法》第二章 数据类型
    语法》第四章 字符串
    语法》第七章 函数
    取模和取余的区别
    语法》第五章 对象
    语法》第三章 数值
    语法》第一章 基本语法
  • 原文地址:https://www.cnblogs.com/ft-Pavilion/p/5945645.html
Copyright © 2011-2022 走看看