實(shí)際案例:
公司開(kāi)發(fā)了新的項(xiàng)目江掩,采用Extjs6学辱,需要和老系統(tǒng)的數(shù)據(jù)做同步乘瓤,比如組織架構(gòu)和人員同步(增、刪策泣、改衙傀、異動(dòng))等,因?yàn)閮烧呖蚣懿煌荆蛎煌程В瑪?shù)據(jù)庫(kù)也不同,所以就存在了跨域問(wèn)題危队,如何能實(shí)現(xiàn)案例中遇到的問(wèn)題呢聪建?
廢話不多說(shuō),直接上代碼:
1茫陆、我們要實(shí)現(xiàn)新系統(tǒng)同步老系統(tǒng)數(shù)據(jù)金麸,所以ajax請(qǐng)求在新系統(tǒng)發(fā)起。以新增組織為例:
// 跨域新增/修改組織
Ext.data.JsonP.request({
url : directUrl
+ 'jsonpCrossdomain.do?method=setGroupForJsonP',
timeout : 300000,
// 參數(shù)
params : {
parentId : checkNode.get('id'),
orgId : orgId,
orgName : orgName,
orgLevelType : orgLevelType,
sortNum : sortNum,
isUpdate : isUpdate,
mark : 'computer',
loginFrom : 'wx',
sessionUid: sessionUid,
currentUserCipher: currentUserCipher
},
callbackKey : "callback",// callback參數(shù)
success : function(response) {
var result = response.msg;
if (result == 'Y') {
Ext.MessageBox.show({
title : '提示',
msg : '組織數(shù)據(jù)保存成功',
buttons : Ext.MessageBox.OK,
icon : Ext.MessageBox.INFO
});
me.up('window').hide();
} else {
Ext.Msg
.alert('提示',
"當(dāng)前數(shù)據(jù)保存成功簿盅,但老系統(tǒng)數(shù)據(jù)出現(xiàn)同步錯(cuò)誤挥下,請(qǐng)聯(lián)系管理員!");
}
},
failure : function(result) {
if (result == "error") {
Ext.MessageBox.show({
title : '錯(cuò)誤提示',
msg : '遠(yuǎn)程服務(wù)器無(wú)法訪問(wèn)挪鹏,請(qǐng)聯(lián)系管理員见秽!',
buttons : Ext.MessageBox.OK,
icon : Ext.MessageBox.ERROR
});
} else {
console.log(result)
}
}
});
2、在老系統(tǒng)中后臺(tái)寫入方法
/**
* @功能描述:jsonP新增組織
*/
@SuppressWarnings("unchecked")
public ActionForward setGroupForJsonP(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Session session = null;
Transaction tx = null;
String parentId = (String) request.getParameter("parentId");
String isUpdate = (String) request.getParameter("isUpdate");
String orgId=(String) request.getParameter("orgId");
String orgName=(String) request.getParameter("orgName");
String sortNumStr=(String) request.getParameter("sortNum");
String orgLevelType=(String) request.getParameter("orgLevelType");
int sortNum=0;
if(StringUtils.isNotBlank(sortNumStr)){
sortNum=Integer.parseInt(sortNumStr);
}
if(StringUtils.isNotBlank(orgName)){
orgName = new String(orgName.getBytes("iso-8859-1"), "utf-8");
}
boolean jsonP = false;
String cb = request.getParameter("callback");
if (cb != null) {
jsonP = true;
response.setContentType("text/javascript;charset=utf-8");
} else {
response.setContentType("application/x-json;charset=utf-8");
}
//密文驗(yàn)證
String sessionUid = request.getParameter("sessionUid");
String currentUserCipher = request.getParameter("currentUserCipher");
if(!(StringUtils.isNotBlank(currentUserCipher)
&& StringUtils.isNotBlank(sessionUid)
&& sessionUid.equals(AESTools.decrypt(currentUserCipher)))){
String str = "{success:true, msg:'非法訪問(wèn)'}";
if (jsonP) {
response.getWriter().write(cb + "("+str+");");
}else{
response.getWriter().write(str);
}
return null;
}
try {
session = HibernateClass.getSession();
tx = session.beginTransaction();
String str = "{success:true, msg:'Y'}";
List listExisted = session.createQuery("from Systemgroups sg where sg.orgCode = :orgCode")
.setParameter("orgCode", orgId).list();
if ((listExisted.size() > 0) && (isUpdate.equals("false"))) { //有重復(fù)orgId
str = "{success:true, msg:'N'}";
} else {
String longNum = "XXX"; //找到父結(jié)點(diǎn)的長(zhǎng)代碼
List list = session.createQuery("from Systemgroups sg where sg.orgId = :parentid")
.setParameter("parentid", parentId).list();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Systemgroups sg = (Systemgroups) iter.next();
longNum = sg.getLongNumber();
}
if (isUpdate.equals("false")) { //新增組織
Systemgroups sg = new Systemgroups();
sg.setOrgId(orgId);
sg.setOrgCode(orgId);
sg.setOrgName(orgName);
sg.setParentId(parentId);
sg.setIsProjectParent("4".equals(orgLevelType)?true:false);
sg.setIsdeleted("N");
sg.setSortNum(sortNum);
sg.setProjectContractName("");
sg.setLongNumber(longNum + "!" + orgId);
session.save(sg);
} else {//修改組織
Systemgroups sg=(Systemgroups) session.get(Systemgroups.class, orgId);
sg.setOrgName(orgName);
sg.setSortNum(sortNum);
sg.setIsProjectParent("4".equals(orgLevelType)?true:false);
session.update(sg);
}
}
if (jsonP) {
response.getWriter().write(cb + "("+str+");");
}else{
response.getWriter().write(str);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
String str = "{success:true,msg:'更新錯(cuò)誤讨盒,請(qǐng)檢查網(wǎng)絡(luò)連接是否正常解取!'}";
if (jsonP) {
response.getWriter().write(cb + "("+str+");");
}else{
response.getWriter().write(str);
}
} finally {
HibernateClass.closeSession(session);
}
return null;
}