|
94 | 94 |
|
95 | 95 | // Default way to get an element's href. May be overridden at $.rails.href. |
96 | 96 | href: function(element) { |
97 | | - return element.attr('href'); |
| 97 | + return element[0].href; |
98 | 98 | }, |
99 | 99 |
|
100 | 100 | // Submits "remote" forms and links with ajax |
101 | 101 | handleRemote: function(element) { |
102 | | - var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options; |
| 102 | + var method, url, data, withCredentials, dataType, options; |
103 | 103 |
|
104 | 104 | if (rails.fire(element, 'ajax:before')) { |
105 | | - elCrossDomain = element.data('cross-domain'); |
106 | | - crossDomain = elCrossDomain === undefined ? null : elCrossDomain; |
107 | 105 | withCredentials = element.data('with-credentials') || null; |
108 | 106 | dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType); |
109 | 107 |
|
|
155 | 153 | error: function(xhr, status, error) { |
156 | 154 | element.trigger('ajax:error', [xhr, status, error]); |
157 | 155 | }, |
158 | | - crossDomain: crossDomain |
| 156 | + crossDomain: rails.isCrossDomain(url) |
159 | 157 | }; |
160 | 158 |
|
161 | 159 | // There is no withCredentials for IE6-8 when |
|
175 | 173 | } |
176 | 174 | }, |
177 | 175 |
|
| 176 | + // Determines if the request is a cross domain request. |
| 177 | + isCrossDomain: function(url) { |
| 178 | + var originAnchor = document.createElement("a"); |
| 179 | + originAnchor.href = location.href; |
| 180 | + var urlAnchor = document.createElement("a"); |
| 181 | + |
| 182 | + try { |
| 183 | + urlAnchor.href = url; |
| 184 | + // This is a workaround to a IE bug. |
| 185 | + urlAnchor.href = urlAnchor.href; |
| 186 | + |
| 187 | + // Make sure that the browser parses the URL and that the protocols and hosts match. |
| 188 | + return !urlAnchor.protocol || !urlAnchor.host || |
| 189 | + (originAnchor.protocol + "//" + originAnchor.host !== |
| 190 | + urlAnchor.protocol + "//" + urlAnchor.host); |
| 191 | + } catch (e) { |
| 192 | + // If there is an error parsing the URL, assume it is crossDomain. |
| 193 | + return true; |
| 194 | + } |
| 195 | + }, |
| 196 | + |
178 | 197 | // Handles "data-method" on links such as: |
179 | 198 | // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a> |
180 | 199 | handleMethod: function(link) { |
|
186 | 205 | form = $('<form method="post" action="' + href + '"></form>'), |
187 | 206 | metadataInput = '<input name="_method" value="' + method + '" type="hidden" />'; |
188 | 207 |
|
189 | | - if (csrfParam !== undefined && csrfToken !== undefined) { |
| 208 | + if (csrfParam !== undefined && csrfToken !== undefined && !rails.isCrossDomain(href)) { |
190 | 209 | metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />'; |
191 | 210 | } |
192 | 211 |
|
|
0 commit comments