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