UNPKG

ddd-jquery

Version:

de-bowerified, de-amdified, de-gruntified... jQuery.

69 lines 2.81 kB
var jQuery = require('../core'), support = require('../var/support'), __unnameddep__ = require('../ajax'); jQuery.ajaxSettings.xhr = function () { try { return new XMLHttpRequest(); } catch (e) { } }; var xhrId = 0, xhrCallbacks = {}, xhrSuccessStatus = { 0: 200, 1223: 204 }, xhrSupported = jQuery.ajaxSettings.xhr(); if (window.ActiveXObject) { jQuery(window).on('unload', function () { for (var key in xhrCallbacks) { xhrCallbacks[key](); } }); } support.cors = !!xhrSupported && 'withCredentials' in xhrSupported; support.ajax = xhrSupported = !!xhrSupported; jQuery.ajaxTransport(function (options) { var callback; if (support.cors || xhrSupported && !options.crossDomain) { return { send: function (headers, complete) { var i, xhr = options.xhr(), id = ++xhrId; xhr.open(options.type, options.url, options.async, options.username, options.password); if (options.xhrFields) { for (i in options.xhrFields) { xhr[i] = options.xhrFields[i]; } } if (options.mimeType && xhr.overrideMimeType) { xhr.overrideMimeType(options.mimeType); } if (!options.crossDomain && !headers['X-Requested-With']) { headers['X-Requested-With'] = 'XMLHttpRequest'; } for (i in headers) { xhr.setRequestHeader(i, headers[i]); } callback = function (type) { return function () { if (callback) { delete xhrCallbacks[id]; callback = xhr.onload = xhr.onerror = null; if (type === 'abort') { xhr.abort(); } else if (type === 'error') { complete(xhr.status, xhr.statusText); } else { complete(xhrSuccessStatus[xhr.status] || xhr.status, xhr.statusText, typeof xhr.responseText === 'string' ? { text: xhr.responseText } : undefined, xhr.getAllResponseHeaders()); } } }; }; xhr.onload = callback(); xhr.onerror = callback('error'); callback = xhrCallbacks[id] = callback('abort'); xhr.send(options.hasContent && options.data || null); }, abort: function () { if (callback) { callback(); } } }; } });