Skip to content

Instantly share code, notes, and snippets.

@AMHOL
Created June 17, 2014 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMHOL/a2a83746fede5ea7be80 to your computer and use it in GitHub Desktop.
Save AMHOL/a2a83746fede5ea7be80 to your computer and use it in GitHub Desktop.
Angular JS IE CORS fix - if you control the API server

-- CORS FIX - IF YOU CONTROL THE API SERVER

Add the following somewhere in your application config

app.run([
  '$window',
  function($window) {
    if ( !$window.location.origin ) {
      $window.location.origin = $window.location.protocol + "//" + $window.location.hostname + ($window.location.port ? ':' + $window.location.port : '');
    }
    if ( typeof XMLHttpRequest !== 'undefined' && !('withCredentials' in new XMLHttpRequest()) ) {
      var s = document.createElement('script');
      s.type = 'text/javascript';
      s.src = 'http://jpillora.com/xdomain/dist/0.6/xdomain.min.js';
      s.setAttribute('slave', 'http://'+ phantomService.getServiceUri('default') +'/proxy.php?master='+ $window.location.origin);
      document.getElementsByTagName('head')[0].appendChild(s);
    }
  }
]);

Add proxy.php to API server root

<!DOCTYPE HTML>
<script src="http://jpillora.com/xdomain/dist/0.6/xdomain.min.js" master="<?= filter_var($_GET['master'], FILTER_SANITIZE_URL); ?>"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment