jsonp跨域设置cookie以及获取cookie
A项目:
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
type : "get",
async:false,
url : "http://www.b.com/user/checkUserName.htm",
dataType : "jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonpCallback:"successCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
data: "", //请求数据
timeout:2000,
success:function(data){
if(data.retCode!="" && data.retCode!="1" ){
window.location="${base}/login/ssoLogin.jhtml?data="+data.body.data;
}else{
window.location="${base}/index.jhtml";
}
},
error:function(){
window.location="${base}/index.jhtml";
}
});
});
</script>
1.请求B项目为servlet,获取cookie,checkUserName为:
protected void processGet(HttpServletRequest request,
HttpServletResponse response, ServiceSession serviceSession)
throws Throwable {
AppData data=new AppData();
Map<string object> bodyMap=new LinkedHashMap<string object>();
PrintWriter out = response.getWriter();
response.setContentType("text/javascript");
response.setCharacterEncoding("UTF-8");
try {
String cookieValue = "";
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies) {
if(cookie.getName().equals(Constant.cookieName)) {
cookieValue = cookie.getValue();
break;
}
}
}
response.setHeader("Content-type", "text/javascript");
response.setHeader("Access-Control-Allow-Origin","*"); //支持全域名访问,不安全,部署后需要固定限制为客户端网址
response.setHeader("Access-Control-Allow-Methods","GET,OPTIONS,DELETE"); //支持的http 动作
response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type"); //响应头 请按照自己需求添加。
response.setHeader("P3P","CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
if(cookieValue!=""){
data.setRetCode("0");
bodyMap.put("data", cookieValue);
data.setBody(bodyMap);
}else{
data.setRetCode("1");
}
out.print("successCallback("+JSON.toJSONString(data)+")");
} catch (Exception e) {
e.printStackTrace();
}
}
B项目访问A:
<script type="text/javascript">
$(document).ready(function(){
alert(getCookie("FRONT"));
if(getCookie("FRONT")==""){
$.ajax({
type : "get",
async:false,
url : "http://starbhhc.wicp.net/FYJF/ssoLogin/checkUserName.jhtml",
dataType : "jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonpCallback:"successCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
data: "", //请求数据
timeout:2000,
success:function(data){
if(data.retCode!="" && data.retCode!="1"){
setCookie("FRONT",data.body.data);
window.location="http://117.39.28.230:8102/user/platformLogin.htm?data="+data.body.data;
}
},
error:function(){
}
});
}
});
function setCookie(name,value)
{
document.cookie=name+'='+value;
}
function getCookie(name)
{
var arr=new Array();
arr=document.cookie.split(";");
var i=0;
for(i=0; i<arr.length;i++)
{
arr2=arr[i].split("=");
if(arr2[0]==name)
{
return arr2[1];
}
}
return '';
}
$(document).ready(function(){
$('.flexslider').flexslider();
})
</script>
A项目为springMVC,获取A的cookie, checkUserName类为:
@RequestMapping(value = "/checkUserName", method = RequestMethod.GET)
public @ResponseBody void checkUserName(HttpServletRequest request,HttpServletResponse response) {
AppData data=new AppData();
Map<string object> bodyMap=new LinkedHashMap<string object>();
String cookieValue = "";
try {
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies) {
if(cookie.getName().equals(Constant.cookieName)) {
cookieValue = cookie.getValue();
break;
}
}
}
response.setHeader("Content-type", "text/javascript");
response.setHeader("Access-Control-Allow-Origin","*"); //支持全域名访问,不安全,部署后需要固定限制为客户端网址
response.setHeader("Access-Control-Allow-Methods","GET,OPTIONS,DELETE"); //支持的http 动作
response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type"); //响应头 请按照自己需求添加。
response.setHeader("P3P","CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
if(cookieValue!=""){
data.setRetCode("0");
bodyMap.put("data", cookieValue);
data.setBody(bodyMap);
}else{
data.setRetCode("1");
}
response.getWriter().print("successCallback("+JSON.toJSONString(data)+")");
} catch (IOException e) {
e.printStackTrace();
}
}