Javascript AJAX';成功';在IE中执行AJAX之前调用 简述
要添加到列表中的另一个Internet Explorer问题。。。我的页面上有一个关闭当前选项卡/窗口的按钮,但在此之前,它会执行一个AJAX请求“删除文档会话”。非常简单的东西:Javascript AJAX';成功';在IE中执行AJAX之前调用 简述,javascript,php,jquery,ajax,internet-explorer,Javascript,Php,Jquery,Ajax,Internet Explorer,要添加到列表中的另一个Internet Explorer问题。。。我的页面上有一个关闭当前选项卡/窗口的按钮,但在此之前,它会执行一个AJAX请求“删除文档会话”。非常简单的东西: MN.ajax({ url: url, dataType: "JSON", errorText: 'There was a problem deleting the session, please retry later', success: function(data){
MN.ajax({
url: url,
dataType: "JSON",
errorText: 'There was a problem deleting the session, please retry later',
success: function(data){
// Close the tab/window
window.open(window.location, '_self').close();
}
});
忽略MN.ajax()
问题
上述代码在除IE之外的所有浏览器中都可以使用。在IE中,它在实际执行AJAX请求之前关闭窗口/选项卡。AJAX请求应该从我的数据库中删除一条记录,但除非我在window.open(window.location,'.'u self').close()行注释,否则不会删除任何记录代码>
可能的解决办法
我没有对此进行测试,但是我假设如果我用setTimeout
包装关闭窗口,那么它应该可以工作。然而,我想知道的是为什么!!?究竟为什么它会在AJAX请求实际完成之前执行“success”函数
我所尝试的。。。
我尝试过的另一件事是将“关闭窗口”代码放到complete
回调中,而不是success
回调中,但这绝对没有什么区别
更新
以下是完整的包含函数:
/**
* Close the Document
*/
MN.closeDocument = closeDocument;
function closeDocument(document_reference,session_id){
// Show the loading dialog
MN.loadingDialog('Closing Document...',function(){
// Execute the autosave function to save the session
MN('#document-modify-form').autosave('save',function(){
MN.ajax({
url: '//api.example.net/document/session/'+document_reference+'/',
dataType: "JSON",
errorText: 'There was a problem getting the documents session',
success: function(data){
// Only close if there are no changes
switch(data.status){
// There is no session or no changes
case 'no_session':
case 'no_changes':
// Delete the session
MN.ajax({
url: '//api.example.net/document/delete-session/'+document_reference+'/',
dataType: "JSON",
errorText: 'There was a problem deleting the session, please retry later',
success: function(data){
// Close the tab/window
window.open(window.location, '_self').close();
},
error: function(msg){
// Error code here
}
});
break;
// Other cases here
}
},
error: function(msg){
// Error code here
}
});
});
});
}
我已经根据您的代码构建了以下简化的测试页面。这在我的系统(Win8.1 x64,IE11)上正常工作:当ajax调用完成时,窗口在3秒钟后关闭
ajax.php:
ajax测试
功能测试_单击()
{
$.ajax({
url:'ajax.php',
成功:功能(数据)
{
//关闭选项卡/窗口
window.open(window.location,“_self”).close();
}
});
}
经过大量测试,我发现了问题所在。$.ajax
的功能和调用window.open(window.location,''u self').close()的功能绝对没有问题代码>在成功回调中。这管用
我试图删除的文档会话实际上已被删除,但因为窗口正在重新打开,然后由window.open(window.location,'.'u self').close()关闭代码>,正在重新创建会话!因此,该会话似乎从未被删除
这意味着我的问题有两种解决方案,或者,不要使用window.location
,因为这会重新打开同一页面,从而重新创建会话,或者使用javascript找到另一种关闭选项卡/窗口的方法
谢谢您的帮助,很抱歉浪费您的时间!:-) 做一个同步调用怎么样?@RafaelDiaz这不会有什么区别,因为在ajax请求完成之前,函数仍然不应该执行!这很奇怪。那么,你什么时候试过了吗@SyedQarib你错过了重点,修复这一点并不是我真正的目标,因为我可能会在30秒内通过某种肮脏的修复完成。我想知道的是,为什么它一开始就不起作用?在AJAX请求完成之前,如何调用“success”函数?@RobSchmuecker老实说,我认为这不是一个“有趣的现象”,因为微软的团队再次无法编写代码-P.我想如果我用setTimeout
包装它,那么它应该可以工作…我猜OP正在尝试在卸载之前保存的内容
,希望警报会显示这一点-您最初会有一个已删除的会话,然后,一旦您解除警报,它就会重新加载并重新创建会话。造成混淆的另一个因素是,您可能正在使用会话ID-使用新标识符会使您明显地一次又一次地创建新会话。@Alnitak不幸的是,这不是那种会话。警报会显示出来的,我只是太粗心了,没有检查两人之间的会话。。。谢谢你的帮助,这很公平,但是是什么让你相信这是一个MSIE特有的问题呢?该行为与其他浏览器有何不同?@Alnitak该问题在其他浏览器中没有出现。会话被删除了,因为我希望它是这样的,所以实际上是window.open().close()
的行为不同?
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
sleep(3);
?>
<!DOCTYPE html>
<html>
<head>
<title>ajax test</title>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function test_click()
{
$.ajax({
url: 'ajax.php',
success: function(data)
{
// Close the tab/window
window.open(window.location, '_self').close();
}
});
}
</script>
</head>
<body>
<a href="#" onclick="test_click()">Close</a>
</body>
</html>