Bring back in #main prefix on adjacent selectors.
[jquery.git] / test / unit / ajax.js
1 module("ajax", { teardown: moduleTeardown });
2
3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 //if ( !jQuery.browser.safari ) {
7
8 if ( !isLocal ) {
9
10 test("jQuery.ajax() - success callbacks", function() {
11         expect( 8 );
12
13         jQuery.ajaxSetup({ timeout: 0 });
14
15         stop();
16
17         jQuery('#foo').ajaxStart(function(){
18                 ok( true, "ajaxStart" );
19         }).ajaxStop(function(){
20                 ok( true, "ajaxStop" );
21                 start();
22         }).ajaxSend(function(){
23                 ok( true, "ajaxSend" );
24         }).ajaxComplete(function(){
25                 ok( true, "ajaxComplete" );
26         }).ajaxError(function(){
27                 ok( false, "ajaxError" );
28         }).ajaxSuccess(function(){
29                 ok( true, "ajaxSuccess" );
30         });
31
32         jQuery.ajax({
33                 url: url("data/name.html"),
34                 beforeSend: function(){ ok(true, "beforeSend"); },
35                 success: function(){ ok(true, "success"); },
36                 error: function(){ ok(false, "error"); },
37                 complete: function(){ ok(true, "complete"); }
38         });
39 });
40
41 test("jQuery.ajax() - success callbacks - (url, options) syntax", function() {
42         expect( 8 );
43
44         jQuery.ajaxSetup({ timeout: 0 });
45
46         stop();
47
48         setTimeout(function(){
49                 jQuery('#foo').ajaxStart(function(){
50                         ok( true, "ajaxStart" );
51                 }).ajaxStop(function(){
52                         ok( true, "ajaxStop" );
53                         start();
54                 }).ajaxSend(function(){
55                         ok( true, "ajaxSend" );
56                 }).ajaxComplete(function(){
57                         ok( true, "ajaxComplete" );
58                 }).ajaxError(function(){
59                         ok( false, "ajaxError" );
60                 }).ajaxSuccess(function(){
61                         ok( true, "ajaxSuccess" );
62                 });
63
64                 jQuery.ajax( url("data/name.html") , {
65                         beforeSend: function(){ ok(true, "beforeSend"); },
66                         success: function(){ ok(true, "success"); },
67                         error: function(){ ok(false, "error"); },
68                         complete: function(){ ok(true, "complete"); }
69                 });
70         }, 13);
71 });
72
73 test("jQuery.ajax() - success callbacks (late binding)", function() {
74         expect( 8 );
75
76         jQuery.ajaxSetup({ timeout: 0 });
77
78         stop();
79
80         setTimeout(function(){
81                 jQuery('#foo').ajaxStart(function(){
82                         ok( true, "ajaxStart" );
83                 }).ajaxStop(function(){
84                         ok( true, "ajaxStop" );
85                         start();
86                 }).ajaxSend(function(){
87                         ok( true, "ajaxSend" );
88                 }).ajaxComplete(function(){
89                         ok( true, "ajaxComplete" );
90                 }).ajaxError(function(){
91                         ok( false, "ajaxError" );
92                 }).ajaxSuccess(function(){
93                         ok( true, "ajaxSuccess" );
94                 });
95
96                 jQuery.ajax({
97                         url: url("data/name.html"),
98                         beforeSend: function(){ ok(true, "beforeSend"); }
99                 })
100                         .complete(function(){ ok(true, "complete"); })
101                         .success(function(){ ok(true, "success"); })
102                         .error(function(){ ok(false, "error"); });
103         }, 13);
104 });
105
106 test("jQuery.ajax() - success callbacks (oncomplete binding)", function() {
107         expect( 8 );
108
109         jQuery.ajaxSetup({ timeout: 0 });
110
111         stop();
112
113         setTimeout(function(){
114                 jQuery('#foo').ajaxStart(function(){
115                         ok( true, "ajaxStart" );
116                 }).ajaxStop(function(){
117                         ok( true, "ajaxStop" );
118                 }).ajaxSend(function(){
119                         ok( true, "ajaxSend" );
120                 }).ajaxComplete(function(){
121                         ok( true, "ajaxComplete" );
122                 }).ajaxError(function(){
123                         ok( false, "ajaxError" );
124                 }).ajaxSuccess(function(){
125                         ok( true, "ajaxSuccess" );
126                 });
127
128                 jQuery.ajax({
129                         url: url("data/name.html"),
130                         beforeSend: function(){ ok(true, "beforeSend"); },
131                         complete: function(xhr) {
132                                 xhr
133                                 .complete(function(){ ok(true, "complete"); })
134                                 .success(function(){ ok(true, "success"); })
135                                 .error(function(){ ok(false, "error"); })
136                                 .complete(function(){ start(); });
137                         }
138                 });
139         }, 13);
140 });
141
142 test("jQuery.ajax() - success callbacks (very late binding)", function() {
143         expect( 8 );
144
145         jQuery.ajaxSetup({ timeout: 0 });
146
147         stop();
148
149         setTimeout(function(){
150                 jQuery('#foo').ajaxStart(function(){
151                         ok( true, "ajaxStart" );
152                 }).ajaxStop(function(){
153                         ok( true, "ajaxStop" );
154                 }).ajaxSend(function(){
155                         ok( true, "ajaxSend" );
156                 }).ajaxComplete(function(){
157                         ok( true, "ajaxComplete" );
158                 }).ajaxError(function(){
159                         ok( false, "ajaxError" );
160                 }).ajaxSuccess(function(){
161                         ok( true, "ajaxSuccess" );
162                 });
163
164                 jQuery.ajax({
165                         url: url("data/name.html"),
166                         beforeSend: function(){ ok(true, "beforeSend"); },
167                         complete: function(xhr) {
168                                 setTimeout (function() {
169                                         xhr
170                                         .complete(function(){ ok(true, "complete"); })
171                                         .success(function(){ ok(true, "success"); })
172                                         .error(function(){ ok(false, "error"); })
173                                         .complete(function(){ start(); });
174                                 },100);
175                         }
176                 });
177         }, 13);
178 });
179
180 test("jQuery.ajax() - success callbacks (order)", function() {
181         expect( 1 );
182
183         jQuery.ajaxSetup({ timeout: 0 });
184
185         stop();
186
187         var testString = "";
188
189         setTimeout(function(){
190                 jQuery.ajax({
191                         url: url("data/name.html"),
192                         success: function( _1 , _2 , xhr ) {
193                                 xhr.success(function() {
194                                         xhr.success(function() {
195                                                 testString += "E";
196                                         });
197                                         testString += "D";
198                                 });
199                                 testString += "A";
200                         },
201                         complete: function() {
202                                 strictEqual(testString, "ABCDE", "Proper order");
203                                 start();
204                         }
205                 }).success(function() {
206                         testString += "B";
207                 }).success(function() {
208                         testString += "C";
209                 });
210         }, 13);
211 });
212
213 test("jQuery.ajax() - error callbacks", function() {
214         expect( 8 );
215         stop();
216
217         jQuery('#foo').ajaxStart(function(){
218                 ok( true, "ajaxStart" );
219         }).ajaxStop(function(){
220                 ok( true, "ajaxStop" );
221                 start();
222         }).ajaxSend(function(){
223                 ok( true, "ajaxSend" );
224         }).ajaxComplete(function(){
225                 ok( true, "ajaxComplete" );
226         }).ajaxError(function(){
227                 ok( true, "ajaxError" );
228         }).ajaxSuccess(function(){
229                 ok( false, "ajaxSuccess" );
230         });
231
232         jQuery.ajaxSetup({ timeout: 500 });
233
234         jQuery.ajax({
235                 url: url("data/name.php?wait=5"),
236                 beforeSend: function(){ ok(true, "beforeSend"); },
237                 success: function(){ ok(false, "success"); },
238                 error: function(){ ok(true, "error"); },
239                 complete: function(){ ok(true, "complete"); }
240         });
241 });
242
243 test("jQuery.ajax() - responseText on error", function() {
244
245         expect( 1 );
246
247         stop();
248
249         jQuery.ajax({
250                 url: url("data/errorWithText.php"),
251                 error: function(xhr) {
252                         strictEqual( xhr.responseText , "plain text message" , "Test jXHR.responseText is filled for HTTP errors" );
253                 },
254                 complete: function() {
255                         start();
256                 }
257         });
258 });
259
260 test(".ajax() - retry with jQuery.ajax( this )", function() {
261
262         expect( 1 );
263
264         stop();
265
266         var firstTime = 1;
267
268         jQuery.ajax({
269                 url: url("data/errorWithText.php"),
270                 error: function() {
271                         if ( firstTime ) {
272                                 firstTime = 0;
273                                 jQuery.ajax( this );
274                         } else {
275                                 ok( true , "Test retrying with jQuery.ajax(this) works" );
276                                 start();
277                         }
278                 }
279         });
280
281 });
282
283 test(".ajax() - headers" , function() {
284
285         expect( 2 );
286
287         stop();
288
289         var requestHeaders = {
290                 siMPle: "value",
291                 "SometHing-elsE": "other value",
292                 OthEr: "something else"
293                 },
294                 list = [],
295                 i;
296
297         for( i in requestHeaders ) {
298                 list.push( i );
299         }
300
301         jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
302                 headers: requestHeaders,
303                 success: function( data , _ , xhr ) {
304                         var tmp = [];
305                         for ( i in requestHeaders ) {
306                                 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
307                         }
308                         tmp = tmp.join( "" );
309
310                         equals( data , tmp , "Headers were sent" );
311                         equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
312                         start();
313                 },
314                 error: function(){ ok(false, "error"); }
315         });
316
317 });
318
319 test(".ajax() - Accept header" , function() {
320
321         expect( 1 );
322
323         stop();
324
325         jQuery.ajax(url("data/headers.php?keys=accept"), {
326                 headers: {
327                         Accept: "very wrong accept value"
328                 },
329                 beforeSend: function( xhr ) {
330                         xhr.setRequestHeader( "Accept", "*/*" );
331                 },
332                 success: function( data ) {
333                         strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
334                         start();
335                 },
336                 error: function(){ ok(false, "error"); }
337         });
338
339 });
340
341 test(".ajax() - contentType" , function() {
342
343         expect( 2 );
344
345         stop();
346
347         var count = 2;
348
349         function restart() {
350                 if ( ! --count ) {
351                         start();
352                 }
353         }
354
355         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
356                 contentType: "test",
357                 success: function( data ) {
358                         strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
359                 },
360                 complete: function() {
361                         restart();
362                 }
363         });
364
365         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
366                 contentType: false,
367                 success: function( data ) {
368                         strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
369                 },
370                 complete: function() {
371                         restart();
372                 }
373         });
374
375 });
376
377 test(".ajax() - hash", function() {
378         expect(3);
379
380         jQuery.ajax({
381                 url: "data/name.html#foo",
382                 beforeSend: function( xhr, settings ) {
383                         equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
384                         return false;
385                 }
386         });
387
388         jQuery.ajax({
389                 url: "data/name.html?abc#foo",
390                 beforeSend: function( xhr, settings ) {
391                 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
392                         return false;
393                 }
394         });
395
396         jQuery.ajax({
397                 url: "data/name.html?abc#foo",
398                 data: { "test": 123 },
399                 beforeSend: function( xhr, settings ) {
400                         equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
401                         return false;
402                 }
403         });
404 });
405
406 test("jQuery ajax - cross-domain detection", function() {
407
408         expect( 4 );
409
410         var loc = document.location,
411                 otherPort = loc.port === 666 ? 667 : 666,
412                 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
413
414         jQuery.ajax({
415                 dataType: "jsonp",
416                 url: otherProtocol + "//" + loc.host,
417                 beforeSend: function( _ , s ) {
418                         ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
419                         return false;
420                 }
421         });
422
423         jQuery.ajax({
424                 dataType: "jsonp",
425                 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
426                 beforeSend: function( _ , s ) {
427                         ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
428                         return false;
429                 }
430         });
431
432         jQuery.ajax({
433                 dataType: "jsonp",
434                 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
435                 beforeSend: function( _ , s ) {
436                         ok( s.crossDomain , "Test different ports are detected as cross-domain" );
437                         return false;
438                 }
439         });
440
441         jQuery.ajax({
442                 dataType: "jsonp",
443                 url: loc.protocol + "//" + loc.host,
444                 crossDomain: true,
445                 beforeSend: function( _ , s ) {
446                         ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
447                         return false;
448                 }
449         });
450
451 });
452
453 test(".ajax() - 304", function() {
454         expect( 1 );
455         stop();
456
457         jQuery.ajax({
458                 url: url("data/notmodified.php"),
459                 success: function(){ ok(true, "304 ok"); },
460                 // Do this because opera simply refuses to implement 304 handling :(
461                 // A feature-driven way of detecting this would be appreciated
462                 // See: http://gist.github.com/599419
463                 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
464                 complete: function(xhr){ start(); }
465         });
466 });
467
468 test(".load()) - 404 error callbacks", function() {
469         expect( 6 );
470         stop();
471
472         jQuery('#foo').ajaxStart(function(){
473                 ok( true, "ajaxStart" );
474         }).ajaxStop(function(){
475                 ok( true, "ajaxStop" );
476                 start();
477         }).ajaxSend(function(){
478                 ok( true, "ajaxSend" );
479         }).ajaxComplete(function(){
480                 ok( true, "ajaxComplete" );
481         }).ajaxError(function(){
482                 ok( true, "ajaxError" );
483         }).ajaxSuccess(function(){
484                 ok( false, "ajaxSuccess" );
485         });
486
487         jQuery("<div/>").load("data/404.html", function(){
488                 ok(true, "complete");
489         });
490 });
491
492 test("jQuery.ajax() - abort", function() {
493         expect( 8 );
494         stop();
495
496         jQuery('#foo').ajaxStart(function(){
497                 ok( true, "ajaxStart" );
498         }).ajaxStop(function(){
499                 ok( true, "ajaxStop" );
500                 start();
501         }).ajaxSend(function(){
502                 ok( true, "ajaxSend" );
503         }).ajaxComplete(function(){
504                 ok( true, "ajaxComplete" );
505         });
506
507         var xhr = jQuery.ajax({
508                 url: url("data/name.php?wait=5"),
509                 beforeSend: function(){ ok(true, "beforeSend"); },
510                 complete: function(){ ok(true, "complete"); }
511         });
512
513         equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
514
515         xhr.abort();
516         equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
517 });
518
519 test("Ajax events with context", function() {
520         expect(14);
521
522         stop();
523         var context = document.createElement("div");
524
525         function event(e){
526                 equals( this, context, e.type );
527         }
528
529         function callback(msg){
530                 return function(){
531                         equals( this, context, "context is preserved on callback " + msg );
532                 };
533         }
534
535         function nocallback(msg){
536                 return function(){
537                         equals( typeof this.url, "string", "context is settings on callback " + msg );
538                 };
539         }
540
541         jQuery('#foo').add(context)
542                         .ajaxSend(event)
543                         .ajaxComplete(event)
544                         .ajaxError(event)
545                         .ajaxSuccess(event);
546
547         jQuery.ajax({
548                 url: url("data/name.html"),
549                 beforeSend: callback("beforeSend"),
550                 success: callback("success"),
551                 error: callback("error"),
552                 complete:function(){
553                         callback("complete").call(this);
554
555                         jQuery.ajax({
556                                 url: url("data/404.html"),
557                                 context: context,
558                                 beforeSend: callback("beforeSend"),
559                                 error: callback("error"),
560                                 complete: function(){
561                                         callback("complete").call(this);
562
563                                         jQuery('#foo').add(context).unbind();
564
565                                         jQuery.ajax({
566                                                 url: url("data/404.html"),
567                                                 beforeSend: nocallback("beforeSend"),
568                                                 error: nocallback("error"),
569                                                 complete: function(){
570                                                         nocallback("complete").call(this);
571                                                         start();
572                                                 }
573                                         });
574                                 }
575                         });
576                 },
577                 context:context
578         });
579 });
580
581 test("jQuery.ajax context modification", function() {
582         expect(1);
583
584         stop();
585
586         var obj = {};
587
588         jQuery.ajax({
589                 url: url("data/name.html"),
590                 context: obj,
591                 beforeSend: function(){
592                         this.test = "foo";
593                 },
594                 complete: function() {
595                         start();
596                 }
597         });
598
599         equals( obj.test, "foo", "Make sure the original object is maintained." );
600 });
601
602 test("jQuery.ajax context modification through ajaxSetup", function() {
603         expect(4);
604
605         stop();
606
607         var obj = {};
608
609         jQuery.ajaxSetup({
610                 context: obj
611         });
612
613         strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
614
615         jQuery.ajax({
616                 url: url("data/name.html"),
617                 complete: function() {
618                         strictEqual( this, obj, "Make sure the original object is maintained." );
619                         jQuery.ajax({
620                                 url: url("data/name.html"),
621                                 context: {},
622                                 complete: function() {
623                                         ok( this !== obj, "Make sure overidding context is possible." );
624                                         jQuery.ajaxSetup({
625                                                 context: false
626                                         });
627                                         jQuery.ajax({
628                                                 url: url("data/name.html"),
629                                                 beforeSend: function(){
630                                                         this.test = "foo2";
631                                                 },
632                                                 complete: function() {
633                                                         ok( this !== obj, "Make sure unsetting context is possible." );
634                                                         start();
635                                                 }
636                                         });
637                                 }
638                         });
639                 }
640         });
641 });
642
643 test("jQuery.ajax() - disabled globals", function() {
644         expect( 3 );
645         stop();
646
647         jQuery('#foo').ajaxStart(function(){
648                 ok( false, "ajaxStart" );
649         }).ajaxStop(function(){
650                 ok( false, "ajaxStop" );
651         }).ajaxSend(function(){
652                 ok( false, "ajaxSend" );
653         }).ajaxComplete(function(){
654                 ok( false, "ajaxComplete" );
655         }).ajaxError(function(){
656                 ok( false, "ajaxError" );
657         }).ajaxSuccess(function(){
658                 ok( false, "ajaxSuccess" );
659         });
660
661         jQuery.ajax({
662                 global: false,
663                 url: url("data/name.html"),
664                 beforeSend: function(){ ok(true, "beforeSend"); },
665                 success: function(){ ok(true, "success"); },
666                 error: function(){ ok(false, "error"); },
667                 complete: function(){
668                   ok(true, "complete");
669                   setTimeout(function(){ start(); }, 13);
670                 }
671         });
672 });
673
674 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
675         expect(3);
676         stop();
677         jQuery.ajax({
678           url: url("data/with_fries.xml"),
679           dataType: "xml",
680           success: function(resp) {
681                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
682                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
683                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
684                 start();
685           }
686         });
687 });
688
689 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
690         expect(3);
691         stop();
692         jQuery.ajax({
693           url: url("data/with_fries_over_jsonp.php"),
694           dataType: "jsonp xml",
695           success: function(resp) {
696                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
697                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
698                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
699                 start();
700           },
701           error: function(_1,_2,error) {
702                 ok( false, error );
703                 start();
704           }
705         });
706 });
707
708 test("jQuery.ajax - HEAD requests", function() {
709         expect(2);
710
711         stop();
712         jQuery.ajax({
713                 url: url("data/name.html"),
714                 type: "HEAD",
715                 success: function(data, status, xhr){
716                         var h = xhr.getAllResponseHeaders();
717                         ok( /Date/i.test(h), 'No Date in HEAD response' );
718
719                         jQuery.ajax({
720                                 url: url("data/name.html"),
721                                 data: { whip_it: "good" },
722                                 type: "HEAD",
723                                 success: function(data, status, xhr){
724                                         var h = xhr.getAllResponseHeaders();
725                                         ok( /Date/i.test(h), 'No Date in HEAD response with data' );
726                                         start();
727                                 }
728                         });
729                 }
730         });
731
732 });
733
734 test("jQuery.ajax - beforeSend", function() {
735         expect(1);
736         stop();
737
738         var check = false;
739
740         jQuery.ajaxSetup({ timeout: 0 });
741
742         jQuery.ajax({
743                 url: url("data/name.html"),
744                 beforeSend: function(xml) {
745                         check = true;
746                 },
747                 success: function(data) {
748                         ok( check, "check beforeSend was executed" );
749                         start();
750                 }
751         });
752 });
753
754 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
755         expect(2);
756         var request = jQuery.ajax({
757                 url: url("data/name.html"),
758                 beforeSend: function() {
759                         ok( true, "beforeSend got called, canceling" );
760                         return false;
761                 },
762                 success: function() {
763                         ok( false, "request didn't get canceled" );
764                 },
765                 complete: function() {
766                         ok( false, "request didn't get canceled" );
767                 },
768                 error: function() {
769                         ok( false, "request didn't get canceled" );
770                 }
771         });
772         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
773 });
774
775 test("jQuery.ajax - beforeSend, cancel request manually", function() {
776         expect(2);
777         var request = jQuery.ajax({
778                 url: url("data/name.html"),
779                 beforeSend: function(xhr) {
780                         ok( true, "beforeSend got called, canceling" );
781                         xhr.abort();
782                 },
783                 success: function() {
784                         ok( false, "request didn't get canceled" );
785                 },
786                 complete: function() {
787                         ok( false, "request didn't get canceled" );
788                 },
789                 error: function() {
790                         ok( false, "request didn't get canceled" );
791                 }
792         });
793         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
794 });
795
796 window.foobar = null;
797 window.testFoo = undefined;
798
799 test("jQuery.ajax - dataType html", function() {
800         expect(5);
801         stop();
802
803         var verifyEvaluation = function() {
804                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
805                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
806
807                 start();
808         };
809
810         jQuery.ajax({
811           dataType: "html",
812           url: url("data/test.html"),
813           success: function(data) {
814                 jQuery("#ap").html(data);
815                 ok( data.match(/^html text/), 'Check content for datatype html' );
816                 setTimeout(verifyEvaluation, 600);
817           }
818         });
819 });
820
821 test("serialize()", function() {
822         expect(5);
823
824         // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
825         jQuery("#search").after(
826                 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
827                 '<input type="number" id="html5number" name="number" value="43" />'
828         );
829
830         equals( jQuery('#form').serialize(),
831                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
832                 'Check form serialization as query string');
833
834         equals( jQuery('#form :input').serialize(),
835                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
836                 'Check input serialization as query string');
837
838         equals( jQuery('#testForm').serialize(),
839                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
840                 'Check form serialization as query string');
841
842         equals( jQuery('#testForm :input').serialize(),
843                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
844                 'Check input serialization as query string');
845
846         equals( jQuery('#form, #testForm').serialize(),
847                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
848                 'Multiple form serialization as query string');
849
850   /* Temporarily disabled. Opera 10 has problems with form serialization.
851         equals( jQuery('#form, #testForm :input').serialize(),
852                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
853                 'Mixed form/input serialization as query string');
854         */
855         jQuery("#html5email, #html5number").remove();
856 });
857
858 test("jQuery.param()", function() {
859         expect(22);
860
861         equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
862
863         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
864         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
865
866         params = {someName: [1, 2, 3], regularThing: "blah" };
867         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
868
869         params = {foo: ['a', 'b', 'c']};
870         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
871
872         params = {foo: ["baz", 42, "All your base are belong to us"] };
873         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
874
875         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
876         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
877
878         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
879         equals( decodeURIComponent( jQuery.param(params) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure" );
880
881         params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
882         equals( decodeURIComponent( jQuery.param(params) ), "a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17", "nested arrays" );
883
884         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
885         equals( jQuery.param(params,true), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure, forced traditional" );
886
887         equals( decodeURIComponent( jQuery.param({ a: [1,2,3], 'b[]': [4,5,6], 'c[d]': [7,8,9], e: { f: [10], g: [11,12], h: 13 } }) ), "a[]=1&a[]=2&a[]=3&b[]=4&b[]=5&b[]=6&c[d][]=7&c[d][]=8&c[d][]=9&e[f][]=10&e[g][]=11&e[g][]=12&e[h]=13", "Make sure params are not double-encoded." );
888
889         // Make sure empty arrays and objects are handled #6481
890         equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
891         equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
892         equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
893
894         jQuery.ajaxSetup({ traditional: true });
895
896         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
897         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
898
899         params = {someName: [1, 2, 3], regularThing: "blah" };
900         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
901
902         params = {foo: ['a', 'b', 'c']};
903         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
904
905         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
906         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
907
908         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
909         equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
910
911         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
912         equals( jQuery.param(params), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure" );
913
914         params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
915         equals( jQuery.param(params), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject+Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
916
917         params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
918         equals( decodeURIComponent( jQuery.param(params,false) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
919
920         params = { param1: null };
921         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
922 });
923
924 test("synchronous request", function() {
925         expect(1);
926         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
927 });
928
929 test("synchronous request with callbacks", function() {
930         expect(2);
931         var result;
932         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
933         ok( /^{ "data"/.test( result ), "check returned text" );
934 });
935
936 test("pass-through request object", function() {
937         expect(8);
938         stop();
939
940         var target = "data/name.html";
941         var successCount = 0;
942         var errorCount = 0;
943         var errorEx = "";
944         var success = function() {
945                 successCount++;
946         };
947         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
948                 errorCount++;
949                 errorEx += ": " + xml.status;
950         });
951         jQuery("#foo").one('ajaxStop', function () {
952                 equals(successCount, 5, "Check all ajax calls successful");
953                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
954                 jQuery("#foo").unbind('ajaxError');
955
956                 start();
957         });
958
959         ok( jQuery.get(url(target), success), "get" );
960         ok( jQuery.post(url(target), success), "post" );
961         ok( jQuery.getScript(url("data/test.js"), success), "script" );
962         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
963         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
964 });
965
966 test("ajax cache", function () {
967         expect(18);
968
969         stop();
970
971         var count = 0;
972
973         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
974                 var re = /_=(.*?)(&|$)/g;
975                 var oldOne = null;
976                 for (var i = 0; i < 6; i++) {
977                         var ret = re.exec(s.url);
978                         if (!ret) {
979                                 break;
980                         }
981                         oldOne = ret[1];
982                 }
983                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
984                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
985                 if(++count == 6)
986                         start();
987         });
988
989         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
990         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
991         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
992         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
993         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
994         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
995 });
996
997 /*
998  * Test disabled.
999  * The assertions expect that the passed-in object will be modified,
1000  * which shouldn't be the case. Fixes #5439.
1001 test("global ajaxSettings", function() {
1002         expect(2);
1003
1004         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1005         var orig = { url: "data/with_fries.xml" };
1006         var t;
1007
1008         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1009
1010         t = jQuery.extend({}, orig);
1011         t.data = {};
1012         jQuery.ajax(t);
1013         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1014
1015         t = jQuery.extend({}, orig);
1016         t.data = { zoo: 'a', ping: 'b' };
1017         jQuery.ajax(t);
1018         ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" );
1019
1020         jQuery.ajaxSettings = tmp;
1021 });
1022 */
1023
1024 test("load(String)", function() {
1025         expect(1);
1026         stop(); // check if load can be called with only url
1027         jQuery('#first').load("data/name.html", start);
1028 });
1029
1030 test("load('url selector')", function() {
1031         expect(1);
1032         stop(); // check if load can be called with only url
1033         jQuery('#first').load("data/test3.html div.user", function(){
1034                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1035                 start();
1036         });
1037 });
1038
1039 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1040         expect(1);
1041         stop();
1042         jQuery.ajaxSetup({ dataType: "json" });
1043         jQuery("#first").ajaxComplete(function (e, xml, s) {
1044                 equals( s.dataType, "html", "Verify the load() dataType was html" );
1045                 jQuery("#first").unbind("ajaxComplete");
1046                 jQuery.ajaxSetup({ dataType: "" });
1047                 start();
1048         });
1049         jQuery('#first').load("data/test3.html");
1050 });
1051
1052 test("load(String, Function) - simple: inject text into DOM", function() {
1053         expect(2);
1054         stop();
1055         jQuery('#first').load(url("data/name.html"), function() {
1056                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1057                 start();
1058         });
1059 });
1060
1061 test("load(String, Function) - check scripts", function() {
1062         expect(7);
1063         stop();
1064
1065         var verifyEvaluation = function() {
1066                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1067                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1068
1069                 start();
1070         };
1071         jQuery('#first').load(url('data/test.html'), function() {
1072                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1073                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1074                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1075                 setTimeout(verifyEvaluation, 600);
1076         });
1077 });
1078
1079 test("load(String, Function) - check file with only a script tag", function() {
1080         expect(3);
1081         stop();
1082
1083         jQuery('#first').load(url('data/test2.html'), function() {
1084                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1085                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1086
1087                 start();
1088         });
1089 });
1090
1091 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1092         expect(2);
1093         stop();
1094         jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1095         var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1096                 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1097                 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1098                 jQuery.ajaxSetup({ dataFilter: 0 });
1099                 start();
1100         });
1101 });
1102
1103 test("load(String, Object, Function)", function() {
1104         expect(2);
1105         stop();
1106
1107         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1108                 var $post = jQuery(this).find('#post');
1109                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1110                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1111                 start();
1112         });
1113 });
1114
1115 test("load(String, String, Function)", function() {
1116         expect(2);
1117         stop();
1118
1119         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1120                 var $get = jQuery(this).find('#get');
1121                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1122                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
1123                 start();
1124         });
1125 });
1126
1127 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1128         expect(2);
1129         stop();
1130         jQuery.get(url('data/dashboard.xml'), function(xml) {
1131                 var content = [];
1132                 jQuery('tab', xml).each(function() {
1133                         content.push(jQuery(this).text());
1134                 });
1135                 equals( content[0], 'blabla', 'Check first tab');
1136                 equals( content[1], 'blublu', 'Check second tab');
1137                 start();
1138         });
1139 });
1140
1141 test("jQuery.getScript(String, Function) - with callback", function() {
1142         expect(2);
1143         stop();
1144         jQuery.getScript(url("data/test.js"), function() {
1145                 equals( foobar, "bar", 'Check if script was evaluated' );
1146                 setTimeout(start, 100);
1147         });
1148 });
1149
1150 test("jQuery.getScript(String, Function) - no callback", function() {
1151         expect(1);
1152         stop();
1153         jQuery.getScript(url("data/test.js"), function(){
1154                 start();
1155         });
1156 });
1157
1158 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1159
1160         test("jQuery.ajax() - JSONP, " + label, function() {
1161                 expect(17);
1162
1163                 var count = 0;
1164                 function plus(){ if ( ++count == 17 ) start(); }
1165
1166                 stop();
1167
1168                 jQuery.ajax({
1169                         url: "data/jsonp.php",
1170                         dataType: "jsonp",
1171                         crossDomain: crossDomain,
1172                         success: function(data){
1173                                 ok( data.data, "JSON results returned (GET, no callback)" );
1174                                 plus();
1175                         },
1176                         error: function(data){
1177                                 ok( false, "Ajax error JSON (GET, no callback)" );
1178                                 plus();
1179                         }
1180                 });
1181
1182                 jQuery.ajax({
1183                         url: "data/jsonp.php?callback=?",
1184                         dataType: "jsonp",
1185                         crossDomain: crossDomain,
1186                         success: function(data){
1187                                 ok( data.data, "JSON results returned (GET, url callback)" );
1188                                 plus();
1189                         },
1190                         error: function(data){
1191                                 ok( false, "Ajax error JSON (GET, url callback)" );
1192                                 plus();
1193                         }
1194                 });
1195
1196                 jQuery.ajax({
1197                         url: "data/jsonp.php",
1198                         dataType: "jsonp",
1199                         crossDomain: crossDomain,
1200                         data: "callback=?",
1201                         success: function(data){
1202                                 ok( data.data, "JSON results returned (GET, data callback)" );
1203                                 plus();
1204                         },
1205                         error: function(data){
1206                                 ok( false, "Ajax error JSON (GET, data callback)" );
1207                                 plus();
1208                         }
1209                 });
1210
1211                 jQuery.ajax({
1212                         url: "data/jsonp.php?callback=??",
1213                         dataType: "jsonp",
1214                         crossDomain: crossDomain,
1215                         success: function(data){
1216                                 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1217                                 plus();
1218                         },
1219                         error: function(data){
1220                                 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1221                                 plus();
1222                         }
1223                 });
1224
1225                 jQuery.ajax({
1226                         url: "data/jsonp.php",
1227                         dataType: "jsonp",
1228                         crossDomain: crossDomain,
1229                         data: "callback=??",
1230                         success: function(data){
1231                                 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1232                                 plus();
1233                         },
1234                         error: function(data){
1235                                 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1236                                 plus();
1237                         }
1238                 });
1239
1240                 jQuery.ajax({
1241                         url: "data/jsonp.php/??",
1242                         dataType: "jsonp",
1243                         crossDomain: crossDomain,
1244                         success: function(data){
1245                                 ok( data.data, "JSON results returned (GET, REST-like)" );
1246                                 plus();
1247                         },
1248                         error: function(data){
1249                                 ok( false, "Ajax error JSON (GET, REST-like)" );
1250                                 plus();
1251                         }
1252                 });
1253
1254                 jQuery.ajax({
1255                         url: "data/jsonp.php/???json=1",
1256                         dataType: "jsonp",
1257                         crossDomain: crossDomain,
1258                         success: function(data){
1259                                 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1260                                 plus();
1261                         },
1262                         error: function(data){
1263                                 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1264                                 plus();
1265                         }
1266                 });
1267
1268                 jQuery.ajax({
1269                         url: "data/jsonp.php",
1270                         dataType: "jsonp",
1271                         crossDomain: crossDomain,
1272                         data: {
1273                                 callback: "?"
1274                         },
1275                         success: function(data){
1276                                 ok( data.data, "JSON results returned (GET, processed data callback)" );
1277                                 plus();
1278                         },
1279                         error: function(data){
1280                                 ok( false, "Ajax error JSON (GET, processed data callback)" );
1281                                 plus();
1282                         }
1283                 });
1284
1285                 jQuery.ajax({
1286                         url: "data/jsonp.php",
1287                         dataType: "jsonp",
1288                         crossDomain: crossDomain,
1289                         jsonp: "callback",
1290                         success: function(data){
1291                                 ok( data.data, "JSON results returned (GET, data obj callback)" );
1292                                 plus();
1293                         },
1294                         error: function(data){
1295                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1296                                 plus();
1297                         }
1298                 });
1299
1300                 window.jsonpResults = function(data) {
1301                         ok( data.data, "JSON results returned (GET, custom callback function)" );
1302                         window.jsonpResults = undefined;
1303                         plus();
1304                 };
1305
1306                 jQuery.ajax({
1307                         url: "data/jsonp.php",
1308                         dataType: "jsonp",
1309                         crossDomain: crossDomain,
1310                         jsonpCallback: "jsonpResults",
1311                         success: function(data){
1312                                 ok( data.data, "JSON results returned (GET, custom callback name)" );
1313                                 plus();
1314                         },
1315                         error: function(data){
1316                                 ok( false, "Ajax error JSON (GET, custom callback name)" );
1317                                 plus();
1318                         }
1319                 });
1320
1321                 jQuery.ajax({
1322                         type: "POST",
1323                         url: "data/jsonp.php",
1324                         dataType: "jsonp",
1325                         crossDomain: crossDomain,
1326                         success: function(data){
1327                                 ok( data.data, "JSON results returned (POST, no callback)" );
1328                                 plus();
1329                         },
1330                         error: function(data){
1331                                 ok( false, "Ajax error JSON (GET, data obj callback)" );
1332                                 plus();
1333                         }
1334                 });
1335
1336                 jQuery.ajax({
1337                         type: "POST",
1338                         url: "data/jsonp.php",
1339                         data: "callback=?",
1340                         dataType: "jsonp",
1341                         crossDomain: crossDomain,
1342                         success: function(data){
1343                                 ok( data.data, "JSON results returned (POST, data callback)" );
1344                                 plus();
1345                         },
1346                         error: function(data){
1347                                 ok( false, "Ajax error JSON (POST, data callback)" );
1348                                 plus();
1349                         }
1350                 });
1351
1352                 jQuery.ajax({
1353                         type: "POST",
1354                         url: "data/jsonp.php",
1355                         jsonp: "callback",
1356                         dataType: "jsonp",
1357                         crossDomain: crossDomain,
1358                         success: function(data){
1359                                 ok( data.data, "JSON results returned (POST, data obj callback)" );
1360                                 plus();
1361                         },
1362                         error: function(data){
1363                                 ok( false, "Ajax error JSON (POST, data obj callback)" );
1364                                 plus();
1365                         }
1366                 });
1367
1368                 //#7578
1369                 jQuery.ajax({
1370                         url: "data/jsonp.php",
1371                         dataType: "jsonp",
1372                         crossDomain: crossDomain,
1373                         beforeSend: function(){
1374                                 strictEqual( this.cache, false, "cache must be false on JSON request" );
1375                                 plus();
1376                                 return false;
1377                         }
1378                 });
1379
1380                 jQuery.ajax({
1381                         url: "data/jsonp.php?callback=XXX",
1382                         dataType: "jsonp",
1383                         jsonp: false,
1384                         jsonpCallback: "XXX",
1385                         crossDomain: crossDomain,
1386                         beforeSend: function() {
1387                                 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1388                                         "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1389                                 plus();
1390                         },
1391                         success: function(data){
1392                                 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1393                                 plus();
1394                         },
1395                         error: function(data){
1396                                 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1397                                 plus();
1398                         }
1399                 });
1400
1401         });
1402 });
1403
1404 test("jQuery.ajax() - script, Remote", function() {
1405         expect(2);
1406
1407         var base = window.location.href.replace(/[^\/]*$/, "");
1408
1409         stop();
1410
1411         jQuery.ajax({
1412                 url: base + "data/test.js",
1413                 dataType: "script",
1414                 success: function(data){
1415                         ok( foobar, "Script results returned (GET, no callback)" );
1416                         start();
1417                 }
1418         });
1419 });
1420
1421 test("jQuery.ajax() - script, Remote with POST", function() {
1422         expect(3);
1423
1424         var base = window.location.href.replace(/[^\/]*$/, "");
1425
1426         stop();
1427
1428         jQuery.ajax({
1429                 url: base + "data/test.js",
1430                 type: "POST",
1431                 dataType: "script",
1432                 success: function(data, status){
1433                         ok( foobar, "Script results returned (POST, no callback)" );
1434                         equals( status, "success", "Script results returned (POST, no callback)" );
1435                         start();
1436                 },
1437                 error: function(xhr) {
1438                         ok( false, "ajax error, status code: " + xhr.status );
1439                         start();
1440                 }
1441         });
1442 });
1443
1444 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1445         expect(2);
1446
1447         var base = window.location.href.replace(/[^\/]*$/, "");
1448         base = base.replace(/^.*?\/\//, "//");
1449
1450         stop();
1451
1452         jQuery.ajax({
1453                 url: base + "data/test.js",
1454                 dataType: "script",
1455                 success: function(data){
1456                         ok( foobar, "Script results returned (GET, no callback)" );
1457                         start();
1458                 }
1459         });
1460 });
1461
1462 test("jQuery.ajax() - malformed JSON", function() {
1463         expect(2);
1464
1465         stop();
1466
1467         jQuery.ajax({
1468                 url: "data/badjson.js",
1469                 dataType: "json",
1470                 success: function(){
1471                         ok( false, "Success." );
1472                         start();
1473                 },
1474                 error: function(xhr, msg, detailedMsg) {
1475                         equals( "parsererror", msg, "A parse error occurred." );
1476                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1477                         start();
1478                 }
1479         });
1480 });
1481
1482 test("jQuery.ajax() - script by content-type", function() {
1483         expect(1);
1484
1485         stop();
1486
1487         jQuery.ajax({
1488                 url: "data/script.php",
1489                 data: { header: "script" },
1490                 success: function() {
1491                         start();
1492                 }
1493         });
1494 });
1495
1496 test("jQuery.ajax() - json by content-type", function() {
1497         expect(5);
1498
1499         stop();
1500
1501         jQuery.ajax({
1502                 url: "data/json.php",
1503                 data: { header: "json", json: "array" },
1504                 success: function( json ) {
1505                         ok( json.length >= 2, "Check length");
1506                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1507                         equals( json[0].age, 21, 'Check JSON: first, age' );
1508                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1509                         equals( json[1].age, 25, 'Check JSON: second, age' );
1510                         start();
1511                 }
1512         });
1513 });
1514
1515 test("jQuery.ajax() - json by content-type disabled with options", function() {
1516         expect(6);
1517
1518         stop();
1519
1520         jQuery.ajax({
1521                 url: url("data/json.php"),
1522                 data: { header: "json", json: "array" },
1523                 contents: {
1524                         json: false
1525                 },
1526                 success: function( text ) {
1527                         equals( typeof text , "string" , "json wasn't auto-determined" );
1528                         var json = jQuery.parseJSON( text );
1529                         ok( json.length >= 2, "Check length");
1530                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1531                         equals( json[0].age, 21, 'Check JSON: first, age' );
1532                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1533                         equals( json[1].age, 25, 'Check JSON: second, age' );
1534                         start();
1535                 }
1536         });
1537 });
1538
1539 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1540         expect(5);
1541         stop();
1542         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1543           ok( json.length >= 2, "Check length");
1544           equals( json[0].name, 'John', 'Check JSON: first, name' );
1545           equals( json[0].age, 21, 'Check JSON: first, age' );
1546           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1547           equals( json[1].age, 25, 'Check JSON: second, age' );
1548           start();
1549         });
1550 });
1551
1552 test("jQuery.getJSON(String, Function) - JSON object", function() {
1553         expect(2);
1554         stop();
1555         jQuery.getJSON(url("data/json.php"), function(json) {
1556           if (json && json.data) {
1557                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1558                   equals( json.data.length, 25, 'Check JSON: length' );
1559           }
1560           start();
1561         });
1562 });
1563
1564 test("jQuery.getJSON - Using Native JSON", function() {
1565         expect(2);
1566
1567         var old = window.JSON;
1568         JSON = {
1569                 parse: function(str){
1570                         ok( true, "Verifying that parse method was run" );
1571                         return true;
1572                 }
1573         };
1574
1575         stop();
1576         jQuery.getJSON(url("data/json.php"), function(json) {
1577                 window.JSON = old;
1578                 equals( json, true, "Verifying return value" );
1579                 start();
1580         });
1581 });
1582
1583 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1584         expect(2);
1585
1586         var base = window.location.href.replace(/[^\/]*$/, "");
1587
1588         stop();
1589         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1590           equals( json.data.lang, 'en', 'Check JSON: lang' );
1591           equals( json.data.length, 25, 'Check JSON: length' );
1592           start();
1593         });
1594 });
1595
1596 test("jQuery.post - data", function() {
1597         expect(2);
1598         stop();
1599
1600         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1601                 jQuery('math', xml).each(function() {
1602                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1603                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1604                 });
1605                 start();
1606         });
1607 });
1608
1609 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1610         expect(4);
1611         stop();
1612         var done = 0;
1613
1614         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1615           jQuery('math', xml).each(function() {
1616                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1617                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1618                  });
1619           if ( ++done === 2 ) start();
1620         });
1621
1622         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1623           jQuery('math', xml).each(function() {
1624                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1625                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1626                  });
1627           if ( ++done === 2 ) start();
1628         });
1629 });
1630
1631 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1632         stop();
1633
1634         var passed = 0;
1635
1636         jQuery.ajaxSetup({timeout: 1000});
1637
1638         var pass = function() {
1639                 passed++;
1640                 if ( passed == 2 ) {
1641                         ok( true, 'Check local and global callbacks after timeout' );
1642                         jQuery('#main').unbind("ajaxError");
1643                         start();
1644                 }
1645         };
1646
1647         var fail = function(a,b,c) {
1648                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1649                 start();
1650         };
1651
1652         jQuery('#main').ajaxError(pass);
1653
1654         jQuery.ajax({
1655           type: "GET",
1656           url: url("data/name.php?wait=5"),
1657           error: pass,
1658           success: fail
1659         });
1660
1661         // reset timeout
1662         jQuery.ajaxSetup({timeout: 0});
1663 });
1664
1665 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1666         stop();
1667         jQuery.ajaxSetup({timeout: 50});
1668
1669         jQuery.ajax({
1670           type: "GET",
1671           timeout: 15000,
1672           url: url("data/name.php?wait=1"),
1673           error: function() {
1674                    ok( false, 'Check for local timeout failed' );
1675                    start();
1676           },
1677           success: function() {
1678                 ok( true, 'Check for local timeout' );
1679                 start();
1680           }
1681         });
1682
1683         // reset timeout
1684         jQuery.ajaxSetup({timeout: 0});
1685 });
1686
1687 test("jQuery.ajax - simple get", function() {
1688         expect(1);
1689         stop();
1690         jQuery.ajax({
1691           type: "GET",
1692           url: url("data/name.php?name=foo"),
1693           success: function(msg){
1694                 equals( msg, 'bar', 'Check for GET' );
1695                 start();
1696           }
1697         });
1698 });
1699
1700 test("jQuery.ajax - simple post", function() {
1701         expect(1);
1702         stop();
1703         jQuery.ajax({
1704           type: "POST",
1705           url: url("data/name.php"),
1706           data: "name=peter",
1707           success: function(msg){
1708                 equals( msg, 'pan', 'Check for POST' );
1709                 start();
1710           }
1711         });
1712 });
1713
1714 test("ajaxSetup()", function() {
1715         expect(1);
1716         stop();
1717         jQuery.ajaxSetup({
1718                 url: url("data/name.php?name=foo"),
1719                 success: function(msg){
1720                         equals( msg, 'bar', 'Check for GET' );
1721                         start();
1722                 }
1723         });
1724         jQuery.ajax();
1725 });
1726
1727 /*
1728 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1729         stop();
1730         jQuery.ajax({
1731                 url: "data/name.php?wait=1",
1732                 timeout: 500,
1733                 error: function(request, status) {
1734                         ok( status != null, "status shouldn't be null in error handler" );
1735                         equals( "timeout", status );
1736                         start();
1737                 }
1738         });
1739 });
1740 */
1741
1742 test("data option: evaluate function values (#2806)", function() {
1743         stop();
1744         jQuery.ajax({
1745                 url: "data/echoQuery.php",
1746                 data: {
1747                         key: function() {
1748                                 return "value";
1749                         }
1750                 },
1751                 success: function(result) {
1752                         equals( result, "key=value" );
1753                         start();
1754                 }
1755         });
1756 });
1757
1758 test("data option: empty bodies for non-GET requests", function() {
1759         stop();
1760         jQuery.ajax({
1761                 url: "data/echoData.php",
1762                 data: undefined,
1763                 type: "post",
1764                 success: function(result) {
1765                         equals( result, "" );
1766                         start();
1767                 }
1768         });
1769 });
1770
1771 test("jQuery.ajax - If-Modified-Since support", function() {
1772         expect( 3 );
1773
1774         stop();
1775
1776         var url = "data/if_modified_since.php?ts=" + new Date();
1777
1778         jQuery.ajax({
1779                 url: url,
1780                 ifModified: true,
1781                 success: function(data, status) {
1782                         equals(status, "success");
1783
1784                         jQuery.ajax({
1785                                 url: url,
1786                                 ifModified: true,
1787                                 success: function(data, status) {
1788                                         if ( data === "FAIL" ) {
1789                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1790                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1791                                         } else {
1792                                                 equals(status, "notmodified");
1793                                                 ok(data == null, "response body should be empty");
1794                                         }
1795                                         start();
1796                         },
1797                                 error: function() {
1798                                         // Do this because opera simply refuses to implement 304 handling :(
1799                                         // A feature-driven way of detecting this would be appreciated
1800                                         // See: http://gist.github.com/599419
1801                                         ok(jQuery.browser.opera, "error");
1802                                         ok(jQuery.browser.opera, "error");
1803                                         start();
1804                         }
1805                         });
1806                 },
1807                 error: function() {
1808                         equals(false, "error");
1809                         // Do this because opera simply refuses to implement 304 handling :(
1810                         // A feature-driven way of detecting this would be appreciated
1811                         // See: http://gist.github.com/599419
1812                         ok(jQuery.browser.opera, "error");
1813                         start();
1814                 }
1815         });
1816 });
1817
1818 test("jQuery.ajax - Etag support", function() {
1819         expect( 3 );
1820
1821         stop();
1822
1823         var url = "data/etag.php?ts=" + new Date();
1824
1825         jQuery.ajax({
1826                 url: url,
1827                 ifModified: true,
1828                 success: function(data, status) {
1829                         equals(status, "success");
1830
1831                         jQuery.ajax({
1832                                 url: url,
1833                                 ifModified: true,
1834                                 success: function(data, status) {
1835                                         if ( data === "FAIL" ) {
1836                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1837                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1838                                         } else {
1839                                                 equals(status, "notmodified");
1840                                                 ok(data == null, "response body should be empty");
1841                                         }
1842                                         start();
1843                         },
1844                         error: function() {
1845                                         // Do this because opera simply refuses to implement 304 handling :(
1846                                         // A feature-driven way of detecting this would be appreciated
1847                                         // See: http://gist.github.com/599419
1848                                         ok(jQuery.browser.opera, "error");
1849                                         ok(jQuery.browser.opera, "error");
1850                                         start();
1851                                 }
1852                         });
1853                 },
1854                 error: function() {
1855                         // Do this because opera simply refuses to implement 304 handling :(
1856                         // A feature-driven way of detecting this would be appreciated
1857                         // See: http://gist.github.com/599419
1858                         ok(jQuery.browser.opera, "error");
1859                         start();
1860                 }
1861         });
1862 });
1863
1864 test("jQuery ajax - failing cross-domain", function() {
1865
1866         expect( 2 );
1867
1868         stop();
1869
1870         var i = 2;
1871
1872         jQuery.ajax({
1873                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1874                 success: function(){ ok( false , "success" ); },
1875                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1876                 complete: function() { if ( ! --i ) start(); }
1877         });
1878
1879         jQuery.ajax({
1880                 url: 'http://www.google.com',
1881                 success: function(){ ok( false , "success" ); },
1882                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1883                 complete: function() { if ( ! --i ) start(); }
1884         });
1885
1886 });
1887
1888 test("jQuery ajax - atom+xml", function() {
1889
1890         stop();
1891
1892         jQuery.ajax({
1893                 url: url( 'data/atom+xml.php' ),
1894                 success: function(){ ok( true , "success" ); },
1895                 error: function(){ ok( false , "error" ); },
1896                 complete: function() { start(); }
1897         });
1898
1899 });
1900
1901 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1902         var success = false;
1903         try {
1904                 var xhr = jQuery.ajax({ url: window.location });
1905                 success = true;
1906                 xhr.abort();
1907         } catch (e) {}
1908
1909         ok( success, "document.location did not generate exception" );
1910 });
1911
1912 test( "jQuery.ajax - statusCode" , function() {
1913
1914         var count = 12;
1915
1916         expect( 20 );
1917         stop();
1918
1919         function countComplete() {
1920                 if ( ! --count ) {
1921                         start();
1922                 }
1923         }
1924
1925         function createStatusCodes( name , isSuccess ) {
1926                 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1927                 return {
1928                         200: function() {
1929                                 ok( isSuccess , name );
1930                         },
1931                         404: function() {
1932                                 ok( ! isSuccess , name );
1933                         }
1934                 };
1935         }
1936
1937         jQuery.each( {
1938                 "data/name.html": true,
1939                 "data/someFileThatDoesNotExist.html": false
1940         } , function( uri , isSuccess ) {
1941
1942                 jQuery.ajax( url( uri ) , {
1943                         statusCode: createStatusCodes( "in options" , isSuccess ),
1944                         complete: countComplete
1945                 });
1946
1947                 jQuery.ajax( url( uri ) , {
1948                         complete: countComplete
1949                 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
1950
1951                 jQuery.ajax( url( uri ) , {
1952                         complete: function(jXHR) {
1953                                 jXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
1954                                 countComplete();
1955                         }
1956                 });
1957
1958                 jQuery.ajax( url( uri ) , {
1959                         complete: function(jXHR) {
1960                                 setTimeout( function() {
1961                                         jXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
1962                                         countComplete();
1963                                 } , 100 );
1964                         }
1965                 });
1966
1967                 jQuery.ajax( url( uri ) , {
1968                         statusCode: createStatusCodes( "all (options)" , isSuccess ),
1969                         complete: function(jXHR) {
1970                                 jXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
1971                                 setTimeout( function() {
1972                                         jXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
1973                                         countComplete();
1974                                 } , 100 );
1975                         }
1976                 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
1977
1978                 var testString = "";
1979
1980                 jQuery.ajax( url( uri ), {
1981                         success: function( a , b , jXHR ) {
1982                                 ok( isSuccess , "success" );
1983                                 var statusCode = {};
1984                                 statusCode[ jXHR.status ] = function() {
1985                                         testString += "B";
1986                                 };
1987                                 jXHR.statusCode( statusCode );
1988                                 testString += "A";
1989                         },
1990                         error: function( jXHR ) {
1991                                 ok( ! isSuccess , "error" );
1992                                 var statusCode = {};
1993                                 statusCode[ jXHR.status ] = function() {
1994                                         testString += "B";
1995                                 };
1996                                 jXHR.statusCode( statusCode );
1997                                 testString += "A";
1998                         },
1999                         complete: function() {
2000                                 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2001                                                 ( isSuccess ? "success" :  "error" ) + " callbacks" );
2002                                 countComplete();
2003                         }
2004                 } );
2005
2006         });
2007 });
2008
2009 test("jQuery.ajax - transitive conversions", function() {
2010
2011         expect( 8 );
2012
2013         stop();
2014
2015         jQuery.when(
2016
2017                 jQuery.ajax( url("data/json.php") , {
2018                         converters: {
2019                                 "json myjson": function( data ) {
2020                                         ok( true , "converter called" );
2021                                         return data;
2022                                 }
2023                         },
2024                         dataType: "myjson",
2025                         success: function() {
2026                                 ok( true , "Transitive conversion worked" );
2027                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2028                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2029                         }
2030                 }),
2031
2032                 jQuery.ajax( url("data/json.php") , {
2033                         converters: {
2034                                 "json myjson": function( data ) {
2035                                         ok( true , "converter called (*)" );
2036                                         return data;
2037                                 }
2038                         },
2039                         contents: false, /* headers are wrong so we ignore them */
2040                         dataType: "* myjson",
2041                         success: function() {
2042                                 ok( true , "Transitive conversion worked (*)" );
2043                                 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2044                                 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2045                         }
2046                 })
2047
2048         ).then( start , start );
2049
2050 });
2051
2052 test("jQuery.ajax - active counter", function() {
2053     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
2054 });
2055
2056 }
2057
2058 //}