我在使用 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
这里是 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+"¶meters="+parameters+"×tamp"+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!
这篇关于IE 11 错误 - 访问被拒绝 - XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)