9

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

  • This code works normally in most browsers for all functions I have written
  • In IE 11 + Windows 8.1, it works in most cases, except when running a particular copy and paste function
  • Interestingly, when using IE 11, but with a different Document mode such as 8, I still get the same error, even though it works natively in IE8 + Windows 7
  • The error is 'Access is denied'

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:

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

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

6
  • Refer this thread stackoverflow.com/questions/22098259/…
    – Dnyanesh
    Nov 12, 2014 at 16:41
  • This isn't a cross domain request - var url = "control/ajax.php... It's requesting a page on the same site as the user is already visiting
    – Ben
    Nov 12, 2014 at 16:44
  • "yet when I try use in conjunction with a copy+paste method, it returns an Access is denied error" did you try the copy paste on it's own to see if that's where the "Access Denied" error is originating from? eliminate unnecessary code if possible.
    – Kevin B
    Nov 12, 2014 at 16:48
  • That was my initial thought, but the error is clearly on the xmlhttp.open line (as highlighted in the developer tools) - when I comment that out (as well as xmlhttp.send), there are no errors.
    – Ben
    Nov 12, 2014 at 17:01
  • Try putting the xmlhttp.open("GET",url,true) line above the xmlhttp.onreadstatchange declaration.
    – idbehold
    Nov 12, 2014 at 17:23

2 Answers 2

9

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);

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

3
  • 1
    This is so ridiculous and I am very grateful to you for posting this explanation because I would have struggled with it for a long time before figuring out what on earth was going on.
    – glenatron
    Feb 16, 2017 at 11:02
  • How does a 1ms timeout fix the issue? This makes the request succeed? Jun 5, 2017 at 20:44
  • I can't tell you why it works, but it definitely does! I guess it's just a bug!
    – Ben
    Jun 6, 2017 at 8:23
-1
setTimeout(function() {
        ajaxRequest('save_mark','save_mark_completed',[60962,1284,5]) 
    }, 1);

This did work for me for the first call after page load but later calls again started to show Access is denied error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.