Fixes 4825. jQuery.fn.load: use the jXHR's Promise interface to get the actual respon...
[jquery.git] / test / unit / ajax.js
1 module("ajax");
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() - contentType" , function() {
320
321         expect( 2 );
322
323         stop();
324
325         var count = 2;
326
327         function restart() {
328                 if ( ! --count ) {
329                         start();
330                 }
331         }
332
333         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
334                 contentType: "test",
335                 success: function( data ) {
336                         strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
337                 },
338                 complete: function() {
339                         restart();
340                 }
341         });
342
343         jQuery.ajax(url("data/headers.php?keys=content-type" ), {
344                 contentType: false,
345                 success: function( data ) {
346                         strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
347                 },
348                 complete: function() {
349                         restart();
350                 }
351         });
352
353 });
354
355 test(".ajax() - hash", function() {
356         expect(3);
357
358         jQuery.ajax({
359                 url: "data/name.html#foo",
360                 beforeSend: function( xhr, settings ) {
361                         equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
362                         return false;
363                 }
364         });
365
366         jQuery.ajax({
367                 url: "data/name.html?abc#foo",
368                 beforeSend: function( xhr, settings ) {
369                 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
370                         return false;
371                 }
372         });
373
374         jQuery.ajax({
375                 url: "data/name.html?abc#foo",
376                 data: { "test": 123 },
377                 beforeSend: function( xhr, settings ) {
378                         equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
379                         return false;
380                 }
381         });
382 });
383
384 test("jQuery ajax - cross-domain detection", function() {
385
386         expect( 4 );
387
388         var loc = document.location,
389                 otherPort = loc.port === 666 ? 667 : 666,
390                 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
391
392         jQuery.ajax({
393                 dataType: "jsonp",
394                 url: otherProtocol + "//" + loc.host,
395                 beforeSend: function( _ , s ) {
396                         ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
397                         return false;
398                 }
399         });
400
401         jQuery.ajax({
402                 dataType: "jsonp",
403                 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
404                 beforeSend: function( _ , s ) {
405                         ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
406                         return false;
407                 }
408         });
409
410         jQuery.ajax({
411                 dataType: "jsonp",
412                 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
413                 beforeSend: function( _ , s ) {
414                         ok( s.crossDomain , "Test different ports are detected as cross-domain" );
415                         return false;
416                 }
417         });
418
419         jQuery.ajax({
420                 dataType: "jsonp",
421                 url: loc.protocol + "//" + loc.host,
422                 crossDomain: true,
423                 beforeSend: function( _ , s ) {
424                         ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
425                         return false;
426                 }
427         });
428
429 });
430
431 test(".ajax() - 304", function() {
432         expect( 1 );
433         stop();
434
435         jQuery.ajax({
436                 url: url("data/notmodified.php"),
437                 success: function(){ ok(true, "304 ok"); },
438                 // Do this because opera simply refuses to implement 304 handling :(
439                 // A feature-driven way of detecting this would be appreciated
440                 // See: http://gist.github.com/599419
441                 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
442                 complete: function(xhr){ start(); }
443         });
444 });
445
446 test(".load()) - 404 error callbacks", function() {
447         expect( 6 );
448         stop();
449
450         jQuery('#foo').ajaxStart(function(){
451                 ok( true, "ajaxStart" );
452         }).ajaxStop(function(){
453                 ok( true, "ajaxStop" );
454                 start();
455         }).ajaxSend(function(){
456                 ok( true, "ajaxSend" );
457         }).ajaxComplete(function(){
458                 ok( true, "ajaxComplete" );
459         }).ajaxError(function(){
460                 ok( true, "ajaxError" );
461         }).ajaxSuccess(function(){
462                 ok( false, "ajaxSuccess" );
463         });
464
465         jQuery("<div/>").load("data/404.html", function(){
466                 ok(true, "complete");
467         });
468 });
469
470 test("jQuery.ajax() - abort", function() {
471         expect( 8 );
472         stop();
473
474         jQuery('#foo').ajaxStart(function(){
475                 ok( true, "ajaxStart" );
476         }).ajaxStop(function(){
477                 ok( true, "ajaxStop" );
478                 start();
479         }).ajaxSend(function(){
480                 ok( true, "ajaxSend" );
481         }).ajaxComplete(function(){
482                 ok( true, "ajaxComplete" );
483         });
484
485         var xhr = jQuery.ajax({
486                 url: url("data/name.php?wait=5"),
487                 beforeSend: function(){ ok(true, "beforeSend"); },
488                 complete: function(){ ok(true, "complete"); }
489         });
490
491         equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
492
493         xhr.abort();
494         equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
495 });
496
497 test("Ajax events with context", function() {
498         expect(14);
499
500         stop();
501         var context = document.createElement("div");
502
503         function event(e){
504                 equals( this, context, e.type );
505         }
506
507         function callback(msg){
508                 return function(){
509                         equals( this, context, "context is preserved on callback " + msg );
510                 };
511         }
512
513         function nocallback(msg){
514                 return function(){
515                         equals( typeof this.url, "string", "context is settings on callback " + msg );
516                 };
517         }
518
519         jQuery('#foo').add(context)
520                         .ajaxSend(event)
521                         .ajaxComplete(event)
522                         .ajaxError(event)
523                         .ajaxSuccess(event);
524
525         jQuery.ajax({
526                 url: url("data/name.html"),
527                 beforeSend: callback("beforeSend"),
528                 success: callback("success"),
529                 error: callback("error"),
530                 complete:function(){
531                         callback("complete").call(this);
532
533                         jQuery.ajax({
534                                 url: url("data/404.html"),
535                                 context: context,
536                                 beforeSend: callback("beforeSend"),
537                                 error: callback("error"),
538                                 complete: function(){
539                                         callback("complete").call(this);
540
541                                         jQuery('#foo').add(context).unbind();
542
543                                         jQuery.ajax({
544                                                 url: url("data/404.html"),
545                                                 beforeSend: nocallback("beforeSend"),
546                                                 error: nocallback("error"),
547                                                 complete: function(){
548                                                         nocallback("complete").call(this);
549                                                         start();
550                                                 }
551                                         });
552                                 }
553                         });
554                 },
555                 context:context
556         });
557 });
558
559 test("jQuery.ajax context modification", function() {
560         expect(1);
561
562         stop();
563
564         var obj = {};
565
566         jQuery.ajax({
567                 url: url("data/name.html"),
568                 context: obj,
569                 beforeSend: function(){
570                         this.test = "foo";
571                 },
572                 complete: function() {
573                         start();
574                 }
575         });
576
577         equals( obj.test, "foo", "Make sure the original object is maintained." );
578 });
579
580 test("jQuery.ajax() - disabled globals", function() {
581         expect( 3 );
582         stop();
583
584         jQuery('#foo').ajaxStart(function(){
585                 ok( false, "ajaxStart" );
586         }).ajaxStop(function(){
587                 ok( false, "ajaxStop" );
588         }).ajaxSend(function(){
589                 ok( false, "ajaxSend" );
590         }).ajaxComplete(function(){
591                 ok( false, "ajaxComplete" );
592         }).ajaxError(function(){
593                 ok( false, "ajaxError" );
594         }).ajaxSuccess(function(){
595                 ok( false, "ajaxSuccess" );
596         });
597
598         jQuery.ajax({
599                 global: false,
600                 url: url("data/name.html"),
601                 beforeSend: function(){ ok(true, "beforeSend"); },
602                 success: function(){ ok(true, "success"); },
603                 error: function(){ ok(false, "error"); },
604                 complete: function(){
605                   ok(true, "complete");
606                   setTimeout(function(){ start(); }, 13);
607                 }
608         });
609 });
610
611 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
612         expect(3);
613         stop();
614         jQuery.ajax({
615           url: url("data/with_fries.xml"),
616           dataType: "xml",
617           success: function(resp) {
618                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
619                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
620                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
621                 start();
622           }
623         });
624 });
625
626 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
627         expect(3);
628         stop();
629         jQuery.ajax({
630           url: url("data/with_fries_over_jsonp.php"),
631           dataType: "jsonp xml",
632           success: function(resp) {
633                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
634                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
635                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
636                 start();
637           },
638           error: function(_1,_2,error) {
639                 ok( false, error );
640                 start();
641           }
642         });
643 });
644
645 test("jQuery.ajax - HEAD requests", function() {
646         expect(2);
647
648         stop();
649         jQuery.ajax({
650                 url: url("data/name.html"),
651                 type: "HEAD",
652                 success: function(data, status, xhr){
653                         var h = xhr.getAllResponseHeaders();
654                         ok( /Date/i.test(h), 'No Date in HEAD response' );
655
656                         jQuery.ajax({
657                                 url: url("data/name.html"),
658                                 data: { whip_it: "good" },
659                                 type: "HEAD",
660                                 success: function(data, status, xhr){
661                                         var h = xhr.getAllResponseHeaders();
662                                         ok( /Date/i.test(h), 'No Date in HEAD response with data' );
663                                         start();
664                                 }
665                         });
666                 }
667         });
668
669 });
670
671 test("jQuery.ajax - beforeSend", function() {
672         expect(1);
673         stop();
674
675         var check = false;
676
677         jQuery.ajaxSetup({ timeout: 0 });
678
679         jQuery.ajax({
680                 url: url("data/name.html"),
681                 beforeSend: function(xml) {
682                         check = true;
683                 },
684                 success: function(data) {
685                         ok( check, "check beforeSend was executed" );
686                         start();
687                 }
688         });
689 });
690
691 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
692         expect(2);
693         var request = jQuery.ajax({
694                 url: url("data/name.html"),
695                 beforeSend: function() {
696                         ok( true, "beforeSend got called, canceling" );
697                         return false;
698                 },
699                 success: function() {
700                         ok( false, "request didn't get canceled" );
701                 },
702                 complete: function() {
703                         ok( false, "request didn't get canceled" );
704                 },
705                 error: function() {
706                         ok( false, "request didn't get canceled" );
707                 }
708         });
709         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
710 });
711
712 test("jQuery.ajax - beforeSend, cancel request manually", function() {
713         expect(2);
714         var request = jQuery.ajax({
715                 url: url("data/name.html"),
716                 beforeSend: function(xhr) {
717                         ok( true, "beforeSend got called, canceling" );
718                         xhr.abort();
719                 },
720                 success: function() {
721                         ok( false, "request didn't get canceled" );
722                 },
723                 complete: function() {
724                         ok( false, "request didn't get canceled" );
725                 },
726                 error: function() {
727                         ok( false, "request didn't get canceled" );
728                 }
729         });
730         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
731 });
732
733 window.foobar = null;
734 window.testFoo = undefined;
735
736 test("jQuery.ajax - dataType html", function() {
737         expect(5);
738         stop();
739
740         var verifyEvaluation = function() {
741                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
742                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
743
744                 start();
745         };
746
747         jQuery.ajax({
748           dataType: "html",
749           url: url("data/test.html"),
750           success: function(data) {
751                 jQuery("#ap").html(data);
752                 ok( data.match(/^html text/), 'Check content for datatype html' );
753                 setTimeout(verifyEvaluation, 600);
754           }
755         });
756 });
757
758 test("serialize()", function() {
759         expect(5);
760
761         // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
762         jQuery("#search").after(
763                 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
764                 '<input type="number" id="html5number" name="number" value="43" />'
765         );
766
767         equals( jQuery('#form').serialize(),
768                 "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",
769                 'Check form serialization as query string');
770
771         equals( jQuery('#form :input').serialize(),
772                 "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",
773                 'Check input serialization as query string');
774
775         equals( jQuery('#testForm').serialize(),
776                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
777                 'Check form serialization as query string');
778
779         equals( jQuery('#testForm :input').serialize(),
780                 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
781                 'Check input serialization as query string');
782
783         equals( jQuery('#form, #testForm').serialize(),
784                 "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=",
785                 'Multiple form serialization as query string');
786
787   /* Temporarily disabled. Opera 10 has problems with form serialization.
788         equals( jQuery('#form, #testForm :input').serialize(),
789                 "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=",
790                 'Mixed form/input serialization as query string');
791         */
792         jQuery("#html5email, #html5number").remove();
793 });
794
795 test("jQuery.param()", function() {
796         expect(22);
797
798         equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
799
800         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
801         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
802
803         params = {someName: [1, 2, 3], regularThing: "blah" };
804         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
805
806         params = {foo: ['a', 'b', 'c']};
807         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
808
809         params = {foo: ["baz", 42, "All your base are belong to us"] };
810         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
811
812         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
813         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" );
814
815         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?" };
816         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" );
817
818         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 ] };
819         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" );
820
821         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?" };
822         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" );
823
824         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." );
825
826         // Make sure empty arrays and objects are handled #6481
827         equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
828         equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
829         equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
830
831         jQuery.ajaxSetup({ traditional: true });
832
833         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
834         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
835
836         params = {someName: [1, 2, 3], regularThing: "blah" };
837         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
838
839         params = {foo: ['a', 'b', 'c']};
840         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
841
842         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
843         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
844
845         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
846         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" );
847
848         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?" };
849         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" );
850
851         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 ] };
852         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)" );
853
854         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?" };
855         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" );
856
857         params = { param1: null };
858         equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
859 });
860
861 test("synchronous request", function() {
862         expect(1);
863         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
864 });
865
866 test("synchronous request with callbacks", function() {
867         expect(2);
868         var result;
869         jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
870         ok( /^{ "data"/.test( result ), "check returned text" );
871 });
872
873 test("pass-through request object", function() {
874         expect(8);
875         stop();
876
877         var target = "data/name.html";
878         var successCount = 0;
879         var errorCount = 0;
880         var errorEx = "";
881         var success = function() {
882                 successCount++;
883         };
884         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
885                 errorCount++;
886                 errorEx += ": " + xml.status;
887         });
888         jQuery("#foo").one('ajaxStop', function () {
889                 equals(successCount, 5, "Check all ajax calls successful");
890                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
891                 jQuery("#foo").unbind('ajaxError');
892
893                 start();
894         });
895
896         ok( jQuery.get(url(target), success), "get" );
897         ok( jQuery.post(url(target), success), "post" );
898         ok( jQuery.getScript(url("data/test.js"), success), "script" );
899         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
900         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
901 });
902
903 test("ajax cache", function () {
904         expect(18);
905
906         stop();
907
908         var count = 0;
909
910         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
911                 var re = /_=(.*?)(&|$)/g;
912                 var oldOne = null;
913                 for (var i = 0; i < 6; i++) {
914                         var ret = re.exec(s.url);
915                         if (!ret) {
916                                 break;
917                         }
918                         oldOne = ret[1];
919                 }
920                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
921                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
922                 if(++count == 6)
923                         start();
924         });
925
926         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
927         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
928         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
929         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
930         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
931         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
932 });
933
934 /*
935  * Test disabled.
936  * The assertions expect that the passed-in object will be modified,
937  * which shouldn't be the case. Fixes #5439.
938 test("global ajaxSettings", function() {
939         expect(2);
940
941         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
942         var orig = { url: "data/with_fries.xml" };
943         var t;
944
945         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
946
947         t = jQuery.extend({}, orig);
948         t.data = {};
949         jQuery.ajax(t);
950         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
951
952         t = jQuery.extend({}, orig);
953         t.data = { zoo: 'a', ping: 'b' };
954         jQuery.ajax(t);
955         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' }" );
956
957         jQuery.ajaxSettings = tmp;
958 });
959 */
960
961 test("load(String)", function() {
962         expect(1);
963         stop(); // check if load can be called with only url
964         jQuery('#first').load("data/name.html", start);
965 });
966
967 test("load('url selector')", function() {
968         expect(1);
969         stop(); // check if load can be called with only url
970         jQuery('#first').load("data/test3.html div.user", function(){
971                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
972                 start();
973         });
974 });
975
976 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
977         expect(1);
978         stop();
979         jQuery.ajaxSetup({ dataType: "json" });
980         jQuery("#first").ajaxComplete(function (e, xml, s) {
981                 equals( s.dataType, "html", "Verify the load() dataType was html" );
982                 jQuery("#first").unbind("ajaxComplete");
983                 jQuery.ajaxSetup({ dataType: "" });
984                 start();
985         });
986         jQuery('#first').load("data/test3.html");
987 });
988
989 test("load(String, Function) - simple: inject text into DOM", function() {
990         expect(2);
991         stop();
992         jQuery('#first').load(url("data/name.html"), function() {
993                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
994                 start();
995         });
996 });
997
998 test("load(String, Function) - check scripts", function() {
999         expect(7);
1000         stop();
1001
1002         var verifyEvaluation = function() {
1003                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1004                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1005
1006                 start();
1007         };
1008         jQuery('#first').load(url('data/test.html'), function() {
1009                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1010                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1011                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1012                 setTimeout(verifyEvaluation, 600);
1013         });
1014 });
1015
1016 test("load(String, Function) - check file with only a script tag", function() {
1017         expect(3);
1018         stop();
1019
1020         jQuery('#first').load(url('data/test2.html'), function() {
1021                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1022                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1023
1024                 start();
1025         });
1026 });
1027
1028 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1029         expect(2);
1030         stop();
1031         jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1032         var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1033                 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1034                 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1035                 jQuery.ajaxSetup({ dataFilter: 0 });
1036                 start();
1037         });
1038 });
1039
1040 test("load(String, Object, Function)", function() {
1041         expect(2);
1042         stop();
1043
1044         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1045                 var $post = jQuery(this).find('#post');
1046                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1047                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1048                 start();
1049         });
1050 });
1051
1052 test("load(String, String, Function)", function() {
1053         expect(2);
1054         stop();
1055
1056         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1057                 var $get = jQuery(this).find('#get');
1058                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1059                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
1060                 start();
1061         });
1062 });
1063
1064 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1065         expect(2);
1066         stop();
1067         jQuery.get(url('data/dashboard.xml'), function(xml) {
1068                 var content = [];
1069                 jQuery('tab', xml).each(function() {
1070                         content.push(jQuery(this).text());
1071                 });
1072                 equals( content[0], 'blabla', 'Check first tab');
1073                 equals( content[1], 'blublu', 'Check second tab');
1074                 start();
1075         });
1076 });
1077
1078 test("jQuery.getScript(String, Function) - with callback", function() {
1079         expect(2);
1080         stop();
1081         jQuery.getScript(url("data/test.js"), function() {
1082                 equals( foobar, "bar", 'Check if script was evaluated' );
1083                 setTimeout(start, 100);
1084         });
1085 });
1086
1087 test("jQuery.getScript(String, Function) - no callback", function() {
1088         expect(1);
1089         stop();
1090         jQuery.getScript(url("data/test.js"), function(){
1091                 start();
1092         });
1093 });
1094
1095 test("jQuery.ajax() - JSONP, Local", function() {
1096         expect(10);
1097
1098         var count = 0;
1099         function plus(){ if ( ++count == 10 ) start(); }
1100
1101         stop();
1102
1103         jQuery.ajax({
1104                 url: "data/jsonp.php",
1105                 dataType: "jsonp",
1106                 success: function(data){
1107                         ok( data.data, "JSON results returned (GET, no callback)" );
1108                         plus();
1109                 },
1110                 error: function(data){
1111                         ok( false, "Ajax error JSON (GET, no callback)" );
1112                         plus();
1113                 }
1114         });
1115
1116         jQuery.ajax({
1117                 url: "data/jsonp.php?callback=?",
1118                 dataType: "jsonp",
1119                 success: function(data){
1120                         ok( data.data, "JSON results returned (GET, url callback)" );
1121                         plus();
1122                 },
1123                 error: function(data){
1124                         ok( false, "Ajax error JSON (GET, url callback)" );
1125                         plus();
1126                 }
1127         });
1128
1129         jQuery.ajax({
1130                 url: "data/jsonp.php",
1131                 dataType: "jsonp",
1132                 data: "callback=?",
1133                 success: function(data){
1134                         ok( data.data, "JSON results returned (GET, data callback)" );
1135                         plus();
1136                 },
1137                 error: function(data){
1138                         ok( false, "Ajax error JSON (GET, data callback)" );
1139                         plus();
1140                 }
1141         });
1142
1143         jQuery.ajax({
1144                 url: "data/jsonp.php",
1145                 dataType: "jsonp",
1146                 data: {
1147                         callback: "?"
1148                 },
1149                 success: function(data){
1150                         ok( data.data, "JSON results returned (GET, processed data callback)" );
1151                         plus();
1152                 },
1153                 error: function(data){
1154                         ok( false, "Ajax error JSON (GET, processed data callback)" );
1155                         plus();
1156                 }
1157         });
1158
1159         jQuery.ajax({
1160                 url: "data/jsonp.php",
1161                 dataType: "jsonp",
1162                 jsonp: "callback",
1163                 success: function(data){
1164                         ok( data.data, "JSON results returned (GET, data obj callback)" );
1165                         plus();
1166                 },
1167                 error: function(data){
1168                         ok( false, "Ajax error JSON (GET, data obj callback)" );
1169                         plus();
1170                 }
1171         });
1172
1173         jQuery.ajax({
1174                 url: "data/jsonp.php",
1175                 dataType: "jsonp",
1176                 jsonpCallback: "jsonpResults",
1177                 success: function(data){
1178                         ok( data.data, "JSON results returned (GET, custom callback name)" );
1179                         plus();
1180                 },
1181                 error: function(data){
1182                         ok( false, "Ajax error JSON (GET, custom callback name)" );
1183                         plus();
1184                 }
1185         });
1186
1187         jQuery.ajax({
1188                 type: "POST",
1189                 url: "data/jsonp.php",
1190                 dataType: "jsonp",
1191                 success: function(data){
1192                         ok( data.data, "JSON results returned (POST, no callback)" );
1193                         plus();
1194                 },
1195                 error: function(data){
1196                         ok( false, "Ajax error JSON (GET, data obj callback)" );
1197                         plus();
1198                 }
1199         });
1200
1201         jQuery.ajax({
1202                 type: "POST",
1203                 url: "data/jsonp.php",
1204                 data: "callback=?",
1205                 dataType: "jsonp",
1206                 success: function(data){
1207                         ok( data.data, "JSON results returned (POST, data callback)" );
1208                         plus();
1209                 },
1210                 error: function(data){
1211                         ok( false, "Ajax error JSON (POST, data callback)" );
1212                         plus();
1213                 }
1214         });
1215
1216         jQuery.ajax({
1217                 type: "POST",
1218                 url: "data/jsonp.php",
1219                 jsonp: "callback",
1220                 dataType: "jsonp",
1221                 success: function(data){
1222                         ok( data.data, "JSON results returned (POST, data obj callback)" );
1223                         plus();
1224                 },
1225                 error: function(data){
1226                         ok( false, "Ajax error JSON (POST, data obj callback)" );
1227                         plus();
1228                 }
1229         });
1230
1231         //#7578
1232         jQuery.ajax({
1233                 url: "data/jsonp.php",
1234                 dataType: "jsonp",
1235                 beforeSend: function(){
1236                         strictEqual( this.cache, false, "cache must be false on JSON request" );
1237                         plus();
1238                         return false;
1239                 }
1240         });
1241 });
1242
1243 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1244         expect(1);
1245         stop();
1246
1247         window.jsonpResults = function(data) {
1248                 ok( data.data, "JSON results returned (GET, custom callback function)" );
1249                 window.jsonpResults = undefined;
1250                 start();
1251         };
1252
1253         jQuery.ajax({
1254                 url: "data/jsonp.php",
1255                 dataType: "jsonp",
1256                 jsonpCallback: "jsonpResults"
1257         });
1258 });
1259
1260 test("jQuery.ajax() - JSONP, Remote", function() {
1261         expect(4);
1262
1263         var count = 0;
1264         function plus(){ if ( ++count == 4 ) start(); }
1265
1266         var base = window.location.href.replace(/[^\/]*$/, "");
1267
1268         stop();
1269
1270         jQuery.ajax({
1271                 url: base + "data/jsonp.php",
1272                 dataType: "jsonp",
1273                 success: function(data){
1274                         ok( data.data, "JSON results returned (GET, no callback)" );
1275                         plus();
1276                 },
1277                 error: function(data){
1278                         ok( false, "Ajax error JSON (GET, no callback)" );
1279                         plus();
1280                 }
1281         });
1282
1283         jQuery.ajax({
1284                 url: base + "data/jsonp.php?callback=?",
1285                 dataType: "jsonp",
1286                 success: function(data){
1287                         ok( data.data, "JSON results returned (GET, url callback)" );
1288                         plus();
1289                 },
1290                 error: function(data){
1291                         ok( false, "Ajax error JSON (GET, url callback)" );
1292                         plus();
1293                 }
1294         });
1295
1296         jQuery.ajax({
1297                 url: base + "data/jsonp.php",
1298                 dataType: "jsonp",
1299                 data: "callback=?",
1300                 success: function(data){
1301                         ok( data.data, "JSON results returned (GET, data callback)" );
1302                         plus();
1303                 },
1304                 error: function(data){
1305                         ok( false, "Ajax error JSON (GET, data callback)" );
1306                         plus();
1307                 }
1308         });
1309
1310         jQuery.ajax({
1311                 url: base + "data/jsonp.php",
1312                 dataType: "jsonp",
1313                 jsonp: "callback",
1314                 success: function(data){
1315                         ok( data.data, "JSON results returned (GET, data obj callback)" );
1316                         plus();
1317                 },
1318                 error: function(data){
1319                         ok( false, "Ajax error JSON (GET, data obj callback)" );
1320                         plus();
1321                 }
1322         });
1323 });
1324
1325 test("jQuery.ajax() - script, Remote", function() {
1326         expect(2);
1327
1328         var base = window.location.href.replace(/[^\/]*$/, "");
1329
1330         stop();
1331
1332         jQuery.ajax({
1333                 url: base + "data/test.js",
1334                 dataType: "script",
1335                 success: function(data){
1336                         ok( foobar, "Script results returned (GET, no callback)" );
1337                         start();
1338                 }
1339         });
1340 });
1341
1342 test("jQuery.ajax() - script, Remote with POST", function() {
1343         expect(3);
1344
1345         var base = window.location.href.replace(/[^\/]*$/, "");
1346
1347         stop();
1348
1349         jQuery.ajax({
1350                 url: base + "data/test.js",
1351                 type: "POST",
1352                 dataType: "script",
1353                 success: function(data, status){
1354                         ok( foobar, "Script results returned (POST, no callback)" );
1355                         equals( status, "success", "Script results returned (POST, no callback)" );
1356                         start();
1357                 },
1358                 error: function(xhr) {
1359                         ok( false, "ajax error, status code: " + xhr.status );
1360                         start();
1361                 }
1362         });
1363 });
1364
1365 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1366         expect(2);
1367
1368         var base = window.location.href.replace(/[^\/]*$/, "");
1369         base = base.replace(/^.*?\/\//, "//");
1370
1371         stop();
1372
1373         jQuery.ajax({
1374                 url: base + "data/test.js",
1375                 dataType: "script",
1376                 success: function(data){
1377                         ok( foobar, "Script results returned (GET, no callback)" );
1378                         start();
1379                 }
1380         });
1381 });
1382
1383 test("jQuery.ajax() - malformed JSON", function() {
1384         expect(2);
1385
1386         stop();
1387
1388         jQuery.ajax({
1389                 url: "data/badjson.js",
1390                 dataType: "json",
1391                 success: function(){
1392                         ok( false, "Success." );
1393                         start();
1394                 },
1395                 error: function(xhr, msg, detailedMsg) {
1396                         equals( "parsererror", msg, "A parse error occurred." );
1397                         ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1398                         start();
1399                 }
1400         });
1401 });
1402
1403 test("jQuery.ajax() - script by content-type", function() {
1404         expect(1);
1405
1406         stop();
1407
1408         jQuery.ajax({
1409                 url: "data/script.php",
1410                 data: { header: "script" },
1411                 success: function() {
1412                         start();
1413                 }
1414         });
1415 });
1416
1417 test("jQuery.ajax() - json by content-type", function() {
1418         expect(5);
1419
1420         stop();
1421
1422         jQuery.ajax({
1423                 url: "data/json.php",
1424                 data: { header: "json", json: "array" },
1425                 success: function( json ) {
1426                         ok( json.length >= 2, "Check length");
1427                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1428                         equals( json[0].age, 21, 'Check JSON: first, age' );
1429                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1430                         equals( json[1].age, 25, 'Check JSON: second, age' );
1431                         start();
1432                 }
1433         });
1434 });
1435
1436 test("jQuery.ajax() - json by content-type disabled with options", function() {
1437         expect(6);
1438
1439         stop();
1440
1441         jQuery.ajax({
1442                 url: url("data/json.php"),
1443                 data: { header: "json", json: "array" },
1444                 contents: {
1445                         json: false
1446                 },
1447                 success: function( text ) {
1448                         equals( typeof text , "string" , "json wasn't auto-determined" );
1449                         var json = jQuery.parseJSON( text );
1450                         ok( json.length >= 2, "Check length");
1451                         equals( json[0].name, 'John', 'Check JSON: first, name' );
1452                         equals( json[0].age, 21, 'Check JSON: first, age' );
1453                         equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1454                         equals( json[1].age, 25, 'Check JSON: second, age' );
1455                         start();
1456                 }
1457         });
1458 });
1459
1460 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1461         expect(5);
1462         stop();
1463         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1464           ok( json.length >= 2, "Check length");
1465           equals( json[0].name, 'John', 'Check JSON: first, name' );
1466           equals( json[0].age, 21, 'Check JSON: first, age' );
1467           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1468           equals( json[1].age, 25, 'Check JSON: second, age' );
1469           start();
1470         });
1471 });
1472
1473 test("jQuery.getJSON(String, Function) - JSON object", function() {
1474         expect(2);
1475         stop();
1476         jQuery.getJSON(url("data/json.php"), function(json) {
1477           if (json && json.data) {
1478                   equals( json.data.lang, 'en', 'Check JSON: lang' );
1479                   equals( json.data.length, 25, 'Check JSON: length' );
1480           }
1481           start();
1482         });
1483 });
1484
1485 test("jQuery.getJSON - Using Native JSON", function() {
1486         expect(2);
1487
1488         var old = window.JSON;
1489         JSON = {
1490                 parse: function(str){
1491                         ok( true, "Verifying that parse method was run" );
1492                         return true;
1493                 }
1494         };
1495
1496         stop();
1497         jQuery.getJSON(url("data/json.php"), function(json) {
1498                 window.JSON = old;
1499                 equals( json, true, "Verifying return value" );
1500                 start();
1501         });
1502 });
1503
1504 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1505         expect(2);
1506
1507         var base = window.location.href.replace(/[^\/]*$/, "");
1508
1509         stop();
1510         jQuery.getJSON(url(base + "data/json.php"), function(json) {
1511           equals( json.data.lang, 'en', 'Check JSON: lang' );
1512           equals( json.data.length, 25, 'Check JSON: length' );
1513           start();
1514         });
1515 });
1516
1517 test("jQuery.post - data", function() {
1518         expect(2);
1519         stop();
1520
1521         jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1522                 jQuery('math', xml).each(function() {
1523                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1524                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1525                 });
1526                 start();
1527         });
1528 });
1529
1530 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1531         expect(4);
1532         stop();
1533         var done = 0;
1534
1535         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1536           jQuery('math', xml).each(function() {
1537                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1538                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1539                  });
1540           if ( ++done === 2 ) start();
1541         });
1542
1543         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1544           jQuery('math', xml).each(function() {
1545                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1546                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
1547                  });
1548           if ( ++done === 2 ) start();
1549         });
1550 });
1551
1552 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1553         stop();
1554
1555         var passed = 0;
1556
1557         jQuery.ajaxSetup({timeout: 1000});
1558
1559         var pass = function() {
1560                 passed++;
1561                 if ( passed == 2 ) {
1562                         ok( true, 'Check local and global callbacks after timeout' );
1563                         jQuery('#main').unbind("ajaxError");
1564                         start();
1565                 }
1566         };
1567
1568         var fail = function(a,b,c) {
1569                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1570                 start();
1571         };
1572
1573         jQuery('#main').ajaxError(pass);
1574
1575         jQuery.ajax({
1576           type: "GET",
1577           url: url("data/name.php?wait=5"),
1578           error: pass,
1579           success: fail
1580         });
1581
1582         // reset timeout
1583         jQuery.ajaxSetup({timeout: 0});
1584 });
1585
1586 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1587         stop();
1588         jQuery.ajaxSetup({timeout: 50});
1589
1590         jQuery.ajax({
1591           type: "GET",
1592           timeout: 15000,
1593           url: url("data/name.php?wait=1"),
1594           error: function() {
1595                    ok( false, 'Check for local timeout failed' );
1596                    start();
1597           },
1598           success: function() {
1599                 ok( true, 'Check for local timeout' );
1600                 start();
1601           }
1602         });
1603
1604         // reset timeout
1605         jQuery.ajaxSetup({timeout: 0});
1606 });
1607
1608 test("jQuery.ajax - simple get", function() {
1609         expect(1);
1610         stop();
1611         jQuery.ajax({
1612           type: "GET",
1613           url: url("data/name.php?name=foo"),
1614           success: function(msg){
1615                 equals( msg, 'bar', 'Check for GET' );
1616                 start();
1617           }
1618         });
1619 });
1620
1621 test("jQuery.ajax - simple post", function() {
1622         expect(1);
1623         stop();
1624         jQuery.ajax({
1625           type: "POST",
1626           url: url("data/name.php"),
1627           data: "name=peter",
1628           success: function(msg){
1629                 equals( msg, 'pan', 'Check for POST' );
1630                 start();
1631           }
1632         });
1633 });
1634
1635 test("ajaxSetup()", function() {
1636         expect(1);
1637         stop();
1638         jQuery.ajaxSetup({
1639                 url: url("data/name.php?name=foo"),
1640                 success: function(msg){
1641                         equals( msg, 'bar', 'Check for GET' );
1642                         start();
1643                 }
1644         });
1645         jQuery.ajax();
1646 });
1647
1648 /*
1649 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1650         stop();
1651         jQuery.ajax({
1652                 url: "data/name.php?wait=1",
1653                 timeout: 500,
1654                 error: function(request, status) {
1655                         ok( status != null, "status shouldn't be null in error handler" );
1656                         equals( "timeout", status );
1657                         start();
1658                 }
1659         });
1660 });
1661 */
1662
1663 test("data option: evaluate function values (#2806)", function() {
1664         stop();
1665         jQuery.ajax({
1666                 url: "data/echoQuery.php",
1667                 data: {
1668                         key: function() {
1669                                 return "value";
1670                         }
1671                 },
1672                 success: function(result) {
1673                         equals( result, "key=value" );
1674                         start();
1675                 }
1676         });
1677 });
1678
1679 test("data option: empty bodies for non-GET requests", function() {
1680         stop();
1681         jQuery.ajax({
1682                 url: "data/echoData.php",
1683                 data: undefined,
1684                 type: "post",
1685                 success: function(result) {
1686                         equals( result, "" );
1687                         start();
1688                 }
1689         });
1690 });
1691
1692 test("jQuery.ajax - If-Modified-Since support", function() {
1693         expect( 3 );
1694
1695         stop();
1696
1697         var url = "data/if_modified_since.php?ts=" + new Date();
1698
1699         jQuery.ajax({
1700                 url: url,
1701                 ifModified: true,
1702                 success: function(data, status) {
1703                         equals(status, "success");
1704
1705                         jQuery.ajax({
1706                                 url: url,
1707                                 ifModified: true,
1708                                 success: function(data, status) {
1709                                         if ( data === "FAIL" ) {
1710                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1711                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1712                                         } else {
1713                                                 equals(status, "notmodified");
1714                                                 ok(data == null, "response body should be empty");
1715                                         }
1716                                         start();
1717                         },
1718                                 error: function() {
1719                                         // Do this because opera simply refuses to implement 304 handling :(
1720                                         // A feature-driven way of detecting this would be appreciated
1721                                         // See: http://gist.github.com/599419
1722                                         ok(jQuery.browser.opera, "error");
1723                                         ok(jQuery.browser.opera, "error");
1724                                         start();
1725                         }
1726                         });
1727                 },
1728                 error: function() {
1729                         equals(false, "error");
1730                         // Do this because opera simply refuses to implement 304 handling :(
1731                         // A feature-driven way of detecting this would be appreciated
1732                         // See: http://gist.github.com/599419
1733                         ok(jQuery.browser.opera, "error");
1734                         start();
1735                 }
1736         });
1737 });
1738
1739 test("jQuery.ajax - Etag support", function() {
1740         expect( 3 );
1741
1742         stop();
1743
1744         var url = "data/etag.php?ts=" + new Date();
1745
1746         jQuery.ajax({
1747                 url: url,
1748                 ifModified: true,
1749                 success: function(data, status) {
1750                         equals(status, "success");
1751
1752                         jQuery.ajax({
1753                                 url: url,
1754                                 ifModified: true,
1755                                 success: function(data, status) {
1756                                         if ( data === "FAIL" ) {
1757                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1758                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1759                                         } else {
1760                                                 equals(status, "notmodified");
1761                                                 ok(data == null, "response body should be empty");
1762                                         }
1763                                         start();
1764                         },
1765                         error: function() {
1766                                         // Do this because opera simply refuses to implement 304 handling :(
1767                                         // A feature-driven way of detecting this would be appreciated
1768                                         // See: http://gist.github.com/599419
1769                                         ok(jQuery.browser.opera, "error");
1770                                         ok(jQuery.browser.opera, "error");
1771                                         start();
1772                                 }
1773                         });
1774                 },
1775                 error: function() {
1776                         // Do this because opera simply refuses to implement 304 handling :(
1777                         // A feature-driven way of detecting this would be appreciated
1778                         // See: http://gist.github.com/599419
1779                         ok(jQuery.browser.opera, "error");
1780                         start();
1781                 }
1782         });
1783 });
1784
1785 test("jQuery ajax - failing cross-domain", function() {
1786
1787         expect( 2 );
1788
1789         stop();
1790
1791         var i = 2;
1792
1793         if ( jQuery.ajax({
1794                 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1795                 success: function(){ ok( false , "success" ); },
1796                 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1797                 complete: function() { if ( ! --i ) start(); }
1798         }) === false ) {
1799                 ok( true , "no transport" );
1800                 if ( ! --i ) start();
1801         }
1802
1803         if ( jQuery.ajax({
1804                 url: 'http://www.google.com',
1805                 success: function(){ ok( false , "success" ); },
1806                 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1807                 complete: function() { if ( ! --i ) start(); }
1808         }) === false ) {
1809                 ok( true , "no transport" );
1810                 if ( ! --i ) start();
1811         }
1812
1813 });
1814
1815 test("jQuery ajax - atom+xml", function() {
1816
1817         stop();
1818
1819         jQuery.ajax({
1820                 url: url( 'data/atom+xml.php' ),
1821                 success: function(){ ok( true , "success" ); },
1822                 error: function(){ ok( false , "error" ); },
1823                 complete: function() { start(); }
1824         });
1825
1826 });
1827
1828 test("jQuery.ajax - active counter", function() {
1829     ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1830 });
1831
1832 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1833         var success = false;
1834         try {
1835                 var xhr = jQuery.ajax({ url: window.location });
1836                 success = true;
1837                 xhr.abort();
1838         } catch (e) {}
1839
1840         ok( success, "document.location did not generate exception" );
1841 });
1842
1843 }
1844
1845 //}