-
-
Save hd-deman/7249025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment