silverstripe-framework/thirdparty/jquery-fileupload/cors/jquery.xdr-transport.js

77 lines
2.8 KiB
JavaScript

/*
* jQuery XDomainRequest Transport Plugin 1.0.2
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*
* Based on Julian Aubourg's ajaxHooks xdr.js:
* https://github.com/jaubourg/ajaxHooks/
*/
/*jslint unparam: true */
/*global jQuery, window, XDomainRequest */
(function ($) {
'use strict';
if (window.XDomainRequest) {
$.ajaxTransport(function (s) {
if (s.crossDomain && s.async) {
if (s.timeout) {
s.xdrTimeout = s.timeout;
delete s.timeout;
}
var xdr;
return {
send: function (headers, completeCallback) {
function callback(status, statusText, responses, responseHeaders) {
xdr.onload = xdr.onerror = xdr.ontimeout = jQuery.noop;
xdr = null;
completeCallback(status, statusText, responses, responseHeaders);
}
xdr = new XDomainRequest();
// XDomainRequest only supports GET and POST:
if (s.type === 'DELETE') {
s.url = s.url + (/\?/.test(s.url) ? '&' : '?') +
'_method=DELETE';
s.type = 'POST';
} else if (s.type === 'PUT') {
s.url = s.url + (/\?/.test(s.url) ? '&' : '?') +
'_method=PUT';
s.type = 'POST';
}
xdr.open(s.type, s.url);
xdr.onload = function () {
callback(
200,
'OK',
{text: xdr.responseText},
'Content-Type: ' + xdr.contentType
);
};
xdr.onerror = function () {
callback(404, 'Not Found');
};
if (s.xdrTimeout) {
xdr.ontimeout = function () {
callback(0, 'timeout');
};
xdr.timeout = s.xdrTimeout;
}
xdr.send((s.hasContent && s.data) || null);
},
abort: function () {
if (xdr) {
xdr.onerror = jQuery.noop();
xdr.abort();
}
}
};
}
});
}
}(jQuery));