Projet

Général

Profil

Paste
Télécharger (5,71 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / flexslider-2.5.0 / bower_components / jquery / src / ajax / xhr.js @ 0aee3c58

1
define([
2
        "../core",
3
        "../var/support",
4
        "../ajax"
5
], function( jQuery, support ) {
6

    
7
// Create the request object
8
// (This is still attached to ajaxSettings for backward compatibility)
9
jQuery.ajaxSettings.xhr = window.ActiveXObject !== undefined ?
10
        // Support: IE6+
11
        function() {
12

    
13
                // XHR cannot access local files, always use ActiveX for that case
14
                return !this.isLocal &&
15

    
16
                        // Support: IE7-8
17
                        // oldIE XHR does not support non-RFC2616 methods (#13240)
18
                        // See http://msdn.microsoft.com/en-us/library/ie/ms536648(v=vs.85).aspx
19
                        // and http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9
20
                        // Although this check for six methods instead of eight
21
                        // since IE also does not support "trace" and "connect"
22
                        /^(get|post|head|put|delete|options)$/i.test( this.type ) &&
23

    
24
                        createStandardXHR() || createActiveXHR();
25
        } :
26
        // For all other browsers, use the standard XMLHttpRequest object
27
        createStandardXHR;
28

    
29
var xhrId = 0,
30
        xhrCallbacks = {},
31
        xhrSupported = jQuery.ajaxSettings.xhr();
32

    
33
// Support: IE<10
34
// Open requests must be manually aborted on unload (#5280)
35
// See https://support.microsoft.com/kb/2856746 for more info
36
if ( window.attachEvent ) {
37
        window.attachEvent( "onunload", function() {
38
                for ( var key in xhrCallbacks ) {
39
                        xhrCallbacks[ key ]( undefined, true );
40
                }
41
        });
42
}
43

    
44
// Determine support properties
45
support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
46
xhrSupported = support.ajax = !!xhrSupported;
47

    
48
// Create transport if the browser can provide an xhr
49
if ( xhrSupported ) {
50

    
51
        jQuery.ajaxTransport(function( options ) {
52
                // Cross domain only allowed if supported through XMLHttpRequest
53
                if ( !options.crossDomain || support.cors ) {
54

    
55
                        var callback;
56

    
57
                        return {
58
                                send: function( headers, complete ) {
59
                                        var i,
60
                                                xhr = options.xhr(),
61
                                                id = ++xhrId;
62

    
63
                                        // Open the socket
64
                                        xhr.open( options.type, options.url, options.async, options.username, options.password );
65

    
66
                                        // Apply custom fields if provided
67
                                        if ( options.xhrFields ) {
68
                                                for ( i in options.xhrFields ) {
69
                                                        xhr[ i ] = options.xhrFields[ i ];
70
                                                }
71
                                        }
72

    
73
                                        // Override mime type if needed
74
                                        if ( options.mimeType && xhr.overrideMimeType ) {
75
                                                xhr.overrideMimeType( options.mimeType );
76
                                        }
77

    
78
                                        // X-Requested-With header
79
                                        // For cross-domain requests, seeing as conditions for a preflight are
80
                                        // akin to a jigsaw puzzle, we simply never set it to be sure.
81
                                        // (it can always be set on a per-request basis or even using ajaxSetup)
82
                                        // For same-domain requests, won't change header if already provided.
83
                                        if ( !options.crossDomain && !headers["X-Requested-With"] ) {
84
                                                headers["X-Requested-With"] = "XMLHttpRequest";
85
                                        }
86

    
87
                                        // Set headers
88
                                        for ( i in headers ) {
89
                                                // Support: IE<9
90
                                                // IE's ActiveXObject throws a 'Type Mismatch' exception when setting
91
                                                // request header to a null-value.
92
                                                //
93
                                                // To keep consistent with other XHR implementations, cast the value
94
                                                // to string and ignore `undefined`.
95
                                                if ( headers[ i ] !== undefined ) {
96
                                                        xhr.setRequestHeader( i, headers[ i ] + "" );
97
                                                }
98
                                        }
99

    
100
                                        // Do send the request
101
                                        // This may raise an exception which is actually
102
                                        // handled in jQuery.ajax (so no try/catch here)
103
                                        xhr.send( ( options.hasContent && options.data ) || null );
104

    
105
                                        // Listener
106
                                        callback = function( _, isAbort ) {
107
                                                var status, statusText, responses;
108

    
109
                                                // Was never called and is aborted or complete
110
                                                if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
111
                                                        // Clean up
112
                                                        delete xhrCallbacks[ id ];
113
                                                        callback = undefined;
114
                                                        xhr.onreadystatechange = jQuery.noop;
115

    
116
                                                        // Abort manually if needed
117
                                                        if ( isAbort ) {
118
                                                                if ( xhr.readyState !== 4 ) {
119
                                                                        xhr.abort();
120
                                                                }
121
                                                        } else {
122
                                                                responses = {};
123
                                                                status = xhr.status;
124

    
125
                                                                // Support: IE<10
126
                                                                // Accessing binary-data responseText throws an exception
127
                                                                // (#11426)
128
                                                                if ( typeof xhr.responseText === "string" ) {
129
                                                                        responses.text = xhr.responseText;
130
                                                                }
131

    
132
                                                                // Firefox throws an exception when accessing
133
                                                                // statusText for faulty cross-domain requests
134
                                                                try {
135
                                                                        statusText = xhr.statusText;
136
                                                                } catch( e ) {
137
                                                                        // We normalize with Webkit giving an empty statusText
138
                                                                        statusText = "";
139
                                                                }
140

    
141
                                                                // Filter status for non standard behaviors
142

    
143
                                                                // If the request is local and we have data: assume a success
144
                                                                // (success with no data won't get notified, that's the best we
145
                                                                // can do given current implementations)
146
                                                                if ( !status && options.isLocal && !options.crossDomain ) {
147
                                                                        status = responses.text ? 200 : 404;
148
                                                                // IE - #1450: sometimes returns 1223 when it should be 204
149
                                                                } else if ( status === 1223 ) {
150
                                                                        status = 204;
151
                                                                }
152
                                                        }
153
                                                }
154

    
155
                                                // Call complete if needed
156
                                                if ( responses ) {
157
                                                        complete( status, statusText, responses, xhr.getAllResponseHeaders() );
158
                                                }
159
                                        };
160

    
161
                                        if ( !options.async ) {
162
                                                // if we're in sync mode we fire the callback
163
                                                callback();
164
                                        } else if ( xhr.readyState === 4 ) {
165
                                                // (IE6 & IE7) if it's in cache and has been
166
                                                // retrieved directly we need to fire the callback
167
                                                setTimeout( callback );
168
                                        } else {
169
                                                // Add to the list of active xhr callbacks
170
                                                xhr.onreadystatechange = xhrCallbacks[ id ] = callback;
171
                                        }
172
                                },
173

    
174
                                abort: function() {
175
                                        if ( callback ) {
176
                                                callback( undefined, true );
177
                                        }
178
                                }
179
                        };
180
                }
181
        });
182
}
183

    
184
// Functions to create xhrs
185
function createStandardXHR() {
186
        try {
187
                return new window.XMLHttpRequest();
188
        } catch( e ) {}
189
}
190

    
191
function createActiveXHR() {
192
        try {
193
                return new window.ActiveXObject( "Microsoft.XMLHTTP" );
194
        } catch( e ) {}
195
}
196

    
197
});