Skip to content

Instantly share code, notes, and snippets.

@wallin
Created June 28, 2013 13:56
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 wallin/5884850 to your computer and use it in GitHub Desktop.
Save wallin/5884850 to your computer and use it in GitHub Desktop.
Drop-in jQuery-plugin for allowing cross domain ajax-calls in IE
$.ajaxTransport((options, originalOptions, jqXHR) ->
if window.XDomainRequest
xdr = null
return {
send: (headers, completeCallback) ->
# Match protocol since XDR doesn't allow differences
if 'http:' is document.location.protocol
options.url = options.url.replace('https:', 'http:')
xdr = new XDomainRequest()
xdr.open options.type, options.url
xdr.timeout = 20000
xdr.onprogress = ->
xdr.onload = ->
if @contentType.match(/\/xml/)
dom = new ActiveXObject("Microsoft.XMLDOM")
dom.async = false
dom.loadXML @responseText
completeCallback 200, "success", [ dom ]
else
completeCallback 200, "success", [ JSON.parse(@responseText) ]
xdr.ontimeout = ->
completeCallback 408, "error", [ "The request timed out." ]
xdr.onerror = ->
completeCallback 404, "error", [ "The requested resource could not be found." ]
xdr.send(options.data)
abort: -> xdr.abort() if xdr
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment