@@ -203,11 +203,10 @@ jQuery.extend({
203
203
etag : { } ,
204
204
205
205
ajax : function ( origSettings ) {
206
- var s = jQuery . extend ( true , { } , jQuery . ajaxSettings , origSettings ) ;
207
-
208
- var jsonp , status , data ,
209
- callbackContext = origSettings && origSettings . context || s ,
210
- type = s . type . toUpperCase ( ) ;
206
+ var s = jQuery . extend ( true , { } , jQuery . ajaxSettings , origSettings ) ,
207
+ jsonp , status , data , type = s . type . toUpperCase ( ) ;
208
+
209
+ s . context = origSettings && origSettings . context || s ;
211
210
212
211
// convert data if not already a string
213
212
if ( s . data && s . processData && typeof s . data !== "string" ) {
@@ -244,14 +243,14 @@ jQuery.extend({
244
243
// Handle JSONP-style loading
245
244
window [ jsonp ] = window [ jsonp ] || function ( tmp ) {
246
245
data = tmp ;
247
- success ( ) ;
248
- complete ( ) ;
246
+ jQuery . ajax . handleSuccess ( s , xhr , status , data ) ;
247
+ jQuery . ajax . handleComplete ( s , xhr , status , data ) ;
249
248
// Garbage collect
250
249
window [ jsonp ] = undefined ;
251
250
252
251
try {
253
252
delete window [ jsonp ] ;
254
- } catch ( e ) { }
253
+ } catch ( jsonpError ) { }
255
254
256
255
if ( head ) {
257
256
head . removeChild ( script ) ;
@@ -279,7 +278,7 @@ jQuery.extend({
279
278
}
280
279
281
280
// Watch for a new set of requests
282
- if ( s . global && jQuery . active ++ === 0 ) {
281
+ if ( s . global && jQuery . ajax . active ++ === 0 ) {
283
282
jQuery . event . trigger ( "ajaxStart" ) ;
284
283
}
285
284
@@ -306,8 +305,8 @@ jQuery.extend({
306
305
if ( ! done && ( ! this . readyState ||
307
306
this . readyState === "loaded" || this . readyState === "complete" ) ) {
308
307
done = true ;
309
- success ( ) ;
310
- complete ( ) ;
308
+ jQuery . ajax . handleSuccess ( s , xhr , status , data ) ;
309
+ jQuery . ajax . handleComplete ( s , xhr , status , data ) ;
311
310
312
311
// Handle memory leak in IE
313
312
script . onload = script . onreadystatechange = null ;
@@ -371,12 +370,12 @@ jQuery.extend({
371
370
xhr . setRequestHeader ( "Accept" , s . dataType && s . accepts [ s . dataType ] ?
372
371
s . accepts [ s . dataType ] + ", */*" :
373
372
s . accepts . _default ) ;
374
- } catch ( e ) { }
373
+ } catch ( headerError ) { }
375
374
376
375
// Allow custom headers/mimetypes and early abort
377
- if ( s . beforeSend && s . beforeSend . call ( callbackContext , xhr , s ) === false ) {
376
+ if ( s . beforeSend && s . beforeSend . call ( s . context , xhr , s ) === false ) {
378
377
// Handle the global AJAX counter
379
- if ( s . global && ! -- jQuery . active ) {
378
+ if ( s . global && jQuery . ajax . active -- === 1 ) {
380
379
jQuery . event . trigger ( "ajaxStop" ) ;
381
380
}
382
381
@@ -386,7 +385,7 @@ jQuery.extend({
386
385
}
387
386
388
387
if ( s . global ) {
389
- contextTrigger ( "ajaxSend" , [ xhr , s ] ) ;
388
+ jQuery . ajax . triggerGlobal ( s , "ajaxSend" , [ xhr , s ] ) ;
390
389
}
391
390
392
391
// Wait for a response to come back
@@ -396,7 +395,7 @@ jQuery.extend({
396
395
// Opera doesn't call onreadystatechange before this point
397
396
// so we simulate the call
398
397
if ( ! requestDone ) {
399
- complete ( ) ;
398
+ jQuery . ajax . handleComplete ( s , xhr , status , data ) ;
400
399
}
401
400
402
401
requestDone = true ;
@@ -411,9 +410,9 @@ jQuery.extend({
411
410
412
411
status = isTimeout === "timeout" ?
413
412
"timeout" :
414
- ! jQuery . httpSuccess ( xhr ) ?
413
+ ! jQuery . ajax . httpSuccess ( xhr ) ?
415
414
"error" :
416
- s . ifModified && jQuery . httpNotModified ( xhr , s . url ) ?
415
+ s . ifModified && jQuery . ajax . httpNotModified ( xhr , s . url ) ?
417
416
"notmodified" :
418
417
"success" ;
419
418
@@ -423,25 +422,25 @@ jQuery.extend({
423
422
// Watch for, and catch, XML document parse errors
424
423
try {
425
424
// process the data (runs the xml through httpData regardless of callback)
426
- data = jQuery . httpData ( xhr , s . dataType , s ) ;
427
- } catch ( err ) {
425
+ data = jQuery . ajax . httpData ( xhr , s . dataType , s ) ;
426
+ } catch ( parserError ) {
428
427
status = "parsererror" ;
429
- errMsg = err ;
428
+ errMsg = parserError ;
430
429
}
431
430
}
432
431
433
432
// Make sure that the request was successful or notmodified
434
433
if ( status === "success" || status === "notmodified" ) {
435
434
// JSONP handles its own success callback
436
435
if ( ! jsonp ) {
437
- success ( ) ;
436
+ jQuery . ajax . handleSuccess ( s , xhr , status , data ) ;
438
437
}
439
438
} else {
440
- jQuery . handleError ( s , xhr , status , errMsg ) ;
439
+ jQuery . ajax . handleError ( s , xhr , status , errMsg ) ;
441
440
}
442
441
443
442
// Fire the complete handlers
444
- complete ( ) ;
443
+ jQuery . ajax . handleComplete ( s , xhr , status , data ) ;
445
444
446
445
if ( isTimeout === "timeout" ) {
447
446
xhr . abort ( ) ;
@@ -465,7 +464,7 @@ jQuery.extend({
465
464
466
465
onreadystatechange ( "abort" ) ;
467
466
} ;
468
- } catch ( e ) { }
467
+ } catch ( abortError ) { }
469
468
470
469
// Timeout checker
471
470
if ( s . async && s . timeout > 0 ) {
@@ -480,68 +479,138 @@ jQuery.extend({
480
479
// Send the data
481
480
try {
482
481
xhr . send ( type === "POST" || type === "PUT" || type === "DELETE" ? s . data : null ) ;
483
- } catch ( e ) {
484
- jQuery . handleError ( s , xhr , null , e ) ;
482
+
483
+ } catch ( sendError ) {
484
+ jQuery . ajax . handleError ( s , xhr , null , e ) ;
485
+
485
486
// Fire the complete handlers
486
- complete ( ) ;
487
+ jQuery . ajax . handleComplete ( s , xhr , status , data ) ;
487
488
}
488
489
489
490
// firefox 1.5 doesn't fire statechange for sync requests
490
491
if ( ! s . async ) {
491
492
onreadystatechange ( ) ;
492
493
}
493
494
494
- function success ( ) {
495
- // If a local callback was specified, fire it and pass it the data
496
- if ( s . success ) {
497
- s . success . call ( callbackContext , data , status , xhr ) ;
498
- }
495
+ // return XMLHttpRequest to allow aborting the request etc.
496
+ return xhr ;
497
+ } ,
499
498
500
- // Fire the global callback
501
- if ( s . global ) {
502
- contextTrigger ( "ajaxSuccess" , [ xhr , s ] ) ;
499
+ // Serialize an array of form elements or a set of
500
+ // key/values into a query string
501
+ param : function ( a , traditional ) {
502
+ var s = [ ] , add = function ( key , value ) {
503
+ // If value is a function, invoke it and return its value
504
+ value = jQuery . isFunction ( value ) ? value ( ) : value ;
505
+ s [ s . length ] = encodeURIComponent ( key ) + "=" + encodeURIComponent ( value ) ;
506
+ } ;
507
+
508
+ // Set traditional to true for jQuery <= 1.3.2 behavior.
509
+ if ( traditional === undefined ) {
510
+ traditional = jQuery . ajaxSettings . traditional ;
511
+ }
512
+
513
+ // If an array was passed in, assume that it is an array of form elements.
514
+ if ( jQuery . isArray ( a ) || a . jquery ) {
515
+ // Serialize the form elements
516
+ jQuery . each ( a , function ( ) {
517
+ add ( this . name , this . value ) ;
518
+ } ) ;
519
+
520
+ } else {
521
+ // If traditional, encode the "old" way (the way 1.3.2 or older
522
+ // did it), otherwise encode params recursively.
523
+ for ( var prefix in a ) {
524
+ buildParams ( prefix , a [ prefix ] , traditional , add ) ;
503
525
}
504
526
}
505
527
506
- function complete ( ) {
507
- // Process result
508
- if ( s . complete ) {
509
- s . complete . call ( callbackContext , xhr , status ) ;
510
- }
528
+ // Return the resulting serialization
529
+ return s . join ( "&" ) . replace ( r20 , "+" ) ;
530
+ }
531
+ } ) ;
511
532
512
- // The request was completed
513
- if ( s . global ) {
514
- contextTrigger ( "ajaxComplete" , [ xhr , s ] ) ;
515
- }
533
+ function buildParams ( prefix , obj , traditional , add ) {
534
+ if ( jQuery . isArray ( obj ) ) {
535
+ // Serialize array item.
536
+ jQuery . each ( obj , function ( i , v ) {
537
+ if ( traditional || / \[ \] $ / . test ( prefix ) ) {
538
+ // Treat each array item as a scalar.
539
+ add ( prefix , v ) ;
516
540
517
- // Handle the global AJAX counter
518
- if ( s . global && ! -- jQuery . active ) {
519
- jQuery . event . trigger ( "ajaxStop" ) ;
541
+ } else {
542
+ // If array item is non-scalar (array or object), encode its
543
+ // numeric index to resolve deserialization ambiguity issues.
544
+ // Note that rack (as of 1.0.0) can't currently deserialize
545
+ // nested arrays properly, and attempting to do so may cause
546
+ // a server error. Possible fixes are to modify rack's
547
+ // deserialization algorithm or to provide an option or flag
548
+ // to force array serialization to be shallow.
549
+ buildParams ( prefix + "[" + ( typeof v === "object" || jQuery . isArray ( v ) ? i : "" ) + "]" , v , traditional , add ) ;
520
550
}
521
- }
522
-
523
- function contextTrigger ( type , args ) {
524
- ( s . context ? jQuery ( s . context ) : jQuery . event ) . trigger ( type , args ) ;
525
- }
551
+ } ) ;
552
+
553
+ } else if ( ! traditional && obj != null && typeof obj === "object" ) {
554
+ // Serialize object item.
555
+ jQuery . each ( obj , function ( k , v ) {
556
+ buildParams ( prefix + "[" + k + "]" , v , traditional , add ) ;
557
+ } ) ;
558
+
559
+ } else {
560
+ // Serialize scalar item.
561
+ add ( prefix , obj ) ;
562
+ }
563
+ }
526
564
527
- // return XMLHttpRequest to allow aborting the request etc.
528
- return xhr ;
529
- } ,
565
+ jQuery . extend ( jQuery . ajax , {
566
+
567
+ // Counter for holding the number of active queries
568
+ active : 0 ,
530
569
531
570
handleError : function ( s , xhr , status , e ) {
532
571
// If a local callback was specified, fire it
533
572
if ( s . error ) {
534
- s . error . call ( s . context || s , xhr , status , e ) ;
573
+ s . error . call ( s . context , xhr , status , e ) ;
535
574
}
536
575
537
576
// Fire the global callback
538
577
if ( s . global ) {
539
- ( s . context ? jQuery ( s . context ) : jQuery . event ) . trigger ( "ajaxError" , [ xhr , s , e ] ) ;
578
+ jQuery . ajax . triggerGlobal ( s , "ajaxError" , [ xhr , s , e ] ) ;
540
579
}
541
580
} ,
542
581
543
- // Counter for holding the number of active queries
544
- active : 0 ,
582
+ handleSuccess : function ( s , xhr , status , data ) {
583
+ // If a local callback was specified, fire it and pass it the data
584
+ if ( s . success ) {
585
+ s . success . call ( s . context , data , status , xhr ) ;
586
+ }
587
+
588
+ // Fire the global callback
589
+ if ( s . global ) {
590
+ jQuery . ajax . triggerGlobal ( s , "ajaxSuccess" , [ xhr , s ] ) ;
591
+ }
592
+ } ,
593
+
594
+ handleComplete : function ( s , xhr , status ) {
595
+ // Process result
596
+ if ( s . complete ) {
597
+ s . complete . call ( s . context , xhr , status ) ;
598
+ }
599
+
600
+ // The request was completed
601
+ if ( s . global ) {
602
+ jQuery . ajax . triggerGlobal ( s , "ajaxComplete" , [ xhr , s ] ) ;
603
+ }
604
+
605
+ // Handle the global AJAX counter
606
+ if ( s . global && jQuery . ajax . active -- === 1 ) {
607
+ jQuery . event . trigger ( "ajaxStop" ) ;
608
+ }
609
+ } ,
610
+
611
+ triggerGlobal : function ( s , type , args ) {
612
+ ( s . context && s . context . url == null ? jQuery ( s . context ) : jQuery . event ) . trigger ( type , args ) ;
613
+ } ,
545
614
546
615
// Determines if an XMLHttpRequest was successful or not
547
616
httpSuccess : function ( xhr ) {
@@ -601,71 +670,6 @@ jQuery.extend({
601
670
}
602
671
603
672
return data ;
604
- } ,
605
-
606
- // Serialize an array of form elements or a set of
607
- // key/values into a query string
608
- param : function ( a , traditional ) {
609
- var s = [ ] ;
610
-
611
- // Set traditional to true for jQuery <= 1.3.2 behavior.
612
- if ( traditional === undefined ) {
613
- traditional = jQuery . ajaxSettings . traditional ;
614
- }
615
-
616
- // If an array was passed in, assume that it is an array of form elements.
617
- if ( jQuery . isArray ( a ) || a . jquery ) {
618
- // Serialize the form elements
619
- jQuery . each ( a , function ( ) {
620
- add ( this . name , this . value ) ;
621
- } ) ;
622
-
623
- } else {
624
- // If traditional, encode the "old" way (the way 1.3.2 or older
625
- // did it), otherwise encode params recursively.
626
- for ( var prefix in a ) {
627
- buildParams ( prefix , a [ prefix ] ) ;
628
- }
629
- }
630
-
631
- // Return the resulting serialization
632
- return s . join ( "&" ) . replace ( r20 , "+" ) ;
633
-
634
- function buildParams ( prefix , obj ) {
635
- if ( jQuery . isArray ( obj ) ) {
636
- // Serialize array item.
637
- jQuery . each ( obj , function ( i , v ) {
638
- if ( traditional || / \[ \] $ / . test ( prefix ) ) {
639
- // Treat each array item as a scalar.
640
- add ( prefix , v ) ;
641
- } else {
642
- // If array item is non-scalar (array or object), encode its
643
- // numeric index to resolve deserialization ambiguity issues.
644
- // Note that rack (as of 1.0.0) can't currently deserialize
645
- // nested arrays properly, and attempting to do so may cause
646
- // a server error. Possible fixes are to modify rack's
647
- // deserialization algorithm or to provide an option or flag
648
- // to force array serialization to be shallow.
649
- buildParams ( prefix + "[" + ( typeof v === "object" || jQuery . isArray ( v ) ? i : "" ) + "]" , v ) ;
650
- }
651
- } ) ;
652
-
653
- } else if ( ! traditional && obj != null && typeof obj === "object" ) {
654
- // Serialize object item.
655
- jQuery . each ( obj , function ( k , v ) {
656
- buildParams ( prefix + "[" + k + "]" , v ) ;
657
- } ) ;
658
-
659
- } else {
660
- // Serialize scalar item.
661
- add ( prefix , obj ) ;
662
- }
663
- }
664
-
665
- function add ( key , value ) {
666
- // If value is a function, invoke it and return its value
667
- value = jQuery . isFunction ( value ) ? value ( ) : value ;
668
- s [ s . length ] = encodeURIComponent ( key ) + "=" + encodeURIComponent ( value ) ;
669
- }
670
673
}
674
+
671
675
} ) ;
0 commit comments