IE 11 错误 - 访问被拒绝 - XMLHttpRequest

我在使用 IE11 和 ajax 时遇到了一个特殊错误.对于我使用下面的代码发出的几乎所有请求,一切都很好,但是当我尝试与复制+粘贴方法结合使用时,它返回一个访问被拒绝错误.总结一下

I'm having a peculiar error with IE11 and ajax. For nearly all the requests I make using the code below, everything is fine, yet when I try use in conjunction with a copy+paste method, it returns an Access is denied error. So to summarise

  • 这段代码在大多数浏览器中都能正常运行我编写的所有函数
  • 在 IE 11 + Windows 8.1 中,它在大多数情况下都有效,除非运行特定的复制和粘贴功能
  • 有趣的是,当使用 IE 11,但使用不同的文档模式(例如 8)时,我仍然收到相同的错误,即使它在 IE8 + Windows 7 中本机工作
  • 错误是访问被拒绝"

这里是 AJAX 代码:

Here is the AJAX code:

function ajaxRequest(requestName,responseFunction,parameters) {
 var xmlhttp;
 if (requestName.length==0) return;
 if (window.XMLHttpRequest)  {
     xmlhttp=new XMLHttpRequest();
 } else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function() {
     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        if(xmlhttp.responseText == 'Error') alert('Error processing request. Please refresh the page and try again');
        else if(xmlhttp.responseText != '') eval(responseFunction+"('"+xmlhttp.responseText+"')");
     }
 }
 var now = new Date();
 var url = "control/ajax.php?request="+requestName+"&parameters="+parameters+"&timestamp"+now;
 xmlhttp.open("GET",url,true);
 xmlhttp.send();
}

一个失败的例子,设置了以下变量:

An example of a failure, had the following variables set:

请求名称:save_marksheet_mark"响应函数:update_save_marksheet_mark"参数:[60962,1284,5]

requestName: "save_marksheet_mark" responseFunction: "update_save_marksheet_mark" parameters: [60962,1284,5]

这段代码有问题吗?在特定情况下,IE11 是否会在此代码中引发错误?

Is there something wrong with this code? Is there a reason why IE11 would throw an error with this code, in particular circumstances?

推荐答案

这个问题似乎得到了很多关注,所以以防万一有人想知道,我通过在原始 AJAX 上使用 setTimeout() 解决了这个问题称呼.例如:

This question appears to be getting a lot of views, so just in case anybody was wondering, I solved this problem by using a setTimeout() on the original AJAX call. E.g:

setTimeout(function() {
        ajaxRequest('save_mark','save_mark_completed',[60962,1284,5]) 
    }, 1);

我假设这是 IE 中的某种错误.只需 1 毫秒!

I'm assuming it's some kind of bug in IE. Just 1 millisecond was all it needed!

相关文章