标签:angularjs实现cookie跨域 jquery实现cookie跨域 cookie跨域
前后端分离被越来越多的公司重视利用,然后带来的最棘手的问题就是,用户信息应如何保存。<pre name="code" class="javascript">function getAdustryController($scope,$http){ $http.post('http://localhost/ajax/getAllIndustryCategoty.pt',{languageColumn:'name_eu'},{'withCredentials':true}).success(function(data){ $scope.industries = data; }); }JAVA:
public String execute(){ /* 要存入的cookie信息 */ Cookie cookie = new Cookie("test_test","321"); cookie.setMaxAge(3600); response.addCookie(cookie); /* This is a part of security, you cannot do that. If you want to allow credentials then your Access-Control-Allow-Origin must not use *. You will have to specify the exact domain. */ response.setHeader("Access-Control-Allow-Origin", "http://test.domain.cn"); //请求源 response.setHeader("Access-Control-Allow-Methods","POST"); //请求方式POST, GET, OPTIONS response.setHeader("Access-Control-Max-Age", "3600"); //有效期 response.setHeader("Access-Control-Allow-Headers", "Content-Type, *"); //请求头类型 response.setHeader("Access-Control-Allow-Credentials","true"); //是否支持cookie跨域 SiteHandlerAction SiteHandler = (SiteHandlerAction) BeansFactory.getBean(SiteHandlerAction.class); List<IndustryCategory> list = SiteHandler.getAllIndustryCategory(); //所有的分类集合 JSONArray jsonArray = JSONArray.fromObject(list); //将list转为json String json = jsonArray.toString(); //转为json字符串 //将字符串写进输出流 try { PrintWriter write = response.getWriter(); write.print(json); write.close(); } catch (IOException e) { e.printStackTrace(); } return NONE; }
$.ajax("http://localhost/ajax/getAllIndustryCategoty.pt",{ type:"POST", data:{languageColumn:'name_eu'}, xhrFields:{withCredentials: true}, crossDomain::true, success:function(data, status, xhr){} });
Access-Control-Allow-Origin: <origin> | * | 授权的源控制 |
Access-Control-Max-Age: <delta-seconds> | 授权的时间 |
Access-Control-Allow-Credentials: true | false | 控制是否开启与Ajax的Cookie提交方式 |
Access-Control-Allow-Methods: <method>[, <method>]* | 允许请求的HTTP Method |
Access-Control-Allow-Headers: <field-name>[, <field-name>]* | 控制哪些header能发送真正的请求 |
标签:angularjs实现cookie跨域 jquery实现cookie跨域 cookie跨域
原文地址:http://blog.csdn.net/ligang2585116/article/details/44806853