Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 /*
A 2  * jQuery XDomainRequest Transport Plugin 1.1.3
3  * https://github.com/blueimp/jQuery-File-Upload
4  *
5  * Copyright 2011, Sebastian Tschan
6  * https://blueimp.net
7  *
8  * Licensed under the MIT license:
9  * http://www.opensource.org/licenses/MIT
10  *
11  * Based on Julian Aubourg's ajaxHooks xdr.js:
12  * https://github.com/jaubourg/ajaxHooks/
13  */
14
15 /* global define, window, XDomainRequest */
16
17 (function (factory) {
18     'use strict';
19     if (typeof define === 'function' && define.amd) {
20         // Register as an anonymous AMD module:
21         define(['jquery'], factory);
22     } else {
23         // Browser globals:
24         factory(window.jQuery);
25     }
26 }(function ($) {
27     'use strict';
28     if (window.XDomainRequest && !$.support.cors) {
29         $.ajaxTransport(function (s) {
30             if (s.crossDomain && s.async) {
31                 if (s.timeout) {
32                     s.xdrTimeout = s.timeout;
33                     delete s.timeout;
34                 }
35                 var xdr;
36                 return {
37                     send: function (headers, completeCallback) {
38                         var addParamChar = /\?/.test(s.url) ? '&' : '?';
39                         function callback(status, statusText, responses, responseHeaders) {
40                             xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
41                             xdr = null;
42                             completeCallback(status, statusText, responses, responseHeaders);
43                         }
44                         xdr = new XDomainRequest();
45                         // XDomainRequest only supports GET and POST:
46                         if (s.type === 'DELETE') {
47                             s.url = s.url + addParamChar + '_method=DELETE';
48                             s.type = 'POST';
49                         } else if (s.type === 'PUT') {
50                             s.url = s.url + addParamChar + '_method=PUT';
51                             s.type = 'POST';
52                         } else if (s.type === 'PATCH') {
53                             s.url = s.url + addParamChar + '_method=PATCH';
54                             s.type = 'POST';
55                         }
56                         xdr.open(s.type, s.url);
57                         xdr.onload = function () {
58                             callback(
59                                 200,
60                                 'OK',
61                                 {text: xdr.responseText},
62                                 'Content-Type: ' + xdr.contentType
63                             );
64                         };
65                         xdr.onerror = function () {
66                             callback(404, 'Not Found');
67                         };
68                         if (s.xdrTimeout) {
69                             xdr.ontimeout = function () {
70                                 callback(0, 'timeout');
71                             };
72                             xdr.timeout = s.xdrTimeout;
73                         }
74                         xdr.send((s.hasContent && s.data) || null);
75                     },
76                     abort: function () {
77                         if (xdr) {
78                             xdr.onerror = $.noop();
79                             xdr.abort();
80                         }
81                     }
82                 };
83             }
84         });
85     }
86 }));