diff --git a/src/ajax.js b/src/ajax.js index cea2c6c12..95a63fa81 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -184,13 +184,17 @@ $.ajax = function(options){ var settings = $.extend({}, options || {}), - deferred = $.Deferred && $.Deferred() + deferred = $.Deferred && $.Deferred(), + urlAnchor for (key in $.ajaxSettings) if (settings[key] === undefined) settings[key] = $.ajaxSettings[key] ajaxStart(settings) - if (!settings.crossDomain) settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) && - RegExp.$2 != window.location.host + if (!settings.crossDomain) { + urlAnchor = document.createElement('a') + urlAnchor.href = settings.url + settings.crossDomain = (window.location.protocol + '//' + window.location.host) !== (urlAnchor.protocol + '//' + urlAnchor.host) + } if (!settings.url) settings.url = window.location.toString() serializeData(settings) diff --git a/test/ajax.html b/test/ajax.html index 7f0d3df8a..c3afd404b 100644 --- a/test/ajax.html +++ b/test/ajax.html @@ -832,7 +832,7 @@

Zepto Ajax unit tests

overrideMimeType: function(type) { this.responseHeaders['content-type'] = type }, - withCredentials: false, + withCredentials: false, send: function(data) { this.data = data }, @@ -881,7 +881,7 @@

Zepto Ajax unit tests

t.assertEqual(window.location, MockXHR.last.url) }, - testCrossDomain: function(t) { + testCrossDomainCrossOrigin: function(t) { $.ajax({ url: 'http://example.com/foo', beforeSend: function(xhr, settings) { @@ -890,6 +890,60 @@

Zepto Ajax unit tests

}) }, + testCrossDomainSameOrigin: function(t) { + $.ajax({ + url: window.location.href, + beforeSend: function(xhr, settings) { + t.assertFalse(settings.crossDomain) + } + }) + }, + + testCrossDomainLeadingSpace: function(t) { + $.ajax({ + url: ' http://example.com/foo', + beforeSend: function(xhr, settings) { + t.assertTrue(settings.crossDomain) + } + }) + }, + + testCrossDomainTrippleSlash: function(t) { + $.ajax({ + url: 'http:///example.com/foo', + beforeSend: function(xhr, settings) { + t.assertTrue(settings.crossDomain) + } + }) + }, + + testCrossDomainWithQuery: function(t) { + $.ajax({ + url: window.location.href + "?foo=bar", + beforeSend: function(xhr, settings) { + t.assertFalse(settings.crossDomain) + } + }) + }, + + testCrossDomainDifferentScheme: function(t) { + $.ajax({ + url: "foo://" + window.location.host, + beforeSend: function(xhr, settings) { + t.assertTrue(settings.crossDomain) + } + }) + }, + + testCrossDomainPeriodInScheme: function(t) { + $.ajax({ + url: "foo.bar://example.com/foo", + beforeSend: function(xhr, settings) { + t.assertTrue(settings.crossDomain) + } + }) + }, + testDefaultAcceptHeader: function(t) { $.ajax() t.assert(MockXHR.last.headers.some(matchHeader('Accept', '*/*')))