Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions reqwest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

var context = this

if ('window' in context) {
if ('document' in context) {
var doc = document
, byTag = 'getElementsByTagName'
, head = doc[byTag]('head')[0]
Expand All @@ -25,7 +25,6 @@
}
}


var httpsRe = /^http/
, protocolRe = /(^\w+):\/\//
, twoHundo = /^(20\d|1223)$/ //http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
Expand All @@ -34,7 +33,6 @@
, requestedWith = 'X-Requested-With'
, uniqid = 0
, callbackPrefix = 'reqwest_' + (+new Date())
, lastValue // data stored by the most recent JSONP callback
, xmlHttpRequest = 'XMLHttpRequest'
, xDomainRequest = 'XDomainRequest'
, noop = function () {}
Expand Down Expand Up @@ -65,6 +63,11 @@
if (xhr && 'withCredentials' in xhr) {
return xhr
} else if (context[xDomainRequest]) {
var protocolRegExp = /^https?/;
if (window.location.href.match(protocolRegExp)[0] !== o.url.match(protocolRegExp)[0]) {
throw new Error('XDomainRequest: requests must be targeted to the same scheme as the hosting page.')
// As per: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
}
return new XDomainRequest()
} else {
throw new Error('Browser does not support cross-origin requests')
Expand Down Expand Up @@ -126,10 +129,6 @@
}
}

function generalCallback(data) {
lastValue = data
}

function urlappend (url, s) {
return url + (/\?/.test(url) ? '&' : '?') + s
}
Expand All @@ -154,7 +153,9 @@
url = urlappend(url, cbkey + '=' + cbval) // no callback details, add 'em
}

context[cbval] = generalCallback
context[cbval] = function(data) {
script.data = data
}

script.type = 'text/javascript'
script.src = url
Expand All @@ -173,8 +174,7 @@
script.onload = script.onreadystatechange = null
script.onclick && script.onclick()
// Call the user callback with the last value stored and clean up values and scripts.
fn(lastValue)
lastValue = undefined
fn(script.data)
head.removeChild(script)
loaded = 1
}
Expand All @@ -187,7 +187,6 @@
abort: function () {
script.onload = script.onreadystatechange = null
err({}, 'Request is aborted: timeout', {})
lastValue = undefined
head.removeChild(script)
loaded = 1
}
Expand Down
Loading