Cross domain scripting, jQuery and IE8
-
I can't get this damn thing to work and it's driving me nuts. Here's the scenario: Linux box running Apache - set up to send the Access-Control-Allow-Origin * header for the folder I'm working with PHP web service that will build and return a JSON object on a request to mypage.php?project=1 or similar Simple html test page running locally (from the file system) on my machine using jQuery to retrieve the JSON object from my web service like so:
$.getJSON(url, function (data) { $.each(data.factors, function (i, field) { $("div").append(field.FactorName + " "); }); });
Works fine in FF, works fine (on FF and IE) if I upload the page to the same server as the web service and run it form there, doesn't work at all in IE when running the page locally (doesn't look like the response is ever received). I did a little research and it looks like for cross domain scripting in IE you should use XDomainRequest instead of XMLHttpRequest (which I think jQuery is using under the covers in getJSON). So I try this (based on some website):
if ($.browser.msie && window.XDomainRequest) { var xdr = new XDomainRequest(); xdr.open("GET",url); xdr.onload = function() { alert("XDR Sent"); }; xdr.send(); } else { $.getJSON(url, function (data) { $.each(data.factors, function (i, field) { $("div").append(field.FactorName + " "); }); }); }
On IE it chokes on xdr.open with an "Access is denied" error. Any idea what the problem might be?