Skip to content

Instantly share code, notes, and snippets.

@MrOrz
Created September 1, 2013 16:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MrOrz/6405400 to your computer and use it in GitHub Desktop.
Save MrOrz/6405400 to your computer and use it in GitHub Desktop.
Patching `XMLHttpRequest` for AngularJS and Internet Explorer < 10. It replaces the original `window.XMLHttpRequest` with a thin interface that only implements the parts AngularJS core uses. Missing methods of `XDomainRequest` are fed with `angular.noop`.
# A XMLHttpRequest wrapper for IE < 10.
# When it comes to cross domain ajax requests, it uses XDomainRequest instead.
# ** Only supports GET and POST requests! **
# Targeting IE < 10
# https://github.com/amcintyre-cs/strophejs-plugins/commit/b0a6ff2571bb70dd2f6f65c46034f4e47d75d564
return unless window.XDomainRequest and !("withCredentials" in window.XMLHttpRequest)
OriginalXMLHttpRequest = window.XMLHttpRequest
window.location.origin ?= window.location.protocol + '//' + window.location.host
# Determine whether the requested URL is in another domain.
isXDomain = (requestUrl) ->
if requestUrl[0] == '/'
return false if requestUrl.length == 1 # '/' is same domain
# '/....' is same domain
# '//...' is cross domain
return if requestUrl[1] == '/' then true else false
requestUrl.slice(0, window.location.origin.length) != window.location.origin
class window.XMLHttpRequest
open: (method, url, others...) ->
if !isXDomain(url)
@implementation = new OriginalXMLHttpRequest
# Updates the properties when readystate changes.
@implementation.onreadystatechange = () =>
# Updates properties
if @implementation.readyState == 4
for prop in ['readyState', 'status', 'responseText']
@[prop] = @implementation[prop]
# Invoke the callback bound on 'this'
@onreadystatechange() if @onreadystatechange
# Borrow the methods in OriginalXMLHttpRequest.
for func in ['abort', 'getAllResponseHeaders', 'getResponseHeader', 'send', 'setRequestHeader']
do (func) =>
@[func] = () ->
@implementation[func](arguments...)
else
@implementation = new XDomainRequest
# Updates the properties when the request is fully loaded
# (XDomainRequest has no onreadystatechange!)
@implementation.onload = () =>
# Updates properties
@responseText = @implementation.responseText
# Missing properties in XDomainRequest
@readyState = 4
@status = 200
# Invoke the callback bound on 'this'
@onreadystatechange() if @onreadystatechange
# Borrow the methods in XDomainRequest.
@abort = () -> @implementation.abort(arguments...)
@send = () -> @implementation.send(arguments...)
# Missing methods in XDomainRequest
for func in ['getResponseHeader', 'getAllResponseHeaders', 'setRequestHeader']
do (func) =>
@[func] = angular.noop
@implementation.open method, window.encodeURI(url)
@MrOrz
Copy link
Author

MrOrz commented Sep 1, 2013

Be sure to remove window.encodeURI in the last line if you already encoded your URLs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment