Home  >  Article  >  Web Front-end  >  关闭页面window.location事件未执行的原因及解决方法_javascript技巧

关闭页面window.location事件未执行的原因及解决方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:38:001483browse

1、问题描述:

JS中定义widow.location = function(),页面关闭时,logout()函数未执行。

window.onunload = function() {
logout();
}

function logout(reqParam, callback){
var userManageServiceUrl = "http://" + getServerAddr() + "/axis2/services/UserManageService";
var urlList = [];
var url = window.location.href;
urlList = url.split("?");
var sessionID = urlList[1];
reqParam.sessionID = sessionID;
var pl = new SOAPClientParameters();
var reqParamStr = JSON.stringify(reqParam);
pl.add("reqParam", reqParamStr);
SOAPClient.invoke(userManageServiceUrl, "logout", pl, false, callback);
}

2、问题原因:

logout()中调用SOAPClient.invoke()方法,参数为true,代表前端和server是异步方式通信,即前端还未接收到server端的响应,便已经执行后面的语句了,在该问题中表现为前端执行logout()时还未等到server的响应,便已经将页面关闭了,所以表现为logout()未执行。

3、解决方法:

将前端和server通信方式改为同步,即将SOAPClient.invoke()方法中true改为false,问题得到解决。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn