Getting $.param working well; Patch by ben_alman
[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         setTimeout(function(){
18                 jQuery('#foo').ajaxStart(function(){
19                         ok( true, "ajaxStart" );
20                 }).ajaxStop(function(){
21                         ok( true, "ajaxStop" );
22                         start();
23                 }).ajaxSend(function(){
24                         ok( true, "ajaxSend" );
25                 }).ajaxComplete(function(){
26                         ok( true, "ajaxComplete" );
27                 }).ajaxError(function(){
28                         ok( false, "ajaxError" );
29                 }).ajaxSuccess(function(){
30                         ok( true, "ajaxSuccess" );
31                 });
32
33                 jQuery.ajax({
34                         url: url("data/name.html"),
35                         beforeSend: function(){ ok(true, "beforeSend"); },
36                         success: function(){ ok(true, "success"); },
37                         error: function(){ ok(false, "error"); },
38                         complete: function(){ ok(true, "complete"); }
39                 });
40         }, 13);
41 });
42
43 test("jQuery.ajax() - error callbacks", function() {
44         expect( 8 );
45         stop();
46
47         jQuery('#foo').ajaxStart(function(){
48                 ok( true, "ajaxStart" );
49         }).ajaxStop(function(){
50                 ok( true, "ajaxStop" );
51                 start();
52         }).ajaxSend(function(){
53                 ok( true, "ajaxSend" );
54         }).ajaxComplete(function(){
55                 ok( true, "ajaxComplete" );
56         }).ajaxError(function(){
57                 ok( true, "ajaxError" );
58         }).ajaxSuccess(function(){
59                 ok( false, "ajaxSuccess" );
60         });
61
62         jQuery.ajaxSetup({ timeout: 500 });
63
64         jQuery.ajax({
65                 url: url("data/name.php?wait=5"),
66                 beforeSend: function(){ ok(true, "beforeSend"); },
67                 success: function(){ ok(false, "success"); },
68                 error: function(){ ok(true, "error"); },
69                 complete: function(){ ok(true, "complete"); }
70         });
71 });
72
73 test("Ajax events with context", function() {
74         expect(6);
75         
76         stop();
77         var context = {};
78         
79         function event(e){
80                 equals( this, context, e.type );
81         }
82
83         function callback(){
84                 equals( this, context, "context is preserved on callback" );
85         }
86         
87         jQuery('#foo').add(context)
88                         .ajaxSend(event)
89                         .ajaxComplete(event)
90                         .ajaxError(event)
91                         .ajaxSuccess(event);
92
93         jQuery.ajax({
94                 url: url("data/name.html"),
95                 beforeSend: callback,
96                 success: callback,
97                 error: callback,
98                 complete:function(){
99                         callback.call(this);
100                         setTimeout(proceed, 300);
101                 },
102                 context:context
103         });
104         
105         function proceed(){
106                 jQuery('#foo').add(context).unbind();
107                 start();
108         }
109 });
110
111 test("jQuery.ajax() - disabled globals", function() {
112         expect( 3 );
113         stop();
114
115         jQuery('#foo').ajaxStart(function(){
116                 ok( false, "ajaxStart" );
117         }).ajaxStop(function(){
118                 ok( false, "ajaxStop" );
119         }).ajaxSend(function(){
120                 ok( false, "ajaxSend" );
121         }).ajaxComplete(function(){
122                 ok( false, "ajaxComplete" );
123         }).ajaxError(function(){
124                 ok( false, "ajaxError" );
125         }).ajaxSuccess(function(){
126                 ok( false, "ajaxSuccess" );
127         });
128
129         jQuery.ajax({
130                 global: false,
131                 url: url("data/name.html"),
132                 beforeSend: function(){ ok(true, "beforeSend"); },
133                 success: function(){ ok(true, "success"); },
134                 error: function(){ ok(false, "error"); },
135                 complete: function(){
136                   ok(true, "complete");
137                   setTimeout(function(){ start(); }, 13);
138                 }
139         });
140 });
141
142 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
143         expect(3);
144         stop();
145         jQuery.ajax({
146           url: url("data/with_fries.xml"),
147           dataType: "xml",
148           success: function(resp) {
149                 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
150                 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
151                 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
152                 start();
153           }
154         });
155 });
156
157 test("jQuery.ajax - beforeSend", function() {
158         expect(1);
159         stop();
160
161         var check = false;
162
163         jQuery.ajaxSetup({ timeout: 0 });
164
165         jQuery.ajax({
166                 url: url("data/name.html"),
167                 beforeSend: function(xml) {
168                         check = true;
169                 },
170                 success: function(data) {
171                         ok( check, "check beforeSend was executed" );
172                         start();
173                 }
174         });
175 });
176
177 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
178         expect(2);
179         var request = jQuery.ajax({
180                 url: url("data/name.html"),
181                 beforeSend: function() {
182                         ok( true, "beforeSend got called, canceling" );
183                         return false;
184                 },
185                 success: function() {
186                         ok( false, "request didn't get canceled" );
187                 },
188                 complete: function() {
189                         ok( false, "request didn't get canceled" );
190                 },
191                 error: function() {
192                         ok( false, "request didn't get canceled" );
193                 }
194         });
195         ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
196 });
197
198 window.foobar = null;
199 window.testFoo = undefined;
200
201 test("jQuery.ajax - dataType html", function() {
202         expect(5);
203         stop();
204
205         var verifyEvaluation = function() {
206                 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
207                 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
208
209                 start();
210         };
211
212         jQuery.ajax({
213           dataType: "html",
214           url: url("data/test.html"),
215           success: function(data) {
216                 jQuery("#ap").html(data);
217                 ok( data.match(/^html text/), 'Check content for datatype html' );
218                 setTimeout(verifyEvaluation, 600);
219           }
220         });
221 });
222
223 test("serialize()", function() {
224         expect(5);
225
226         equals( jQuery('#form').serialize(),
227                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2",
228                 'Check form serialization as query string');
229
230         equals( jQuery('#form :input').serialize(),
231                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2",
232                 'Check input serialization as query string');
233
234         equals( jQuery('#testForm').serialize(),
235                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
236                 'Check form serialization as query string');
237
238         equals( jQuery('#testForm :input').serialize(),
239                 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
240                 'Check input serialization as query string');
241
242         equals( jQuery('#form, #testForm').serialize(),
243                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
244                 'Multiple form serialization as query string');
245
246   /* Temporarily disabled. Opera 10 has problems with form serialization.
247         equals( jQuery('#form, #testForm :input').serialize(),
248                 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
249                 'Mixed form/input serialization as query string');
250         */
251 });
252
253 test("jQuery.param()", function() {
254         expect(13);
255         
256   equals( jQuery.param.traditional, undefined, "traditional flag, undefined by default" );
257   
258         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
259         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
260
261         params = {someName: [1, 2, 3], regularThing: "blah" };
262         equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
263
264         params = {foo: ['a', 'b', 'c']};
265         equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
266
267         params = {foo: ["baz", 42, "All your base are belong to us"] };
268         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
269
270         params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
271         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" );
272         
273         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?" };
274         equals( jQuery.param(params), "a%5B%5D=1&a%5B%5D=2&b%5Bc%5D=3&b%5Bd%5D%5B%5D=4&b%5Bd%5D%5B%5D=5&b%5Be%5D%5Bx%5D%5B%5D=6&b%5Be%5D%5By%5D=7&b%5Be%5D%5Bz%5D%5B%5D=8&b%5Be%5D%5Bz%5D%5B%5D=9&b%5Bf%5D=true&b%5Bg%5D=false&b%5Bh%5D=undefined&i%5B%5D=10&i%5B%5D=11&j=true&k=false&l%5B%5D=undefined&l%5B%5D=0&m=cowboy+hat%3F", "huge structure" );
275         
276         jQuery.param.traditional = true;
277         
278         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
279         equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
280
281         params = {someName: [1, 2, 3], regularThing: "blah" };
282         equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
283
284         params = {foo: ['a', 'b', 'c']};
285         equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
286
287         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
288         equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
289
290         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
291         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" );
292         
293         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?" };
294         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" );
295
296 });
297
298 test("synchronous request", function() {
299         expect(1);
300         ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
301 });
302
303 test("synchronous request with callbacks", function() {
304         expect(2);
305         var result;
306         jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
307         ok( /^{ "data"/.test( result ), "check returned text" );
308 });
309
310 test("pass-through request object", function() {
311         expect(8);
312         stop();
313
314         var target = "data/name.html";
315         var successCount = 0;
316         var errorCount = 0;
317         var errorEx = "";
318         var success = function() {
319                 successCount++;
320         };
321         jQuery("#foo").ajaxError(function (e, xml, s, ex) {
322                 errorCount++;
323                 errorEx += ": " + xml.status;
324         });
325         jQuery("#foo").one('ajaxStop', function () {
326                 equals(successCount, 5, "Check all ajax calls successful");
327                 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
328                 jQuery("#foo").unbind('ajaxError');
329
330                 start();
331         });
332
333         ok( jQuery.get(url(target), success), "get" );
334         ok( jQuery.post(url(target), success), "post" );
335         ok( jQuery.getScript(url("data/test.js"), success), "script" );
336         ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
337         ok( jQuery.ajax({url: url(target), success: success}), "generic" );
338 });
339
340 test("ajax cache", function () {
341         expect(18);
342         
343         stop();
344
345         var count = 0;
346
347         jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
348                 var re = /_=(.*?)(&|$)/g;
349                 var oldOne = null;
350                 for (var i = 0; i < 6; i++) {
351                         var ret = re.exec(s.url);
352                         if (!ret) {
353                                 break;
354                         }
355                         oldOne = ret[1];
356                 }
357                 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
358                 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
359                 if(++count == 6)
360                         start();
361         });
362
363         ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
364         ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
365         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
366         ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
367         ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
368         ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
369 });
370
371 test("global ajaxSettings", function() {
372         expect(2);
373
374         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
375         var orig = { url: "data/with_fries.xml" };
376         var t;
377
378         jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
379
380         t = jQuery.extend({}, orig);
381         t.data = {};
382         jQuery.ajax(t);
383         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
384
385         t = jQuery.extend({}, orig);
386         t.data = { zoo: 'a', ping: 'b' };
387         jQuery.ajax(t);
388         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' }" );
389
390         jQuery.ajaxSettings = tmp;
391 });
392
393 test("load(String)", function() {
394         expect(1);
395         stop(); // check if load can be called with only url
396         jQuery('#first').load("data/name.html", start);
397 });
398
399 test("load('url selector')", function() {
400         expect(1);
401         stop(); // check if load can be called with only url
402         jQuery('#first').load("data/test3.html div.user", function(){
403                 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
404                 start();
405         });
406 });
407
408 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
409         expect(1);
410         stop();
411         jQuery.ajaxSetup({ dataType: "json" });
412         jQuery("#first").ajaxComplete(function (e, xml, s) {
413                 equals( s.dataType, "html", "Verify the load() dataType was html" );
414                 jQuery("#first").unbind("ajaxComplete");
415                 jQuery.ajaxSetup({ dataType: "" });
416                 start();
417         });
418         jQuery('#first').load("data/test3.html");
419 });
420
421 test("load(String, Function) - simple: inject text into DOM", function() {
422         expect(2);
423         stop();
424         jQuery('#first').load(url("data/name.html"), function() {
425                 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
426                 start();
427         });
428 });
429
430 test("load(String, Function) - check scripts", function() {
431         expect(7);
432         stop();
433
434         var verifyEvaluation = function() {
435                 equals( foobar, "bar", 'Check if script src was evaluated after load' );
436                 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
437
438                 start();
439         };
440         jQuery('#first').load(url('data/test.html'), function() {
441                 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
442                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
443                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
444                 setTimeout(verifyEvaluation, 600);
445         });
446 });
447
448 test("load(String, Function) - check file with only a script tag", function() {
449         expect(3);
450         stop();
451
452         jQuery('#first').load(url('data/test2.html'), function() {
453                 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
454                 equals( testFoo, "foo", 'Check if script was evaluated after load' );
455
456                 start();
457         });
458 });
459
460 test("load(String, Object, Function)", function() {
461         expect(2);
462         stop();
463
464         jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
465                 var $post = jQuery(this).find('#post');
466                 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
467                 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
468                 start();
469         });
470 });
471
472 test("load(String, String, Function)", function() {
473         expect(2);
474         stop();
475
476         jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
477                 var $get = jQuery(this).find('#get');
478                 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
479                 equals( $get.find('#bar').text(), 'ok', 'Check if a      of data is passed correctly');
480                 start();
481         });
482 });
483
484 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
485         expect(2);
486         stop();
487         jQuery.get(url('data/dashboard.xml'), function(xml) {
488                 var content = [];
489                 jQuery('tab', xml).each(function() {
490                         content.push(jQuery(this).text());
491                 });
492                 equals( content[0], 'blabla', 'Check first tab');
493                 equals( content[1], 'blublu', 'Check second tab');
494                 start();
495         });
496 });
497
498 test("jQuery.getScript(String, Function) - with callback", function() {
499         expect(2);
500         stop();
501         jQuery.getScript(url("data/test.js"), function() {
502                 equals( foobar, "bar", 'Check if script was evaluated' );
503                 setTimeout(start, 100);
504         });
505 });
506
507 test("jQuery.getScript(String, Function) - no callback", function() {
508         expect(1);
509         stop();
510         jQuery.getScript(url("data/test.js"), function(){
511                 start();
512         });
513 });
514
515 test("jQuery.ajax() - JSONP, Local", function() {
516         expect(7);
517
518         var count = 0;
519         function plus(){ if ( ++count == 7 ) start(); }
520
521         stop();
522
523         jQuery.ajax({
524                 url: "data/jsonp.php",
525                 dataType: "jsonp",
526                 success: function(data){
527                         ok( data.data, "JSON results returned (GET, no callback)" );
528                         plus();
529                 },
530                 error: function(data){
531                         ok( false, "Ajax error JSON (GET, no callback)" );
532                         plus();
533                 }
534         });
535
536         jQuery.ajax({
537                 url: "data/jsonp.php?callback=?",
538                 dataType: "jsonp",
539                 success: function(data){
540                         ok( data.data, "JSON results returned (GET, url callback)" );
541                         plus();
542                 },
543                 error: function(data){
544                         ok( false, "Ajax error JSON (GET, url callback)" );
545                         plus();
546                 }
547         });
548
549         jQuery.ajax({
550                 url: "data/jsonp.php",
551                 dataType: "jsonp",
552                 data: "callback=?",
553                 success: function(data){
554                         ok( data.data, "JSON results returned (GET, data callback)" );
555                         plus();
556                 },
557                 error: function(data){
558                         ok( false, "Ajax error JSON (GET, data callback)" );
559                         plus();
560                 }
561         });
562
563         jQuery.ajax({
564                 url: "data/jsonp.php",
565                 dataType: "jsonp",
566                 jsonp: "callback",
567                 success: function(data){
568                         ok( data.data, "JSON results returned (GET, data obj callback)" );
569                         plus();
570                 },
571                 error: function(data){
572                         ok( false, "Ajax error JSON (GET, data obj callback)" );
573                         plus();
574                 }
575         });
576
577         jQuery.ajax({
578                 type: "POST",
579                 url: "data/jsonp.php",
580                 dataType: "jsonp",
581                 success: function(data){
582                         ok( data.data, "JSON results returned (POST, no callback)" );
583                         plus();
584                 },
585                 error: function(data){
586                         ok( false, "Ajax error JSON (GET, data obj callback)" );
587                         plus();
588                 }
589         });
590
591         jQuery.ajax({
592                 type: "POST",
593                 url: "data/jsonp.php",
594                 data: "callback=?",
595                 dataType: "jsonp",
596                 success: function(data){
597                         ok( data.data, "JSON results returned (POST, data callback)" );
598                         plus();
599                 },
600                 error: function(data){
601                         ok( false, "Ajax error JSON (POST, data callback)" );
602                         plus();
603                 }
604         });
605
606         jQuery.ajax({
607                 type: "POST",
608                 url: "data/jsonp.php",
609                 jsonp: "callback",
610                 dataType: "jsonp",
611                 success: function(data){
612                         ok( data.data, "JSON results returned (POST, data obj callback)" );
613                         plus();
614                 },
615                 error: function(data){
616                         ok( false, "Ajax error JSON (POST, data obj callback)" );
617                         plus();
618                 }
619         });
620 });
621
622 test("jQuery.ajax() - JSONP, Remote", function() {
623         expect(4);
624
625         var count = 0;
626         function plus(){ if ( ++count == 4 ) start(); }
627
628         var base = window.location.href.replace(/\?.*$/, "");
629
630         stop();
631
632         jQuery.ajax({
633                 url: base + "data/jsonp.php",
634                 dataType: "jsonp",
635                 success: function(data){
636                         ok( data.data, "JSON results returned (GET, no callback)" );
637                         plus();
638                 },
639                 error: function(data){
640                         ok( false, "Ajax error JSON (GET, no callback)" );
641                         plus();
642                 }
643         });
644
645         jQuery.ajax({
646                 url: base + "data/jsonp.php?callback=?",
647                 dataType: "jsonp",
648                 success: function(data){
649                         ok( data.data, "JSON results returned (GET, url callback)" );
650                         plus();
651                 },
652                 error: function(data){
653                         ok( false, "Ajax error JSON (GET, url callback)" );
654                         plus();
655                 }
656         });
657
658         jQuery.ajax({
659                 url: base + "data/jsonp.php",
660                 dataType: "jsonp",
661                 data: "callback=?",
662                 success: function(data){
663                         ok( data.data, "JSON results returned (GET, data callback)" );
664                         plus();
665                 },
666                 error: function(data){
667                         ok( false, "Ajax error JSON (GET, data callback)" );
668                         plus();
669                 }
670         });
671
672         jQuery.ajax({
673                 url: base + "data/jsonp.php",
674                 dataType: "jsonp",
675                 jsonp: "callback",
676                 success: function(data){
677                         ok( data.data, "JSON results returned (GET, data obj callback)" );
678                         plus();
679                 },
680                 error: function(data){
681                         ok( false, "Ajax error JSON (GET, data obj callback)" );
682                         plus();
683                 }
684         });
685 });
686
687 test("jQuery.ajax() - script, Remote", function() {
688         expect(2);
689
690         var base = window.location.href.replace(/\?.*$/, "");
691
692         stop();
693
694         jQuery.ajax({
695                 url: base + "data/test.js",
696                 dataType: "script",
697                 success: function(data){
698                         ok( foobar, "Script results returned (GET, no callback)" );
699                         start();
700                 }
701         });
702 });
703
704 test("jQuery.ajax() - script, Remote with POST", function() {
705         expect(3);
706
707         var base = window.location.href.replace(/\?.*$/, "");
708
709         stop();
710
711         jQuery.ajax({
712                 url: base + "data/test.js",
713                 type: "POST",
714                 dataType: "script",
715                 success: function(data, status){
716                         ok( foobar, "Script results returned (POST, no callback)" );
717                         equals( status, "success", "Script results returned (POST, no callback)" );
718                         start();
719                 },
720                 error: function(xhr) {
721                         ok( false, "ajax error, status code: " + xhr.status );
722                         start();
723                 }
724         });
725 });
726
727 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
728         expect(2);
729
730         var base = window.location.href.replace(/\?.*$/, "");
731         base = base.replace(/^.*?\/\//, "//");
732
733         stop();
734
735         jQuery.ajax({
736                 url: base + "data/test.js",
737                 dataType: "script",
738                 success: function(data){
739                         ok( foobar, "Script results returned (GET, no callback)" );
740                         start();
741                 }
742         });
743 });
744
745 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
746         expect(5);
747         stop();
748         jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
749           ok( json.length >= 2, "Check length");
750           equals( json[0].name, 'John', 'Check JSON: first, name' );
751           equals( json[0].age, 21, 'Check JSON: first, age' );
752           equals( json[1].name, 'Peter', 'Check JSON: second, name' );
753           equals( json[1].age, 25, 'Check JSON: second, age' );
754           start();
755         });
756 });
757
758 test("jQuery.getJSON(String, Function) - JSON object", function() {
759         expect(2);
760         stop();
761         jQuery.getJSON(url("data/json.php"), function(json) {
762           if (json && json.data) {
763                   equals( json.data.lang, 'en', 'Check JSON: lang' );
764                   equals( json.data.length, 25, 'Check JSON: length' );
765           }
766           start();
767         });
768 });
769
770 test("jQuery.getJSON - Using Native JSON", function() {
771         expect(2);
772         
773         var old = window.JSON;
774         JSON = {
775                 parse: function(str){
776                         ok( true, "Verifying that parse method was run" );
777                         return true;
778                 }
779         };
780
781         stop();
782         jQuery.getJSON(url("data/json.php"), function(json) {
783                 if (!old)
784                         delete window.JSON
785                 else
786                         window.JSON = old;
787           equals( json, true, "Verifying return value" );
788           start();
789         });
790 });
791
792 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
793         expect(2);
794
795         var base = window.location.href.replace(/\?.*$/, "");
796
797         stop();
798         jQuery.getJSON(url(base + "data/json.php"), function(json) {
799           equals( json.data.lang, 'en', 'Check JSON: lang' );
800           equals( json.data.length, 25, 'Check JSON: length' );
801           start();
802         });
803 });
804
805 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
806         expect(4);
807         stop();
808         var done = 0;
809
810         jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
811           jQuery('math', xml).each(function() {
812                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
813                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
814                  });
815           if ( ++done === 2 ) start();
816         });
817
818         jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
819           jQuery('math', xml).each(function() {
820                         equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
821                         equals( jQuery('result', this).text(), '3', 'Check for XML' );
822                  });
823           if ( ++done === 2 ) start();
824         });
825 });
826
827 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
828         stop();
829
830         var passed = 0;
831
832         jQuery.ajaxSetup({timeout: 1000});
833
834         var pass = function() {
835                 passed++;
836                 if ( passed == 2 ) {
837                         ok( true, 'Check local and global callbacks after timeout' );
838                         jQuery('#main').unbind("ajaxError");
839                         start();
840                 }
841         };
842
843         var fail = function(a,b,c) {
844                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
845                 start();
846         };
847
848         jQuery('#main').ajaxError(pass);
849
850         jQuery.ajax({
851           type: "GET",
852           url: url("data/name.php?wait=5"),
853           error: pass,
854           success: fail
855         });
856
857         // reset timeout
858         jQuery.ajaxSetup({timeout: 0});
859 });
860
861 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
862         stop();
863         jQuery.ajaxSetup({timeout: 50});
864
865         jQuery.ajax({
866           type: "GET",
867           timeout: 15000,
868           url: url("data/name.php?wait=1"),
869           error: function() {
870                    ok( false, 'Check for local timeout failed' );
871                    start();
872           },
873           success: function() {
874                 ok( true, 'Check for local timeout' );
875                 start();
876           }
877         });
878
879         // reset timeout
880         jQuery.ajaxSetup({timeout: 0});
881 });
882
883 test("jQuery.ajax - simple get", function() {
884         expect(1);
885         stop();
886         jQuery.ajax({
887           type: "GET",
888           url: url("data/name.php?name=foo"),
889           success: function(msg){
890                 equals( msg, 'bar', 'Check for GET' );
891                 start();
892           }
893         });
894 });
895
896 test("jQuery.ajax - simple post", function() {
897         expect(1);
898         stop();
899         jQuery.ajax({
900           type: "POST",
901           url: url("data/name.php"),
902           data: "name=peter",
903           success: function(msg){
904                 equals( msg, 'pan', 'Check for POST' );
905                 start();
906           }
907         });
908 });
909
910 test("ajaxSetup()", function() {
911         expect(1);
912         stop();
913         jQuery.ajaxSetup({
914                 url: url("data/name.php?name=foo"),
915                 success: function(msg){
916                         equals( msg, 'bar', 'Check for GET' );
917                         start();
918                 }
919         });
920         jQuery.ajax();
921 });
922
923 /*
924 test("custom timeout does not set error message when timeout occurs, see #970", function() {
925         stop();
926         jQuery.ajax({
927                 url: "data/name.php?wait=1",
928                 timeout: 500,
929                 error: function(request, status) {
930                         ok( status != null, "status shouldn't be null in error handler" );
931                         equals( "timeout", status );
932                         start();
933                 }
934         });
935 });
936 */
937
938 test("data option: evaluate function values (#2806)", function() {
939         stop();
940         jQuery.ajax({
941                 url: "data/echoQuery.php",
942                 data: {
943                         key: function() {
944                                 return "value";
945                         }
946                 },
947                 success: function(result) {
948                         equals( result, "key=value" );
949                         start();
950                 }
951         })
952 });
953
954 test("jQuery.ajax - If-Modified-Since support", function() {
955         expect( 3 );
956
957         stop();
958
959         var url = "data/if_modified_since.php?ts=" + new Date();
960
961         jQuery.ajax({
962                 url: url,
963                 ifModified: true,
964                 success: function(data, status) { 
965                         equals(status, "success");
966                         
967                         jQuery.ajax({
968                                 url: url,
969                                 ifModified: true,
970                                 success: function(data, status) { 
971                                         if ( data === "FAIL" ) {
972                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
973                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
974                                         } else {
975                                                 equals(status, "notmodified");
976                                                 ok(data == null, "response body should be empty")
977                                         }
978                                         start();
979                                 }
980                         });
981                 }
982         });
983 });
984
985 test("jQuery.ajax - Etag support", function() {
986         expect( 3 );
987
988         stop();
989
990         var url = "data/etag.php?ts=" + new Date();
991
992         jQuery.ajax({
993                 url: url,
994                 ifModified: true,
995                 success: function(data, status) { 
996                         equals(status, "success");
997                         
998                         jQuery.ajax({
999                                 url: url,
1000                                 ifModified: true,
1001                                 success: function(data, status) { 
1002                                         if ( data === "FAIL" ) {
1003                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1004                                                 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1005                                         } else {
1006                                                 equals(status, "notmodified");
1007                                                 ok(data == null, "response body should be empty")
1008                                         }
1009                                         start();
1010                                 }
1011                         });
1012                 }
1013         });
1014 });
1015
1016 }
1017
1018 //}