123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- (function (factory) {
- 'use strict';
- if (typeof define === 'function' && define.amd) {
-
- define(['jquery'], factory);
- } else {
-
- factory(window.jQuery);
- }
- }(function ($) {
- 'use strict';
-
- var counter = 0;
-
-
-
-
-
-
-
-
- $.ajaxTransport('iframe', function (options) {
- if (options.async && (options.type === 'POST' || options.type === 'GET')) {
- var form,
- iframe;
- return {
- send: function (_, completeCallback) {
- form = $('<form style="display:none;"></form>');
- form.attr('accept-charset', options.formAcceptCharset);
-
-
-
-
-
- iframe = $(
- '<iframe src="javascript:false;" name="iframe-transport-' +
- (counter += 1) + '"></iframe>'
- ).bind('load', function () {
- var fileInputClones,
- paramNames = $.isArray(options.paramName) ?
- options.paramName : [options.paramName];
- iframe
- .unbind('load')
- .bind('load', function () {
- var response;
-
-
- try {
- response = iframe.contents();
-
-
-
- if (!response.length || !response[0].firstChild) {
- throw new Error();
- }
- } catch (e) {
- response = undefined;
- }
-
-
- completeCallback(
- 200,
- 'success',
- {'iframe': response}
- );
-
-
- $('<iframe src="javascript:false;"></iframe>')
- .appendTo(form);
- form.remove();
- });
- form
- .prop('target', iframe.prop('name'))
- .prop('action', options.url)
- .prop('method', options.type);
- if (options.formData) {
- $.each(options.formData, function (index, field) {
- $('<input type="hidden"/>')
- .prop('name', field.name)
- .val(field.value)
- .appendTo(form);
- });
- }
- if (options.fileInput && options.fileInput.length &&
- options.type === 'POST') {
- fileInputClones = options.fileInput.clone();
-
- options.fileInput.after(function (index) {
- return fileInputClones[index];
- });
- if (options.paramName) {
- options.fileInput.each(function (index) {
- $(this).prop(
- 'name',
- paramNames[index] || options.paramName
- );
- });
- }
-
-
- form
- .append(options.fileInput)
- .prop('enctype', 'multipart/form-data')
-
- .prop('encoding', 'multipart/form-data');
- }
- form.submit();
-
-
- if (fileInputClones && fileInputClones.length) {
- options.fileInput.each(function (index, input) {
- var clone = $(fileInputClones[index]);
- $(input).prop('name', clone.prop('name'));
- clone.replaceWith(input);
- });
- }
- });
- form.append(iframe).appendTo(document.body);
- },
- abort: function () {
- if (iframe) {
-
-
-
- iframe
- .unbind('load')
- .prop('src', 'javascript'.concat(':false;'));
- }
- if (form) {
- form.remove();
- }
- }
- };
- }
- });
-
-
- $.ajaxSetup({
- converters: {
- 'iframe text': function (iframe) {
- return $(iframe[0].body).text();
- },
- 'iframe json': function (iframe) {
- return $.parseJSON($(iframe[0].body).text());
- },
- 'iframe html': function (iframe) {
- return $(iframe[0].body).html();
- },
- 'iframe script': function (iframe) {
- return $.globalEval($(iframe[0].body).text());
- }
- }
- });
- }));
|