ajax有跨域:不同项目名即跨域,ajax解决跨域访问的本质就是java回调js
在跨域请求的时候,如果没有解决shrio是拿不到其他系统的登录信息(可以调用对方项目的方法(ip相同项目名不同)),如果ip也不同那么方法都调用不了
1,jsonp回调
2,Java回调js(jsonp本质)
A:
回调的函数服务端也要声明
http://blog.csdn.net/yttea/article/details/51549736
http://chenshengzun.iteye.com/blog/1847629
function getSeeion(){
$.ajax({
url:"http://localhost:8081/web/siteindex.do/top",
type:"post",
dataType:"jsonp",
//cache:false,
async:true,
data:{"accountid":"21","path":"${TopPath}"},
jsonpCallback: "jsonpcallback",
success: function (response) {
alert(response);
}
});
function jsonpcallback(data){
alert(data);
}
}
B:下面这种没有跨域问题
<script src="http://localhost:8081/web/siteindex.do/top">
//返回的会放在这个里面
</script>
这种可以结合Java回调js(jsonp本质)用response.writer();把要回调的函数及其参数返回打出,即可回调,
前提是jsp已写好被回调的函数(jsonp的本质)
EsteelHome项目:
function jsonpcallback(data){
login11(data);
}
function login11(name){
isLogin=true;
document.getElementById("hyy").innerHTML=name;
document.getElementById("ydl").style.display="";
document.getElementById("wdl").style.display="none";
}
///这个要注意掉的时机,否则会引起调用过早,但是页面没有渲染出来,找不到hyy元素
<script type="text/javascript" src="http://localhost:8081/web/siteindex.do/webTop">
</script>
Esteelweb项目:
@RequestMapping(value = "/webTop")
@ResponseBody
public void webTop(Model model,HttpServletRequest request,HttpServletResponse response, HttpSession session) throws IOException {
/*CusUserBean cusUserBean = CASUtil.getCusUserBean();*/
CusFirmBean cusFirmBean= CASUtil.getCustomer();
//request.getSession().setAttribute(Constants.SESSION_FIRM, cusUserBean);
response.setCharacterEncoding("UTF-8");
if(cusFirmBean!=null){
response.getWriter().write("jsonpcallback"+ "('"+cusFirmBean.getCustomerName()+"');");//moduleId
}
else{
response.getWriter().write("jsonpcallback"+ "();");//moduleId
}
// return "frame/index.p";
}
C:用Java写一个代理
发起http请求