scratch – Diff between revs 58 and 125
?pathlinks?
Rev 58 | Rev 125 | |||
---|---|---|---|---|
1 | define( [ |
1 | define([ |
|
2 | "./core", |
2 | "./core", |
|
3 | "./var/document", |
- | ||
4 | "./var/rnothtmlwhite", |
3 | "./var/rnotwhite", |
|
5 | "./ajax/var/location", |
- | ||
6 | "./ajax/var/nonce", |
4 | "./ajax/var/nonce", |
|
7 | "./ajax/var/rquery", |
5 | "./ajax/var/rquery", |
|
8 | |
- | ||
9 | "./core/init", |
6 | "./core/init", |
|
- | 7 | "./ajax/parseJSON", |
||
10 | "./ajax/parseXML", |
8 | "./ajax/parseXML", |
|
11 | "./event/trigger", |
- | ||
12 | "./deferred", |
9 | "./deferred" |
|
13 | "./serialize" // jQuery.param |
- | ||
14 | ], function( jQuery, document, rnothtmlwhite, location, nonce, rquery ) { |
10 | ], function( jQuery, rnotwhite, nonce, rquery ) { |
|
15 | |
- | ||
16 | "use strict"; |
- | ||
17 | |
11 | |
|
18 | var |
- | ||
19 | r20 = /%20/g, |
12 | var |
|
20 | rhash = /#.*$/, |
13 | rhash = /#.*$/, |
|
21 | rantiCache = /([?&])_=[^&]*/, |
14 | rts = /([?&])_=[^&]*/, |
|
22 | rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg, |
- | ||
23 | |
15 | rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg, |
|
24 | // #7653, #8125, #8152: local protocol detection |
16 | // #7653, #8125, #8152: local protocol detection |
|
25 | rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/, |
17 | rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/, |
|
26 | rnoContent = /^(?:GET|HEAD)$/, |
18 | rnoContent = /^(?:GET|HEAD)$/, |
|
27 | rprotocol = /^\/\//, |
19 | rprotocol = /^\/\//, |
|
- | 20 | rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/, |
||
28 | |
21 | |
|
29 | /* Prefilters |
22 | /* Prefilters |
|
30 | * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example) |
23 | * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example) |
|
31 | * 2) These are called: |
24 | * 2) These are called: |
|
32 | * - BEFORE asking for a transport |
25 | * - BEFORE asking for a transport |
|
33 | * - AFTER param serialization (s.data is a string if s.processData is true) |
26 | * - AFTER param serialization (s.data is a string if s.processData is true) |
|
34 | * 3) key is the dataType |
27 | * 3) key is the dataType |
|
35 | * 4) the catchall symbol "*" can be used |
28 | * 4) the catchall symbol "*" can be used |
|
36 | * 5) execution will start with transport dataType and THEN continue down to "*" if needed |
29 | * 5) execution will start with transport dataType and THEN continue down to "*" if needed |
|
37 | */ |
30 | */ |
|
38 | prefilters = {}, |
31 | prefilters = {}, |
|
39 | |
32 | |
|
40 | /* Transports bindings |
33 | /* Transports bindings |
|
41 | * 1) key is the dataType |
34 | * 1) key is the dataType |
|
42 | * 2) the catchall symbol "*" can be used |
35 | * 2) the catchall symbol "*" can be used |
|
43 | * 3) selection will start with transport dataType and THEN go to "*" if needed |
36 | * 3) selection will start with transport dataType and THEN go to "*" if needed |
|
44 | */ |
37 | */ |
|
45 | transports = {}, |
38 | transports = {}, |
|
46 | |
39 | |
|
47 | // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression |
40 | // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression |
|
48 | allTypes = "*/".concat( "*" ), |
41 | allTypes = "*/".concat( "*" ), |
|
- | 42 | |
||
49 | |
43 | // Document location |
|
- | 44 | ajaxLocation = window.location.href, |
||
50 | // Anchor tag for parsing the document origin |
45 | |
|
51 | originAnchor = document.createElement( "a" ); |
46 | // Segment location into parts |
|
52 | originAnchor.href = location.href; |
47 | ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || []; |
|
53 | |
48 | |
|
54 | // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport |
49 | // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport |
|
55 | function addToPrefiltersOrTransports( structure ) { |
50 | function addToPrefiltersOrTransports( structure ) { |
|
56 | |
51 | |
|
57 | // dataTypeExpression is optional and defaults to "*" |
52 | // dataTypeExpression is optional and defaults to "*" |
|
58 | return function( dataTypeExpression, func ) { |
53 | return function( dataTypeExpression, func ) { |
|
59 | |
54 | |
|
60 | if ( typeof dataTypeExpression !== "string" ) { |
55 | if ( typeof dataTypeExpression !== "string" ) { |
|
61 | func = dataTypeExpression; |
56 | func = dataTypeExpression; |
|
62 | dataTypeExpression = "*"; |
57 | dataTypeExpression = "*"; |
|
63 | } |
58 | } |
|
64 | |
59 | |
|
65 | var dataType, |
60 | var dataType, |
|
66 | i = 0, |
61 | i = 0, |
|
67 | dataTypes = dataTypeExpression.toLowerCase().match( rnothtmlwhite ) || []; |
62 | dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || []; |
|
68 | |
63 | |
|
69 | if ( jQuery.isFunction( func ) ) { |
- | ||
70 | |
64 | if ( jQuery.isFunction( func ) ) { |
|
71 | // For each dataType in the dataTypeExpression |
65 | // For each dataType in the dataTypeExpression |
|
72 | while ( ( dataType = dataTypes[ i++ ] ) ) { |
- | ||
73 | |
66 | while ( (dataType = dataTypes[i++]) ) { |
|
74 | // Prepend if requested |
67 | // Prepend if requested |
|
75 | if ( dataType[ 0 ] === "+" ) { |
68 | if ( dataType[0] === "+" ) { |
|
76 | dataType = dataType.slice( 1 ) || "*"; |
69 | dataType = dataType.slice( 1 ) || "*"; |
|
77 | ( structure[ dataType ] = structure[ dataType ] || [] ).unshift( func ); |
70 | (structure[ dataType ] = structure[ dataType ] || []).unshift( func ); |
|
78 | |
71 | |
|
79 | // Otherwise append |
72 | // Otherwise append |
|
80 | } else { |
73 | } else { |
|
81 | ( structure[ dataType ] = structure[ dataType ] || [] ).push( func ); |
74 | (structure[ dataType ] = structure[ dataType ] || []).push( func ); |
|
82 | } |
75 | } |
|
83 | } |
76 | } |
|
84 | } |
77 | } |
|
85 | }; |
78 | }; |
|
86 | } |
79 | } |
|
87 | |
80 | |
|
88 | // Base inspection function for prefilters and transports |
81 | // Base inspection function for prefilters and transports |
|
89 | function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) { |
82 | function inspectPrefiltersOrTransports( structure, options, originalOptions, jqXHR ) { |
|
90 | |
83 | |
|
91 | var inspected = {}, |
84 | var inspected = {}, |
|
92 | seekingTransport = ( structure === transports ); |
85 | seekingTransport = ( structure === transports ); |
|
93 | |
86 | |
|
94 | function inspect( dataType ) { |
87 | function inspect( dataType ) { |
|
95 | var selected; |
88 | var selected; |
|
96 | inspected[ dataType ] = true; |
89 | inspected[ dataType ] = true; |
|
97 | jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) { |
90 | jQuery.each( structure[ dataType ] || [], function( _, prefilterOrFactory ) { |
|
98 | var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR ); |
91 | var dataTypeOrTransport = prefilterOrFactory( options, originalOptions, jqXHR ); |
|
99 | if ( typeof dataTypeOrTransport === "string" && |
- | ||
100 | !seekingTransport && !inspected[ dataTypeOrTransport ] ) { |
92 | if ( typeof dataTypeOrTransport === "string" && !seekingTransport && !inspected[ dataTypeOrTransport ] ) { |
|
101 | |
- | ||
102 | options.dataTypes.unshift( dataTypeOrTransport ); |
93 | options.dataTypes.unshift( dataTypeOrTransport ); |
|
103 | inspect( dataTypeOrTransport ); |
94 | inspect( dataTypeOrTransport ); |
|
104 | return false; |
95 | return false; |
|
105 | } else if ( seekingTransport ) { |
96 | } else if ( seekingTransport ) { |
|
106 | return !( selected = dataTypeOrTransport ); |
97 | return !( selected = dataTypeOrTransport ); |
|
107 | } |
98 | } |
|
108 | } ); |
99 | }); |
|
109 | return selected; |
100 | return selected; |
|
110 | } |
101 | } |
|
111 | |
102 | |
|
112 | return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" ); |
103 | return inspect( options.dataTypes[ 0 ] ) || !inspected[ "*" ] && inspect( "*" ); |
|
113 | } |
104 | } |
|
114 | |
105 | |
|
115 | // A special extend for ajax options |
106 | // A special extend for ajax options |
|
116 | // that takes "flat" options (not to be deep extended) |
107 | // that takes "flat" options (not to be deep extended) |
|
117 | // Fixes #9887 |
108 | // Fixes #9887 |
|
118 | function ajaxExtend( target, src ) { |
109 | function ajaxExtend( target, src ) { |
|
119 | var key, deep, |
110 | var key, deep, |
|
120 | flatOptions = jQuery.ajaxSettings.flatOptions || {}; |
111 | flatOptions = jQuery.ajaxSettings.flatOptions || {}; |
|
121 | |
112 | |
|
122 | for ( key in src ) { |
113 | for ( key in src ) { |
|
123 | if ( src[ key ] !== undefined ) { |
114 | if ( src[ key ] !== undefined ) { |
|
124 | ( flatOptions[ key ] ? target : ( deep || ( deep = {} ) ) )[ key ] = src[ key ]; |
115 | ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ]; |
|
125 | } |
116 | } |
|
126 | } |
117 | } |
|
127 | if ( deep ) { |
118 | if ( deep ) { |
|
128 | jQuery.extend( true, target, deep ); |
119 | jQuery.extend( true, target, deep ); |
|
129 | } |
120 | } |
|
130 | |
121 | |
|
131 | return target; |
122 | return target; |
|
132 | } |
123 | } |
|
133 | |
124 | |
|
134 | /* Handles responses to an ajax request: |
125 | /* Handles responses to an ajax request: |
|
135 | * - finds the right dataType (mediates between content-type and expected dataType) |
126 | * - finds the right dataType (mediates between content-type and expected dataType) |
|
136 | * - returns the corresponding response |
127 | * - returns the corresponding response |
|
137 | */ |
128 | */ |
|
138 | function ajaxHandleResponses( s, jqXHR, responses ) { |
129 | function ajaxHandleResponses( s, jqXHR, responses ) { |
|
139 | |
130 | |
|
140 | var ct, type, finalDataType, firstDataType, |
131 | var ct, type, finalDataType, firstDataType, |
|
141 | contents = s.contents, |
132 | contents = s.contents, |
|
142 | dataTypes = s.dataTypes; |
133 | dataTypes = s.dataTypes; |
|
143 | |
134 | |
|
144 | // Remove auto dataType and get content-type in the process |
135 | // Remove auto dataType and get content-type in the process |
|
145 | while ( dataTypes[ 0 ] === "*" ) { |
136 | while ( dataTypes[ 0 ] === "*" ) { |
|
146 | dataTypes.shift(); |
137 | dataTypes.shift(); |
|
147 | if ( ct === undefined ) { |
138 | if ( ct === undefined ) { |
|
148 | ct = s.mimeType || jqXHR.getResponseHeader( "Content-Type" ); |
139 | ct = s.mimeType || jqXHR.getResponseHeader("Content-Type"); |
|
149 | } |
140 | } |
|
150 | } |
141 | } |
|
151 | |
142 | |
|
152 | // Check if we're dealing with a known content-type |
143 | // Check if we're dealing with a known content-type |
|
153 | if ( ct ) { |
144 | if ( ct ) { |
|
154 | for ( type in contents ) { |
145 | for ( type in contents ) { |
|
155 | if ( contents[ type ] && contents[ type ].test( ct ) ) { |
146 | if ( contents[ type ] && contents[ type ].test( ct ) ) { |
|
156 | dataTypes.unshift( type ); |
147 | dataTypes.unshift( type ); |
|
157 | break; |
148 | break; |
|
158 | } |
149 | } |
|
159 | } |
150 | } |
|
160 | } |
151 | } |
|
161 | |
152 | |
|
162 | // Check to see if we have a response for the expected dataType |
153 | // Check to see if we have a response for the expected dataType |
|
163 | if ( dataTypes[ 0 ] in responses ) { |
154 | if ( dataTypes[ 0 ] in responses ) { |
|
164 | finalDataType = dataTypes[ 0 ]; |
155 | finalDataType = dataTypes[ 0 ]; |
|
165 | } else { |
156 | } else { |
|
166 | |
- | ||
167 | // Try convertible dataTypes |
157 | // Try convertible dataTypes |
|
168 | for ( type in responses ) { |
158 | for ( type in responses ) { |
|
169 | if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[ 0 ] ] ) { |
159 | if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) { |
|
170 | finalDataType = type; |
160 | finalDataType = type; |
|
171 | break; |
161 | break; |
|
172 | } |
162 | } |
|
173 | if ( !firstDataType ) { |
163 | if ( !firstDataType ) { |
|
174 | firstDataType = type; |
164 | firstDataType = type; |
|
175 | } |
165 | } |
|
176 | } |
166 | } |
|
177 | |
- | ||
178 | // Or just use first one |
167 | // Or just use first one |
|
179 | finalDataType = finalDataType || firstDataType; |
168 | finalDataType = finalDataType || firstDataType; |
|
180 | } |
169 | } |
|
181 | |
170 | |
|
182 | // If we found a dataType |
171 | // If we found a dataType |
|
183 | // We add the dataType to the list if needed |
172 | // We add the dataType to the list if needed |
|
184 | // and return the corresponding response |
173 | // and return the corresponding response |
|
185 | if ( finalDataType ) { |
174 | if ( finalDataType ) { |
|
186 | if ( finalDataType !== dataTypes[ 0 ] ) { |
175 | if ( finalDataType !== dataTypes[ 0 ] ) { |
|
187 | dataTypes.unshift( finalDataType ); |
176 | dataTypes.unshift( finalDataType ); |
|
188 | } |
177 | } |
|
189 | return responses[ finalDataType ]; |
178 | return responses[ finalDataType ]; |
|
190 | } |
179 | } |
|
191 | } |
180 | } |
|
192 | |
181 | |
|
193 | /* Chain conversions given the request and the original response |
182 | /* Chain conversions given the request and the original response |
|
194 | * Also sets the responseXXX fields on the jqXHR instance |
183 | * Also sets the responseXXX fields on the jqXHR instance |
|
195 | */ |
184 | */ |
|
196 | function ajaxConvert( s, response, jqXHR, isSuccess ) { |
185 | function ajaxConvert( s, response, jqXHR, isSuccess ) { |
|
197 | var conv2, current, conv, tmp, prev, |
186 | var conv2, current, conv, tmp, prev, |
|
198 | converters = {}, |
187 | converters = {}, |
|
199 | |
- | ||
200 | // Work with a copy of dataTypes in case we need to modify it for conversion |
188 | // Work with a copy of dataTypes in case we need to modify it for conversion |
|
201 | dataTypes = s.dataTypes.slice(); |
189 | dataTypes = s.dataTypes.slice(); |
|
202 | |
190 | |
|
203 | // Create converters map with lowercased keys |
191 | // Create converters map with lowercased keys |
|
204 | if ( dataTypes[ 1 ] ) { |
192 | if ( dataTypes[ 1 ] ) { |
|
205 | for ( conv in s.converters ) { |
193 | for ( conv in s.converters ) { |
|
206 | converters[ conv.toLowerCase() ] = s.converters[ conv ]; |
194 | converters[ conv.toLowerCase() ] = s.converters[ conv ]; |
|
207 | } |
195 | } |
|
208 | } |
196 | } |
|
209 | |
197 | |
|
210 | current = dataTypes.shift(); |
198 | current = dataTypes.shift(); |
|
211 | |
199 | |
|
212 | // Convert to each sequential dataType |
200 | // Convert to each sequential dataType |
|
213 | while ( current ) { |
201 | while ( current ) { |
|
214 | |
202 | |
|
215 | if ( s.responseFields[ current ] ) { |
203 | if ( s.responseFields[ current ] ) { |
|
216 | jqXHR[ s.responseFields[ current ] ] = response; |
204 | jqXHR[ s.responseFields[ current ] ] = response; |
|
217 | } |
205 | } |
|
218 | |
206 | |
|
219 | // Apply the dataFilter if provided |
207 | // Apply the dataFilter if provided |
|
220 | if ( !prev && isSuccess && s.dataFilter ) { |
208 | if ( !prev && isSuccess && s.dataFilter ) { |
|
221 | response = s.dataFilter( response, s.dataType ); |
209 | response = s.dataFilter( response, s.dataType ); |
|
222 | } |
210 | } |
|
223 | |
211 | |
|
224 | prev = current; |
212 | prev = current; |
|
225 | current = dataTypes.shift(); |
213 | current = dataTypes.shift(); |
|
226 | |
214 | |
|
227 | if ( current ) { |
215 | if ( current ) { |
|
228 | |
216 | |
|
229 | // There's only work to do if current dataType is non-auto |
217 | // There's only work to do if current dataType is non-auto |
|
230 | if ( current === "*" ) { |
218 | if ( current === "*" ) { |
|
231 | |
219 | |
|
232 | current = prev; |
220 | current = prev; |
|
233 | |
221 | |
|
234 | // Convert response if prev dataType is non-auto and differs from current |
222 | // Convert response if prev dataType is non-auto and differs from current |
|
235 | } else if ( prev !== "*" && prev !== current ) { |
223 | } else if ( prev !== "*" && prev !== current ) { |
|
236 | |
224 | |
|
237 | // Seek a direct converter |
225 | // Seek a direct converter |
|
238 | conv = converters[ prev + " " + current ] || converters[ "* " + current ]; |
226 | conv = converters[ prev + " " + current ] || converters[ "* " + current ]; |
|
239 | |
227 | |
|
240 | // If none found, seek a pair |
228 | // If none found, seek a pair |
|
241 | if ( !conv ) { |
229 | if ( !conv ) { |
|
242 | for ( conv2 in converters ) { |
230 | for ( conv2 in converters ) { |
|
243 | |
231 | |
|
244 | // If conv2 outputs current |
232 | // If conv2 outputs current |
|
245 | tmp = conv2.split( " " ); |
233 | tmp = conv2.split( " " ); |
|
246 | if ( tmp[ 1 ] === current ) { |
234 | if ( tmp[ 1 ] === current ) { |
|
247 | |
235 | |
|
248 | // If prev can be converted to accepted input |
236 | // If prev can be converted to accepted input |
|
249 | conv = converters[ prev + " " + tmp[ 0 ] ] || |
237 | conv = converters[ prev + " " + tmp[ 0 ] ] || |
|
250 | converters[ "* " + tmp[ 0 ] ]; |
238 | converters[ "* " + tmp[ 0 ] ]; |
|
251 | if ( conv ) { |
239 | if ( conv ) { |
|
252 | |
- | ||
253 | // Condense equivalence converters |
240 | // Condense equivalence converters |
|
254 | if ( conv === true ) { |
241 | if ( conv === true ) { |
|
255 | conv = converters[ conv2 ]; |
242 | conv = converters[ conv2 ]; |
|
256 | |
243 | |
|
257 | // Otherwise, insert the intermediate dataType |
244 | // Otherwise, insert the intermediate dataType |
|
258 | } else if ( converters[ conv2 ] !== true ) { |
245 | } else if ( converters[ conv2 ] !== true ) { |
|
259 | current = tmp[ 0 ]; |
246 | current = tmp[ 0 ]; |
|
260 | dataTypes.unshift( tmp[ 1 ] ); |
247 | dataTypes.unshift( tmp[ 1 ] ); |
|
261 | } |
248 | } |
|
262 | break; |
249 | break; |
|
263 | } |
250 | } |
|
264 | } |
251 | } |
|
265 | } |
252 | } |
|
266 | } |
253 | } |
|
267 | |
254 | |
|
268 | // Apply converter (if not an equivalence) |
255 | // Apply converter (if not an equivalence) |
|
269 | if ( conv !== true ) { |
256 | if ( conv !== true ) { |
|
270 | |
257 | |
|
271 | // Unless errors are allowed to bubble, catch and return them |
258 | // Unless errors are allowed to bubble, catch and return them |
|
272 | if ( conv && s.throws ) { |
259 | if ( conv && s[ "throws" ] ) { |
|
273 | response = conv( response ); |
260 | response = conv( response ); |
|
274 | } else { |
261 | } else { |
|
275 | try { |
262 | try { |
|
276 | response = conv( response ); |
263 | response = conv( response ); |
|
277 | } catch ( e ) { |
264 | } catch ( e ) { |
|
278 | return { |
- | ||
279 | state: "parsererror", |
- | ||
280 | error: conv ? e : "No conversion from " + prev + " to " + current |
265 | return { state: "parsererror", error: conv ? e : "No conversion from " + prev + " to " + current }; |
|
281 | }; |
- | ||
282 | } |
266 | } |
|
283 | } |
267 | } |
|
284 | } |
268 | } |
|
285 | } |
269 | } |
|
286 | } |
270 | } |
|
287 | } |
271 | } |
|
288 | |
272 | |
|
289 | return { state: "success", data: response }; |
273 | return { state: "success", data: response }; |
|
290 | } |
274 | } |
|
291 | |
275 | |
|
292 | jQuery.extend( { |
276 | jQuery.extend({ |
|
293 | |
277 | |
|
294 | // Counter for holding the number of active queries |
278 | // Counter for holding the number of active queries |
|
295 | active: 0, |
279 | active: 0, |
|
296 | |
280 | |
|
297 | // Last-Modified header cache for next request |
281 | // Last-Modified header cache for next request |
|
298 | lastModified: {}, |
282 | lastModified: {}, |
|
299 | etag: {}, |
283 | etag: {}, |
|
300 | |
284 | |
|
301 | ajaxSettings: { |
285 | ajaxSettings: { |
|
302 | url: location.href, |
286 | url: ajaxLocation, |
|
303 | type: "GET", |
287 | type: "GET", |
|
304 | isLocal: rlocalProtocol.test( location.protocol ), |
288 | isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ), |
|
305 | global: true, |
289 | global: true, |
|
306 | processData: true, |
290 | processData: true, |
|
307 | async: true, |
291 | async: true, |
|
308 | contentType: "application/x-www-form-urlencoded; charset=UTF-8", |
292 | contentType: "application/x-www-form-urlencoded; charset=UTF-8", |
|
309 | |
- | ||
310 | /* |
293 | /* |
|
311 | timeout: 0, |
294 | timeout: 0, |
|
312 | data: null, |
295 | data: null, |
|
313 | dataType: null, |
296 | dataType: null, |
|
314 | username: null, |
297 | username: null, |
|
315 | password: null, |
298 | password: null, |
|
316 | cache: null, |
299 | cache: null, |
|
317 | throws: false, |
300 | throws: false, |
|
318 | traditional: false, |
301 | traditional: false, |
|
319 | headers: {}, |
302 | headers: {}, |
|
320 | */ |
303 | */ |
|
321 | |
304 | |
|
322 | accepts: { |
305 | accepts: { |
|
323 | "*": allTypes, |
306 | "*": allTypes, |
|
324 | text: "text/plain", |
307 | text: "text/plain", |
|
325 | html: "text/html", |
308 | html: "text/html", |
|
326 | xml: "application/xml, text/xml", |
309 | xml: "application/xml, text/xml", |
|
327 | json: "application/json, text/javascript" |
310 | json: "application/json, text/javascript" |
|
328 | }, |
311 | }, |
|
329 | |
312 | |
|
330 | contents: { |
313 | contents: { |
|
331 | xml: /\bxml\b/, |
314 | xml: /xml/, |
|
332 | html: /\bhtml/, |
315 | html: /html/, |
|
333 | json: /\bjson\b/ |
316 | json: /json/ |
|
334 | }, |
317 | }, |
|
335 | |
318 | |
|
336 | responseFields: { |
319 | responseFields: { |
|
337 | xml: "responseXML", |
320 | xml: "responseXML", |
|
338 | text: "responseText", |
321 | text: "responseText", |
|
339 | json: "responseJSON" |
322 | json: "responseJSON" |
|
340 | }, |
323 | }, |
|
341 | |
324 | |
|
342 | // Data converters |
325 | // Data converters |
|
343 | // Keys separate source (or catchall "*") and destination types with a single space |
326 | // Keys separate source (or catchall "*") and destination types with a single space |
|
344 | converters: { |
327 | converters: { |
|
345 | |
328 | |
|
346 | // Convert anything to text |
329 | // Convert anything to text |
|
347 | "* text": String, |
330 | "* text": String, |
|
348 | |
331 | |
|
349 | // Text to html (true = no transformation) |
332 | // Text to html (true = no transformation) |
|
350 | "text html": true, |
333 | "text html": true, |
|
351 | |
334 | |
|
352 | // Evaluate text as a json expression |
335 | // Evaluate text as a json expression |
|
353 | "text json": JSON.parse, |
336 | "text json": jQuery.parseJSON, |
|
354 | |
337 | |
|
355 | // Parse text as xml |
338 | // Parse text as xml |
|
356 | "text xml": jQuery.parseXML |
339 | "text xml": jQuery.parseXML |
|
357 | }, |
340 | }, |
|
358 | |
341 | |
|
359 | // For options that shouldn't be deep extended: |
342 | // For options that shouldn't be deep extended: |
|
360 | // you can add your own custom options here if |
343 | // you can add your own custom options here if |
|
361 | // and when you create one that shouldn't be |
344 | // and when you create one that shouldn't be |
|
362 | // deep extended (see ajaxExtend) |
345 | // deep extended (see ajaxExtend) |
|
363 | flatOptions: { |
346 | flatOptions: { |
|
364 | url: true, |
347 | url: true, |
|
365 | context: true |
348 | context: true |
|
366 | } |
349 | } |
|
367 | }, |
350 | }, |
|
368 | |
351 | |
|
369 | // Creates a full fledged settings object into target |
352 | // Creates a full fledged settings object into target |
|
370 | // with both ajaxSettings and settings fields. |
353 | // with both ajaxSettings and settings fields. |
|
371 | // If target is omitted, writes into ajaxSettings. |
354 | // If target is omitted, writes into ajaxSettings. |
|
372 | ajaxSetup: function( target, settings ) { |
355 | ajaxSetup: function( target, settings ) { |
|
373 | return settings ? |
356 | return settings ? |
|
374 | |
357 | |
|
375 | // Building a settings object |
358 | // Building a settings object |
|
376 | ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) : |
359 | ajaxExtend( ajaxExtend( target, jQuery.ajaxSettings ), settings ) : |
|
377 | |
360 | |
|
378 | // Extending ajaxSettings |
361 | // Extending ajaxSettings |
|
379 | ajaxExtend( jQuery.ajaxSettings, target ); |
362 | ajaxExtend( jQuery.ajaxSettings, target ); |
|
380 | }, |
363 | }, |
|
381 | |
364 | |
|
382 | ajaxPrefilter: addToPrefiltersOrTransports( prefilters ), |
365 | ajaxPrefilter: addToPrefiltersOrTransports( prefilters ), |
|
383 | ajaxTransport: addToPrefiltersOrTransports( transports ), |
366 | ajaxTransport: addToPrefiltersOrTransports( transports ), |
|
384 | |
367 | |
|
385 | // Main method |
368 | // Main method |
|
386 | ajax: function( url, options ) { |
369 | ajax: function( url, options ) { |
|
387 | |
370 | |
|
388 | // If url is an object, simulate pre-1.5 signature |
371 | // If url is an object, simulate pre-1.5 signature |
|
389 | if ( typeof url === "object" ) { |
372 | if ( typeof url === "object" ) { |
|
390 | options = url; |
373 | options = url; |
|
391 | url = undefined; |
374 | url = undefined; |
|
392 | } |
375 | } |
|
393 | |
376 | |
|
394 | // Force options to be an object |
377 | // Force options to be an object |
|
395 | options = options || {}; |
378 | options = options || {}; |
|
396 | |
379 | |
|
397 | var transport, |
380 | var transport, |
|
398 | |
- | ||
399 | // URL without anti-cache param |
381 | // URL without anti-cache param |
|
400 | cacheURL, |
382 | cacheURL, |
|
401 | |
- | ||
402 | // Response headers |
383 | // Response headers |
|
403 | responseHeadersString, |
384 | responseHeadersString, |
|
404 | responseHeaders, |
385 | responseHeaders, |
|
405 | |
- | ||
406 | // timeout handle |
386 | // timeout handle |
|
407 | timeoutTimer, |
387 | timeoutTimer, |
|
408 | |
- | ||
409 | // Url cleanup var |
388 | // Cross-domain detection vars |
|
410 | urlAnchor, |
- | ||
411 | |
- | ||
412 | // Request state (becomes false upon send and true upon completion) |
- | ||
413 | completed, |
389 | parts, |
|
414 | |
- | ||
415 | // To know if global events are to be dispatched |
390 | // To know if global events are to be dispatched |
|
416 | fireGlobals, |
391 | fireGlobals, |
|
417 | |
- | ||
418 | // Loop variable |
392 | // Loop variable |
|
419 | i, |
393 | i, |
|
420 | |
- | ||
421 | // uncached part of the url |
- | ||
422 | uncached, |
- | ||
423 | |
- | ||
424 | // Create the final options object |
394 | // Create the final options object |
|
425 | s = jQuery.ajaxSetup( {}, options ), |
395 | s = jQuery.ajaxSetup( {}, options ), |
|
426 | |
- | ||
427 | // Callbacks context |
396 | // Callbacks context |
|
428 | callbackContext = s.context || s, |
397 | callbackContext = s.context || s, |
|
429 | |
- | ||
430 | // Context for global events is callbackContext if it is a DOM node or jQuery collection |
398 | // Context for global events is callbackContext if it is a DOM node or jQuery collection |
|
431 | globalEventContext = s.context && |
- | ||
432 | ( callbackContext.nodeType || callbackContext.jquery ) ? |
399 | globalEventContext = s.context && ( callbackContext.nodeType || callbackContext.jquery ) ? |
|
433 | jQuery( callbackContext ) : |
400 | jQuery( callbackContext ) : |
|
434 | jQuery.event, |
401 | jQuery.event, |
|
435 | |
- | ||
436 | // Deferreds |
402 | // Deferreds |
|
437 | deferred = jQuery.Deferred(), |
403 | deferred = jQuery.Deferred(), |
|
438 | completeDeferred = jQuery.Callbacks( "once memory" ), |
404 | completeDeferred = jQuery.Callbacks("once memory"), |
|
439 | |
- | ||
440 | // Status-dependent callbacks |
405 | // Status-dependent callbacks |
|
441 | statusCode = s.statusCode || {}, |
406 | statusCode = s.statusCode || {}, |
|
442 | |
- | ||
443 | // Headers (they are sent all at once) |
407 | // Headers (they are sent all at once) |
|
444 | requestHeaders = {}, |
408 | requestHeaders = {}, |
|
445 | requestHeadersNames = {}, |
409 | requestHeadersNames = {}, |
|
- | 410 | // The jqXHR state |
||
446 | |
411 | state = 0, |
|
447 | // Default abort message |
412 | // Default abort message |
|
448 | strAbort = "canceled", |
413 | strAbort = "canceled", |
|
449 | |
- | ||
450 | // Fake xhr |
414 | // Fake xhr |
|
451 | jqXHR = { |
415 | jqXHR = { |
|
452 | readyState: 0, |
416 | readyState: 0, |
|
453 | |
417 | |
|
454 | // Builds headers hashtable if needed |
418 | // Builds headers hashtable if needed |
|
455 | getResponseHeader: function( key ) { |
419 | getResponseHeader: function( key ) { |
|
456 | var match; |
420 | var match; |
|
457 | if ( completed ) { |
421 | if ( state === 2 ) { |
|
458 | if ( !responseHeaders ) { |
422 | if ( !responseHeaders ) { |
|
459 | responseHeaders = {}; |
423 | responseHeaders = {}; |
|
460 | while ( ( match = rheaders.exec( responseHeadersString ) ) ) { |
424 | while ( (match = rheaders.exec( responseHeadersString )) ) { |
|
461 | responseHeaders[ match[ 1 ].toLowerCase() ] = match[ 2 ]; |
425 | responseHeaders[ match[1].toLowerCase() ] = match[ 2 ]; |
|
462 | } |
426 | } |
|
463 | } |
427 | } |
|
464 | match = responseHeaders[ key.toLowerCase() ]; |
428 | match = responseHeaders[ key.toLowerCase() ]; |
|
465 | } |
429 | } |
|
466 | return match == null ? null : match; |
430 | return match == null ? null : match; |
|
467 | }, |
431 | }, |
|
468 | |
432 | |
|
469 | // Raw string |
433 | // Raw string |
|
470 | getAllResponseHeaders: function() { |
434 | getAllResponseHeaders: function() { |
|
471 | return completed ? responseHeadersString : null; |
435 | return state === 2 ? responseHeadersString : null; |
|
472 | }, |
436 | }, |
|
473 | |
437 | |
|
474 | // Caches the header |
438 | // Caches the header |
|
475 | setRequestHeader: function( name, value ) { |
439 | setRequestHeader: function( name, value ) { |
|
476 | if ( completed == null ) { |
440 | var lname = name.toLowerCase(); |
|
477 | name = requestHeadersNames[ name.toLowerCase() ] = |
441 | if ( !state ) { |
|
478 | requestHeadersNames[ name.toLowerCase() ] || name; |
442 | name = requestHeadersNames[ lname ] = requestHeadersNames[ lname ] || name; |
|
479 | requestHeaders[ name ] = value; |
443 | requestHeaders[ name ] = value; |
|
480 | } |
444 | } |
|
481 | return this; |
445 | return this; |
|
482 | }, |
446 | }, |
|
483 | |
447 | |
|
484 | // Overrides response content-type header |
448 | // Overrides response content-type header |
|
485 | overrideMimeType: function( type ) { |
449 | overrideMimeType: function( type ) { |
|
486 | if ( completed == null ) { |
450 | if ( !state ) { |
|
487 | s.mimeType = type; |
451 | s.mimeType = type; |
|
488 | } |
452 | } |
|
489 | return this; |
453 | return this; |
|
490 | }, |
454 | }, |
|
491 | |
455 | |
|
492 | // Status-dependent callbacks |
456 | // Status-dependent callbacks |
|
493 | statusCode: function( map ) { |
457 | statusCode: function( map ) { |
|
494 | var code; |
458 | var code; |
|
495 | if ( map ) { |
459 | if ( map ) { |
|
496 | if ( completed ) { |
460 | if ( state < 2 ) { |
|
497 | |
- | ||
498 | // Execute the appropriate callbacks |
- | ||
499 | jqXHR.always( map[ jqXHR.status ] ); |
- | ||
500 | } else { |
- | ||
501 | |
- | ||
502 | // Lazy-add the new callbacks in a way that preserves old ones |
- | ||
503 | for ( code in map ) { |
461 | for ( code in map ) { |
|
- | 462 | // Lazy-add the new callback in a way that preserves old ones |
||
504 | statusCode[ code ] = [ statusCode[ code ], map[ code ] ]; |
463 | statusCode[ code ] = [ statusCode[ code ], map[ code ] ]; |
|
505 | } |
464 | } |
|
- | 465 | } else { |
||
- | 466 | // Execute the appropriate callbacks |
||
- | 467 | jqXHR.always( map[ jqXHR.status ] ); |
||
506 | } |
468 | } |
|
507 | } |
469 | } |
|
508 | return this; |
470 | return this; |
|
509 | }, |
471 | }, |
|
510 | |
472 | |
|
511 | // Cancel the request |
473 | // Cancel the request |
|
512 | abort: function( statusText ) { |
474 | abort: function( statusText ) { |
|
513 | var finalText = statusText || strAbort; |
475 | var finalText = statusText || strAbort; |
|
514 | if ( transport ) { |
476 | if ( transport ) { |
|
515 | transport.abort( finalText ); |
477 | transport.abort( finalText ); |
|
516 | } |
478 | } |
|
517 | done( 0, finalText ); |
479 | done( 0, finalText ); |
|
518 | return this; |
480 | return this; |
|
519 | } |
481 | } |
|
520 | }; |
482 | }; |
|
521 | |
483 | |
|
522 | // Attach deferreds |
484 | // Attach deferreds |
|
- | 485 | deferred.promise( jqXHR ).complete = completeDeferred.add; |
||
- | 486 | jqXHR.success = jqXHR.done; |
||
523 | deferred.promise( jqXHR ); |
487 | jqXHR.error = jqXHR.fail; |
|
- | 488 | |
||
524 | |
489 | // Remove hash character (#7531: and string promotion) |
|
525 | // Add protocol if not provided (prefilters might expect it) |
490 | // Add protocol if not provided (prefilters might expect it) |
|
526 | // Handle falsy url in the settings object (#10093: consistency with old signature) |
491 | // Handle falsy url in the settings object (#10093: consistency with old signature) |
|
527 | // We also use the url parameter if available |
492 | // We also use the url parameter if available |
|
528 | s.url = ( ( url || s.url || location.href ) + "" ) |
493 | s.url = ( ( url || s.url || ajaxLocation ) + "" ).replace( rhash, "" ) |
|
529 | .replace( rprotocol, location.protocol + "//" ); |
494 | .replace( rprotocol, ajaxLocParts[ 1 ] + "//" ); |
|
530 | |
495 | |
|
531 | // Alias method option to type as per ticket #12004 |
496 | // Alias method option to type as per ticket #12004 |
|
532 | s.type = options.method || options.type || s.method || s.type; |
497 | s.type = options.method || options.type || s.method || s.type; |
|
533 | |
498 | |
|
534 | // Extract dataTypes list |
499 | // Extract dataTypes list |
|
535 | s.dataTypes = ( s.dataType || "*" ).toLowerCase().match( rnothtmlwhite ) || [ "" ]; |
500 | s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ]; |
|
536 | |
501 | |
|
537 | // A cross-domain request is in order when the origin doesn't match the current origin. |
502 | // A cross-domain request is in order when we have a protocol:host:port mismatch |
|
538 | if ( s.crossDomain == null ) { |
503 | if ( s.crossDomain == null ) { |
|
539 | urlAnchor = document.createElement( "a" ); |
- | ||
540 | |
- | ||
541 | // Support: IE <=8 - 11, Edge 12 - 13 |
- | ||
542 | // IE throws exception on accessing the href property if url is malformed, |
- | ||
543 | // e.g. http://example.com:80x/ |
- | ||
544 | try { |
- | ||
545 | urlAnchor.href = s.url; |
- | ||
546 | |
504 | parts = rurl.exec( s.url.toLowerCase() ); |
|
547 | // Support: IE <=8 - 11 only |
505 | s.crossDomain = !!( parts && |
|
548 | // Anchor's host property isn't correctly set when s.url is relative |
- | ||
549 | urlAnchor.href = urlAnchor.href; |
506 | ( parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] || |
|
550 | s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !== |
- | ||
551 | urlAnchor.protocol + "//" + urlAnchor.host; |
- | ||
552 | } catch ( e ) { |
- | ||
553 | |
507 | ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? "80" : "443" ) ) !== |
|
554 | // If there is an error parsing the URL, assume it is crossDomain, |
- | ||
555 | // it can be rejected by the transport if it is invalid |
- | ||
556 | s.crossDomain = true; |
508 | ( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? "80" : "443" ) ) ) |
|
557 | } |
509 | ); |
|
558 | } |
510 | } |
|
559 | |
511 | |
|
560 | // Convert data if not already a string |
512 | // Convert data if not already a string |
|
561 | if ( s.data && s.processData && typeof s.data !== "string" ) { |
513 | if ( s.data && s.processData && typeof s.data !== "string" ) { |
|
562 | s.data = jQuery.param( s.data, s.traditional ); |
514 | s.data = jQuery.param( s.data, s.traditional ); |
|
563 | } |
515 | } |
|
564 | |
516 | |
|
565 | // Apply prefilters |
517 | // Apply prefilters |
|
566 | inspectPrefiltersOrTransports( prefilters, s, options, jqXHR ); |
518 | inspectPrefiltersOrTransports( prefilters, s, options, jqXHR ); |
|
567 | |
519 | |
|
568 | // If request was aborted inside a prefilter, stop there |
520 | // If request was aborted inside a prefilter, stop there |
|
569 | if ( completed ) { |
521 | if ( state === 2 ) { |
|
570 | return jqXHR; |
522 | return jqXHR; |
|
571 | } |
523 | } |
|
572 | |
524 | |
|
573 | // We can fire global events as of now if asked to |
525 | // We can fire global events as of now if asked to |
|
574 | // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118) |
526 | // Don't fire events if jQuery.event is undefined in an AMD-usage scenario (#15118) |
|
575 | fireGlobals = jQuery.event && s.global; |
527 | fireGlobals = jQuery.event && s.global; |
|
576 | |
528 | |
|
577 | // Watch for a new set of requests |
529 | // Watch for a new set of requests |
|
578 | if ( fireGlobals && jQuery.active++ === 0 ) { |
530 | if ( fireGlobals && jQuery.active++ === 0 ) { |
|
579 | jQuery.event.trigger( "ajaxStart" ); |
531 | jQuery.event.trigger("ajaxStart"); |
|
580 | } |
532 | } |
|
581 | |
533 | |
|
582 | // Uppercase the type |
534 | // Uppercase the type |
|
583 | s.type = s.type.toUpperCase(); |
535 | s.type = s.type.toUpperCase(); |
|
584 | |
536 | |
|
585 | // Determine if request has content |
537 | // Determine if request has content |
|
586 | s.hasContent = !rnoContent.test( s.type ); |
538 | s.hasContent = !rnoContent.test( s.type ); |
|
587 | |
539 | |
|
588 | // Save the URL in case we're toying with the If-Modified-Since |
540 | // Save the URL in case we're toying with the If-Modified-Since |
|
589 | // and/or If-None-Match header later on |
541 | // and/or If-None-Match header later on |
|
590 | // Remove hash to simplify url manipulation |
- | ||
591 | cacheURL = s.url.replace( rhash, "" ); |
542 | cacheURL = s.url; |
|
592 | |
543 | |
|
593 | // More options handling for requests with no content |
544 | // More options handling for requests with no content |
|
594 | if ( !s.hasContent ) { |
545 | if ( !s.hasContent ) { |
|
595 | |
- | ||
596 | // Remember the hash so we can put it back |
- | ||
597 | uncached = s.url.slice( cacheURL.length ); |
- | ||
598 | |
546 | |
|
599 | // If data is available, append data to url |
547 | // If data is available, append data to url |
|
600 | if ( s.data ) { |
548 | if ( s.data ) { |
|
601 | cacheURL += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data; |
- | ||
602 | |
549 | cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data ); |
|
603 | // #9682: remove data so that it's not used in an eventual retry |
550 | // #9682: remove data so that it's not used in an eventual retry |
|
604 | delete s.data; |
551 | delete s.data; |
|
605 | } |
552 | } |
|
606 | |
553 | |
|
607 | // Add or update anti-cache param if needed |
554 | // Add anti-cache in url if needed |
|
608 | if ( s.cache === false ) { |
555 | if ( s.cache === false ) { |
|
609 | cacheURL = cacheURL.replace( rantiCache, "$1" ); |
- | ||
610 | uncached = ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + ( nonce++ ) + uncached; |
- | ||
611 | } |
556 | s.url = rts.test( cacheURL ) ? |
|
612 | |
557 | |
|
613 | // Put hash and anti-cache on the URL that will be requested (gh-1732) |
- | ||
614 | s.url = cacheURL + uncached; |
558 | // If there is already a '_' parameter, set its value |
|
615 | |
559 | cacheURL.replace( rts, "$1_=" + nonce++ ) : |
|
616 | // Change '%20' to '+' if this is encoded form body content (gh-2658) |
560 | |
|
617 | } else if ( s.data && s.processData && |
561 | // Otherwise add one to the end |
|
618 | ( s.contentType || "" ).indexOf( "application/x-www-form-urlencoded" ) === 0 ) { |
562 | cacheURL + ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + nonce++; |
|
619 | s.data = s.data.replace( r20, "+" ); |
563 | } |
|
620 | } |
564 | } |
|
621 | |
565 | |
|
622 | // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. |
566 | // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. |
|
623 | if ( s.ifModified ) { |
567 | if ( s.ifModified ) { |
|
624 | if ( jQuery.lastModified[ cacheURL ] ) { |
568 | if ( jQuery.lastModified[ cacheURL ] ) { |
|
625 | jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] ); |
569 | jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ cacheURL ] ); |
|
626 | } |
570 | } |
|
627 | if ( jQuery.etag[ cacheURL ] ) { |
571 | if ( jQuery.etag[ cacheURL ] ) { |
|
628 | jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] ); |
572 | jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ cacheURL ] ); |
|
629 | } |
573 | } |
|
630 | } |
574 | } |
|
631 | |
575 | |
|
632 | // Set the correct header, if data is being sent |
576 | // Set the correct header, if data is being sent |
|
633 | if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) { |
577 | if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) { |
|
634 | jqXHR.setRequestHeader( "Content-Type", s.contentType ); |
578 | jqXHR.setRequestHeader( "Content-Type", s.contentType ); |
|
635 | } |
579 | } |
|
636 | |
580 | |
|
637 | // Set the Accepts header for the server, depending on the dataType |
581 | // Set the Accepts header for the server, depending on the dataType |
|
638 | jqXHR.setRequestHeader( |
582 | jqXHR.setRequestHeader( |
|
639 | "Accept", |
583 | "Accept", |
|
640 | s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[ 0 ] ] ? |
584 | s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ? |
|
641 | s.accepts[ s.dataTypes[ 0 ] ] + |
- | ||
642 | ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) : |
585 | s.accepts[ s.dataTypes[0] ] + ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) : |
|
643 | s.accepts[ "*" ] |
586 | s.accepts[ "*" ] |
|
644 | ); |
587 | ); |
|
645 | |
588 | |
|
646 | // Check for headers option |
589 | // Check for headers option |
|
647 | for ( i in s.headers ) { |
590 | for ( i in s.headers ) { |
|
648 | jqXHR.setRequestHeader( i, s.headers[ i ] ); |
591 | jqXHR.setRequestHeader( i, s.headers[ i ] ); |
|
649 | } |
592 | } |
|
650 | |
593 | |
|
651 | // Allow custom headers/mimetypes and early abort |
594 | // Allow custom headers/mimetypes and early abort |
|
652 | if ( s.beforeSend && |
- | ||
653 | ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || completed ) ) { |
595 | if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) { |
|
654 | |
- | ||
655 | // Abort if not done already and return |
596 | // Abort if not done already and return |
|
656 | return jqXHR.abort(); |
597 | return jqXHR.abort(); |
|
657 | } |
598 | } |
|
658 | |
599 | |
|
659 | // Aborting is no longer a cancellation |
600 | // Aborting is no longer a cancellation |
|
660 | strAbort = "abort"; |
601 | strAbort = "abort"; |
|
661 | |
602 | |
|
662 | // Install callbacks on deferreds |
603 | // Install callbacks on deferreds |
|
663 | completeDeferred.add( s.complete ); |
604 | for ( i in { success: 1, error: 1, complete: 1 } ) { |
|
664 | jqXHR.done( s.success ); |
605 | jqXHR[ i ]( s[ i ] ); |
|
665 | jqXHR.fail( s.error ); |
606 | } |
|
666 | |
607 | |
|
667 | // Get transport |
608 | // Get transport |
|
668 | transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR ); |
609 | transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR ); |
|
669 | |
610 | |
|
670 | // If no transport, we auto-abort |
611 | // If no transport, we auto-abort |
|
671 | if ( !transport ) { |
612 | if ( !transport ) { |
|
672 | done( -1, "No Transport" ); |
613 | done( -1, "No Transport" ); |
|
673 | } else { |
614 | } else { |
|
674 | jqXHR.readyState = 1; |
615 | jqXHR.readyState = 1; |
|
675 | |
616 | |
|
676 | // Send global event |
617 | // Send global event |
|
677 | if ( fireGlobals ) { |
618 | if ( fireGlobals ) { |
|
678 | globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] ); |
619 | globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] ); |
|
679 | } |
620 | } |
|
680 | |
- | ||
681 | // If request was aborted inside ajaxSend, stop there |
- | ||
682 | if ( completed ) { |
- | ||
683 | return jqXHR; |
- | ||
684 | } |
- | ||
685 | |
- | ||
686 | // Timeout |
621 | // Timeout |
|
687 | if ( s.async && s.timeout > 0 ) { |
622 | if ( s.async && s.timeout > 0 ) { |
|
688 | timeoutTimer = window.setTimeout( function() { |
623 | timeoutTimer = setTimeout(function() { |
|
689 | jqXHR.abort( "timeout" ); |
624 | jqXHR.abort("timeout"); |
|
690 | }, s.timeout ); |
625 | }, s.timeout ); |
|
691 | } |
626 | } |
|
692 | |
627 | |
|
693 | try { |
628 | try { |
|
694 | completed = false; |
629 | state = 1; |
|
695 | transport.send( requestHeaders, done ); |
630 | transport.send( requestHeaders, done ); |
|
696 | } catch ( e ) { |
631 | } catch ( e ) { |
|
697 | |
- | ||
698 | // Rethrow post-completion exceptions |
632 | // Propagate exception as error if not done |
|
699 | if ( completed ) { |
633 | if ( state < 2 ) { |
|
- | 634 | done( -1, e ); |
||
- | 635 | // Simply rethrow otherwise |
||
- | 636 | } else { |
||
700 | throw e; |
637 | throw e; |
|
701 | } |
638 | } |
|
702 | |
- | ||
703 | // Propagate others as results |
- | ||
704 | done( -1, e ); |
- | ||
705 | } |
639 | } |
|
706 | } |
640 | } |
|
707 | |
641 | |
|
708 | // Callback for when everything is done |
642 | // Callback for when everything is done |
|
709 | function done( status, nativeStatusText, responses, headers ) { |
643 | function done( status, nativeStatusText, responses, headers ) { |
|
710 | var isSuccess, success, error, response, modified, |
644 | var isSuccess, success, error, response, modified, |
|
711 | statusText = nativeStatusText; |
645 | statusText = nativeStatusText; |
|
712 | |
646 | |
|
713 | // Ignore repeat invocations |
647 | // Called once |
|
714 | if ( completed ) { |
648 | if ( state === 2 ) { |
|
715 | return; |
649 | return; |
|
716 | } |
650 | } |
|
- | 651 | |
||
717 | |
652 | // State is "done" now |
|
718 | completed = true; |
653 | state = 2; |
|
719 | |
654 | |
|
720 | // Clear timeout if it exists |
655 | // Clear timeout if it exists |
|
721 | if ( timeoutTimer ) { |
656 | if ( timeoutTimer ) { |
|
722 | window.clearTimeout( timeoutTimer ); |
657 | clearTimeout( timeoutTimer ); |
|
723 | } |
658 | } |
|
724 | |
659 | |
|
725 | // Dereference transport for early garbage collection |
660 | // Dereference transport for early garbage collection |
|
726 | // (no matter how long the jqXHR object will be used) |
661 | // (no matter how long the jqXHR object will be used) |
|
727 | transport = undefined; |
662 | transport = undefined; |
|
728 | |
663 | |
|
729 | // Cache response headers |
664 | // Cache response headers |
|
730 | responseHeadersString = headers || ""; |
665 | responseHeadersString = headers || ""; |
|
731 | |
666 | |
|
732 | // Set readyState |
667 | // Set readyState |
|
733 | jqXHR.readyState = status > 0 ? 4 : 0; |
668 | jqXHR.readyState = status > 0 ? 4 : 0; |
|
734 | |
669 | |
|
735 | // Determine if successful |
670 | // Determine if successful |
|
736 | isSuccess = status >= 200 && status < 300 || status === 304; |
671 | isSuccess = status >= 200 && status < 300 || status === 304; |
|
737 | |
672 | |
|
738 | // Get response data |
673 | // Get response data |
|
739 | if ( responses ) { |
674 | if ( responses ) { |
|
740 | response = ajaxHandleResponses( s, jqXHR, responses ); |
675 | response = ajaxHandleResponses( s, jqXHR, responses ); |
|
741 | } |
676 | } |
|
742 | |
677 | |
|
743 | // Convert no matter what (that way responseXXX fields are always set) |
678 | // Convert no matter what (that way responseXXX fields are always set) |
|
744 | response = ajaxConvert( s, response, jqXHR, isSuccess ); |
679 | response = ajaxConvert( s, response, jqXHR, isSuccess ); |
|
745 | |
680 | |
|
746 | // If successful, handle type chaining |
681 | // If successful, handle type chaining |
|
747 | if ( isSuccess ) { |
682 | if ( isSuccess ) { |
|
748 | |
683 | |
|
749 | // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. |
684 | // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. |
|
750 | if ( s.ifModified ) { |
685 | if ( s.ifModified ) { |
|
751 | modified = jqXHR.getResponseHeader( "Last-Modified" ); |
686 | modified = jqXHR.getResponseHeader("Last-Modified"); |
|
752 | if ( modified ) { |
687 | if ( modified ) { |
|
753 | jQuery.lastModified[ cacheURL ] = modified; |
688 | jQuery.lastModified[ cacheURL ] = modified; |
|
754 | } |
689 | } |
|
755 | modified = jqXHR.getResponseHeader( "etag" ); |
690 | modified = jqXHR.getResponseHeader("etag"); |
|
756 | if ( modified ) { |
691 | if ( modified ) { |
|
757 | jQuery.etag[ cacheURL ] = modified; |
692 | jQuery.etag[ cacheURL ] = modified; |
|
758 | } |
693 | } |
|
759 | } |
694 | } |
|
760 | |
695 | |
|
761 | // if no content |
696 | // if no content |
|
762 | if ( status === 204 || s.type === "HEAD" ) { |
697 | if ( status === 204 || s.type === "HEAD" ) { |
|
763 | statusText = "nocontent"; |
698 | statusText = "nocontent"; |
|
764 | |
699 | |
|
765 | // if not modified |
700 | // if not modified |
|
766 | } else if ( status === 304 ) { |
701 | } else if ( status === 304 ) { |
|
767 | statusText = "notmodified"; |
702 | statusText = "notmodified"; |
|
768 | |
703 | |
|
769 | // If we have data, let's convert it |
704 | // If we have data, let's convert it |
|
770 | } else { |
705 | } else { |
|
771 | statusText = response.state; |
706 | statusText = response.state; |
|
772 | success = response.data; |
707 | success = response.data; |
|
773 | error = response.error; |
708 | error = response.error; |
|
774 | isSuccess = !error; |
709 | isSuccess = !error; |
|
775 | } |
710 | } |
|
776 | } else { |
711 | } else { |
|
777 | |
- | ||
778 | // Extract error from statusText and normalize for non-aborts |
712 | // Extract error from statusText and normalize for non-aborts |
|
779 | error = statusText; |
713 | error = statusText; |
|
780 | if ( status || !statusText ) { |
714 | if ( status || !statusText ) { |
|
781 | statusText = "error"; |
715 | statusText = "error"; |
|
782 | if ( status < 0 ) { |
716 | if ( status < 0 ) { |
|
783 | status = 0; |
717 | status = 0; |
|
784 | } |
718 | } |
|
785 | } |
719 | } |
|
786 | } |
720 | } |
|
787 | |
721 | |
|
788 | // Set data for the fake xhr object |
722 | // Set data for the fake xhr object |
|
789 | jqXHR.status = status; |
723 | jqXHR.status = status; |
|
790 | jqXHR.statusText = ( nativeStatusText || statusText ) + ""; |
724 | jqXHR.statusText = ( nativeStatusText || statusText ) + ""; |
|
791 | |
725 | |
|
792 | // Success/Error |
726 | // Success/Error |
|
793 | if ( isSuccess ) { |
727 | if ( isSuccess ) { |
|
794 | deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); |
728 | deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); |
|
795 | } else { |
729 | } else { |
|
796 | deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] ); |
730 | deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] ); |
|
797 | } |
731 | } |
|
798 | |
732 | |
|
799 | // Status-dependent callbacks |
733 | // Status-dependent callbacks |
|
800 | jqXHR.statusCode( statusCode ); |
734 | jqXHR.statusCode( statusCode ); |
|
801 | statusCode = undefined; |
735 | statusCode = undefined; |
|
802 | |
736 | |
|
803 | if ( fireGlobals ) { |
737 | if ( fireGlobals ) { |
|
804 | globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError", |
738 | globalEventContext.trigger( isSuccess ? "ajaxSuccess" : "ajaxError", |
|
805 | [ jqXHR, s, isSuccess ? success : error ] ); |
739 | [ jqXHR, s, isSuccess ? success : error ] ); |
|
806 | } |
740 | } |
|
807 | |
741 | |
|
808 | // Complete |
742 | // Complete |
|
809 | completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] ); |
743 | completeDeferred.fireWith( callbackContext, [ jqXHR, statusText ] ); |
|
810 | |
744 | |
|
811 | if ( fireGlobals ) { |
745 | if ( fireGlobals ) { |
|
812 | globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] ); |
746 | globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] ); |
|
813 | |
- | ||
814 | // Handle the global AJAX counter |
747 | // Handle the global AJAX counter |
|
815 | if ( !( --jQuery.active ) ) { |
748 | if ( !( --jQuery.active ) ) { |
|
816 | jQuery.event.trigger( "ajaxStop" ); |
749 | jQuery.event.trigger("ajaxStop"); |
|
817 | } |
750 | } |
|
818 | } |
751 | } |
|
819 | } |
752 | } |
|
820 | |
753 | |
|
821 | return jqXHR; |
754 | return jqXHR; |
|
822 | }, |
755 | }, |
|
823 | |
756 | |
|
824 | getJSON: function( url, data, callback ) { |
757 | getJSON: function( url, data, callback ) { |
|
825 | return jQuery.get( url, data, callback, "json" ); |
758 | return jQuery.get( url, data, callback, "json" ); |
|
826 | }, |
759 | }, |
|
827 | |
760 | |
|
828 | getScript: function( url, callback ) { |
761 | getScript: function( url, callback ) { |
|
829 | return jQuery.get( url, undefined, callback, "script" ); |
762 | return jQuery.get( url, undefined, callback, "script" ); |
|
830 | } |
763 | } |
|
831 | } ); |
764 | }); |
|
832 | |
765 | |
|
833 | jQuery.each( [ "get", "post" ], function( i, method ) { |
766 | jQuery.each( [ "get", "post" ], function( i, method ) { |
|
834 | jQuery[ method ] = function( url, data, callback, type ) { |
767 | jQuery[ method ] = function( url, data, callback, type ) { |
|
835 | |
- | ||
836 | // Shift arguments if data argument was omitted |
768 | // Shift arguments if data argument was omitted |
|
837 | if ( jQuery.isFunction( data ) ) { |
769 | if ( jQuery.isFunction( data ) ) { |
|
838 | type = type || callback; |
770 | type = type || callback; |
|
839 | callback = data; |
771 | callback = data; |
|
840 | data = undefined; |
772 | data = undefined; |
|
841 | } |
773 | } |
|
842 | |
- | ||
843 | // The url can be an options object (which then must have .url) |
774 | |
|
844 | return jQuery.ajax( jQuery.extend( { |
775 | return jQuery.ajax({ |
|
845 | url: url, |
776 | url: url, |
|
846 | type: method, |
777 | type: method, |
|
847 | dataType: type, |
778 | dataType: type, |
|
848 | data: data, |
779 | data: data, |
|
849 | success: callback |
780 | success: callback |
|
850 | }, jQuery.isPlainObject( url ) && url ) ); |
781 | }); |
|
851 | }; |
782 | }; |
|
852 | } ); |
783 | }); |
|
853 | |
784 | |
|
854 | return jQuery; |
785 | return jQuery; |
|
855 | } ); |
786 | }); |
|
856 | |
787 | |