手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

dubbo restful 如何设置跨域addHeade

时间:2024-10-16 02:29:18

1、首先,在ajax请求参数中,json格式需要标准格式,手动拼接引号var data = "{\"title\":\""+title+"\",\"context\":\""+context+"\",\"actionType\":"+actionType+",\"notifyType\":"+notifyType+",\"sendType\":"+sendType+",\"startTime\":\""+startTime+"\",\"endTime\":\""+endTime+"\",\"activeStartTime\":\""+activeStime+"\",\"activeEndTime\":\""+activeEtime+"\",\"weeks\":["+weeks+"],\"time\":\""+time+"\",\"fromType\":0}";

dubbo restful 如何设置跨域addHeade

2、Dubbox添加响应过滤器:public class ControllFilter implements ContainerResponseFilter {public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {if (containerRequestContext.getMethod().equals("OPTIONS")) {containerResponseContext.getHeaders().add("Access-Control-Allow-Origin", "*");

dubbo restful 如何设置跨域addHeade

3、containerResponseContext.getHeaders().add("Access-Control-Allow-Headers", "Content-Type,x-requested-with,Authorization,Access-Control-Allow-Origin");containerResponseContext.getHeaders().add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");containerResponseContext.getHeaders().add("Access-Control-Max-Age" ,"360");}}}

dubbo restful 如何设置跨域addHeade

4、xml修改:<dubbo:protocol name="rest" port="8828" threads="500" contextpath="services" extension="com.xxx.xxx.filter.ControllFilter" server="tomcat" accepts="500"/>

dubbo restful 如何设置跨域addHeade

5、实现代码方法中给Response添加跨域代码:public AddDictResponse addDict(AddDictRequest request) {AddDictResponse response = new AddDictResponse();try {}catch (Exception ex){response.setStatusCode(ErrorCodeEnm.FAIL.getCode());response.setDescription(ErrorCodeEnm.SYS_ERROR.getCodeMsg());}finally {if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) {RpcContext.getContext().getResponse(HttpServletResponse.class).addHeader("Access-Control-Allow-Origin","*");}}return response.

dubbo restful 如何设置跨域addHeade
© 手抄报圈