由A和B两个系统组成的sso站点中,A系统的网址为http://a.domin.com:8899,B系统的网址为http://b.domin.com:9876.
//B系统后台的test方法
public void test(HttpServletRequest request,HttpServletResponse response) throws Exception
{
//String data = (String) request.getParameter("data");//取参数
//业务处理略
response.setContentType("application/json; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "http://b.domin.com:9876");//如果有端口必须加上
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "GET,POST");
response.setHeader("Access-Control-Allow-Headers",
"Content-Type, Content-Range, Content-Disposition, Content-Description");
response.getWriter().write("{\"state\":1,\"msg\":\"ok\"}");
return;
}
//A系统页面部分调用B系统的test方法可以用如下代码:
function test(){
var contentType ="application/x-www-form-urlencoded; charset=utf-8";
if(window.XDomainRequest){//for IE8,IE9
contentType = "text/plain";
}
$.ajax({
url : "test",//test方法的访问路径
data:{id:1,age:22},
type : "POST",
dataType : "json",
xhrFields: {
withCredentials: true
},
crossDomain: true,
contentType:contentType,
success : function(data) {
alert(data.state);
},
error : function(data){
alert(2);
}
});
}