1

I have the following code which works on all browsers except IE: I get Access Denied. Please Help!!! I have looked at tons of examples and still cannot resolve this. I would like to keep it simple as all I am doing is returning some text from the URL.

<script type="text/javascript">
    // Insure the document is loaded...
    $(document).ready(function() {

        // Set the URL for the POS API...
        var PosApiUrl = 'https://test.safety.com/posapi/getposmessage?app=RMIS';


        // Load the POS API Message...
        function loadPOSMessage(edurl) {

            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else if (window.XDomainRequest) {
                xmlhttp = new XDomainRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var jsonEdition = xmlhttp.responseText;
                    var objEdition = JSON.parse(jsonEdition);
                    $.each(objEdition, function(key, val) {
                        if (val.length === 0) {
                            $('.posMsgContainer').hide();
                        } else {
                            $('.posMsgContainer').html(val);
                        }


                    });
                }
            };
            xmlhttp.open("GET", edurl, true);
            xmlhttp.send();
        }

        // Make the call to load the POS message...
        loadPOSMessage(PosApiUrl);


    });
  </script>
Paul Evans
  • 11
  • 1
  • http://stackoverflow.com/questions/10232017/ie9-jquery-ajax-with-cors-returns-access-is-denied – A.O. Jul 17 '13 at 21:23

1 Answers1

0

The code above won't work because the API for IE's XDomainRequest is slightly different from that of XMLHttpRequest. For example, instead of onreadystatechange() it uses onload().

I've found that the simplest way to support CORS on IE 8/9 is to include the xhr-xdr-adapter.js file from here: https://github.com/intuit/xhr-xdr-adapter/blob/master/src/xhr-xdr-adapter.js

You can include it where needed as follows:

<!--[if lt IE 10]>
<script src="xhr-xdr-adapter.js" type="text/javascript"></script>
<![endif]-->

Once you include the xhr-xdr-adapter, you can use the standard XMLHttpRequest API, and not worry about whether you're running on IE 8/9 or not (as long as you don't need to do something fancy like send custom headers, or use a method other than GET or POST.)

Lawrence
  • 4,909
  • 1
  • 15
  • 11