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 ) {
10 test("jQuery.ajax() - success callbacks", function() {
13 jQuery.ajaxSetup({ timeout: 0 });
17 jQuery('#foo').ajaxStart(function(){
18 ok( true, "ajaxStart" );
19 }).ajaxStop(function(){
20 ok( true, "ajaxStop" );
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" );
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"); }
41 test("jQuery.ajax() - success callbacks - (url, options) syntax", function() {
44 jQuery.ajaxSetup({ timeout: 0 });
48 setTimeout(function(){
49 jQuery('#foo').ajaxStart(function(){
50 ok( true, "ajaxStart" );
51 }).ajaxStop(function(){
52 ok( true, "ajaxStop" );
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" );
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"); }
73 test("jQuery.ajax() - success/error callbacks (remote)", function() {
75 var supports = jQuery.support.cors;
77 expect( supports ? 9 : 4 );
79 jQuery.ajaxSetup({ timeout: 0 });
83 setTimeout(function(){
84 jQuery('#foo').ajaxStart(function(){
85 ok( true, "ajaxStart" );
86 }).ajaxStop(function(){
87 ok( true, "ajaxStop" );
89 }).ajaxSend(function(){
90 ok( supports , "ajaxSend" );
91 }).ajaxComplete(function(){
92 ok( true, "ajaxComplete" );
93 }).ajaxError(function(){
94 ok( ! supports, "ajaxError" );
95 }).ajaxSuccess(function(){
96 ok( supports, "ajaxSuccess" );
100 // JULIAN TODO: Get an url especially for jQuery
101 url: "http://rockstarapps.com/test.php",
103 beforeSend: function(){ ok(supports, "beforeSend"); },
104 success: function( val ){ ok(supports, "success"); ok(supports && val.length, "data received"); },
105 error: function(_ , a , b ){ ok(false, "error"); },
106 complete: function(){ ok(supports, "complete"); }
111 test("jQuery.ajax() - success callbacks (late binding)", function() {
114 jQuery.ajaxSetup({ timeout: 0 });
118 setTimeout(function(){
119 jQuery('#foo').ajaxStart(function(){
120 ok( true, "ajaxStart" );
121 }).ajaxStop(function(){
122 ok( true, "ajaxStop" );
124 }).ajaxSend(function(){
125 ok( true, "ajaxSend" );
126 }).ajaxComplete(function(){
127 ok( true, "ajaxComplete" );
128 }).ajaxError(function(){
129 ok( false, "ajaxError" );
130 }).ajaxSuccess(function(){
131 ok( true, "ajaxSuccess" );
135 url: url("data/name.html"),
136 beforeSend: function(){ ok(true, "beforeSend"); }
138 .complete(function(){ ok(true, "complete"); })
139 .success(function(){ ok(true, "success"); })
140 .error(function(){ ok(false, "error"); });
144 test("jQuery.ajax() - success callbacks (oncomplete binding)", function() {
147 jQuery.ajaxSetup({ timeout: 0 });
151 setTimeout(function(){
152 jQuery('#foo').ajaxStart(function(){
153 ok( true, "ajaxStart" );
154 }).ajaxStop(function(){
155 ok( true, "ajaxStop" );
156 }).ajaxSend(function(){
157 ok( true, "ajaxSend" );
158 }).ajaxComplete(function(){
159 ok( true, "ajaxComplete" );
160 }).ajaxError(function(){
161 ok( false, "ajaxError" );
162 }).ajaxSuccess(function(){
163 ok( true, "ajaxSuccess" );
167 url: url("data/name.html"),
168 beforeSend: function(){ ok(true, "beforeSend"); },
169 complete: function(xhr) {
171 .complete(function(){ ok(true, "complete"); })
172 .success(function(){ ok(true, "success"); })
173 .error(function(){ ok(false, "error"); })
174 .complete(function(){ start(); });
180 test("jQuery.ajax() - success callbacks (very late binding)", function() {
183 jQuery.ajaxSetup({ timeout: 0 });
187 setTimeout(function(){
188 jQuery('#foo').ajaxStart(function(){
189 ok( true, "ajaxStart" );
190 }).ajaxStop(function(){
191 ok( true, "ajaxStop" );
192 }).ajaxSend(function(){
193 ok( true, "ajaxSend" );
194 }).ajaxComplete(function(){
195 ok( true, "ajaxComplete" );
196 }).ajaxError(function(){
197 ok( false, "ajaxError" );
198 }).ajaxSuccess(function(){
199 ok( true, "ajaxSuccess" );
203 url: url("data/name.html"),
204 beforeSend: function(){ ok(true, "beforeSend"); },
205 complete: function(xhr) {
206 setTimeout (function() {
208 .complete(function(){ ok(true, "complete"); })
209 .success(function(){ ok(true, "success"); })
210 .error(function(){ ok(false, "error"); })
211 .complete(function(){ start(); });
218 test("jQuery.ajax() - success callbacks (order)", function() {
221 jQuery.ajaxSetup({ timeout: 0 });
227 setTimeout(function(){
229 url: url("data/name.html"),
230 success: function( _1 , _2 , xhr ) {
231 xhr.success(function() {
232 xhr.success(function() {
239 complete: function() {
240 strictEqual(testString, "ABCDE", "Proper order");
243 }).success(function() {
245 }).success(function() {
251 test("jQuery.ajax() - error callbacks", function() {
255 jQuery('#foo').ajaxStart(function(){
256 ok( true, "ajaxStart" );
257 }).ajaxStop(function(){
258 ok( true, "ajaxStop" );
260 }).ajaxSend(function(){
261 ok( true, "ajaxSend" );
262 }).ajaxComplete(function(){
263 ok( true, "ajaxComplete" );
264 }).ajaxError(function(){
265 ok( true, "ajaxError" );
266 }).ajaxSuccess(function(){
267 ok( false, "ajaxSuccess" );
270 jQuery.ajaxSetup({ timeout: 500 });
273 url: url("data/name.php?wait=5"),
274 beforeSend: function(){ ok(true, "beforeSend"); },
275 success: function(){ ok(false, "success"); },
276 error: function(){ ok(true, "error"); },
277 complete: function(){ ok(true, "complete"); }
281 test(".ajax() - headers" , function() {
287 var requestHeaders = {
289 "SometHing-elsE": "other value",
290 OthEr: "something else"
295 for( i in requestHeaders ) {
299 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
300 headers: requestHeaders,
301 success: function( data , _ , xhr ) {
303 for ( i in requestHeaders ) {
304 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
306 tmp = tmp.join( "" );
308 equals( data , tmp , "Headers were sent" );
309 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
312 error: function(){ ok(false, "error"); }
317 test(".ajax() - hash", function() {
321 url: "data/name.html#foo",
322 beforeSend: function( xhr, settings ) {
323 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
329 url: "data/name.html?abc#foo",
330 beforeSend: function( xhr, settings ) {
331 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
337 url: "data/name.html?abc#foo",
338 data: { "test": 123 },
339 beforeSend: function( xhr, settings ) {
340 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
346 test(".ajax() - 304", function() {
351 url: url("data/notmodified.php"),
352 success: function(){ ok(true, "304 ok"); },
353 // Do this because opera simply refuses to implement 304 handling :(
354 // A feature-driven way of detecting this would be appreciated
355 // See: http://gist.github.com/599419
356 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
357 complete: function(xhr){ start(); }
361 test(".load()) - 404 error callbacks", function() {
365 jQuery('#foo').ajaxStart(function(){
366 ok( true, "ajaxStart" );
367 }).ajaxStop(function(){
368 ok( true, "ajaxStop" );
370 }).ajaxSend(function(){
371 ok( true, "ajaxSend" );
372 }).ajaxComplete(function(){
373 ok( true, "ajaxComplete" );
374 }).ajaxError(function(){
375 ok( true, "ajaxError" );
376 }).ajaxSuccess(function(){
377 ok( false, "ajaxSuccess" );
380 jQuery("<div/>").load("data/404.html", function(){
381 ok(true, "complete");
385 test("jQuery.ajax() - abort", function() {
389 jQuery('#foo').ajaxStart(function(){
390 ok( true, "ajaxStart" );
391 }).ajaxStop(function(){
392 ok( true, "ajaxStop" );
394 }).ajaxSend(function(){
395 ok( true, "ajaxSend" );
396 }).ajaxComplete(function(){
397 ok( true, "ajaxComplete" );
400 var xhr = jQuery.ajax({
401 url: url("data/name.php?wait=5"),
402 beforeSend: function(){ ok(true, "beforeSend"); },
403 complete: function(){ ok(true, "complete"); }
406 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
409 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
412 test("Ajax events with context", function() {
416 var context = document.createElement("div");
419 equals( this, context, e.type );
422 function callback(msg){
424 equals( this, context, "context is preserved on callback " + msg );
428 function nocallback(msg){
430 equals( typeof this.url, "string", "context is settings on callback " + msg );
434 jQuery('#foo').add(context)
441 url: url("data/name.html"),
442 beforeSend: callback("beforeSend"),
443 success: callback("success"),
444 error: callback("error"),
446 callback("complete").call(this);
449 url: url("data/404.html"),
451 beforeSend: callback("beforeSend"),
452 error: callback("error"),
453 complete: function(){
454 callback("complete").call(this);
456 jQuery('#foo').add(context).unbind();
459 url: url("data/404.html"),
460 beforeSend: nocallback("beforeSend"),
461 error: nocallback("error"),
462 complete: function(){
463 nocallback("complete").call(this);
474 test("jQuery.ajax context modification", function() {
482 url: url("data/name.html"),
484 beforeSend: function(){
487 complete: function() {
492 equals( obj.test, "foo", "Make sure the original object is maintained." );
495 test("jQuery.ajax() - disabled globals", function() {
499 jQuery('#foo').ajaxStart(function(){
500 ok( false, "ajaxStart" );
501 }).ajaxStop(function(){
502 ok( false, "ajaxStop" );
503 }).ajaxSend(function(){
504 ok( false, "ajaxSend" );
505 }).ajaxComplete(function(){
506 ok( false, "ajaxComplete" );
507 }).ajaxError(function(){
508 ok( false, "ajaxError" );
509 }).ajaxSuccess(function(){
510 ok( false, "ajaxSuccess" );
515 url: url("data/name.html"),
516 beforeSend: function(){ ok(true, "beforeSend"); },
517 success: function(){ ok(true, "success"); },
518 error: function(){ ok(false, "error"); },
519 complete: function(){
520 ok(true, "complete");
521 setTimeout(function(){ start(); }, 13);
526 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
530 url: url("data/with_fries.xml"),
532 success: function(resp) {
533 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
534 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
535 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
541 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
545 url: url("data/with_fries_over_jsonp.php"),
546 dataType: "jsonp xml",
547 success: function(resp) {
548 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
549 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
550 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
553 error: function(_1,_2,error) {
560 test("jQuery.ajax - HEAD requests", function() {
565 url: url("data/name.html"),
567 success: function(data, status, xhr){
568 var h = xhr.getAllResponseHeaders();
569 ok( /Date/i.test(h), 'No Date in HEAD response' );
572 url: url("data/name.html"),
573 data: { whip_it: "good" },
575 success: function(data, status, xhr){
576 var h = xhr.getAllResponseHeaders();
577 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
586 test("jQuery.ajax - beforeSend", function() {
592 jQuery.ajaxSetup({ timeout: 0 });
595 url: url("data/name.html"),
596 beforeSend: function(xml) {
599 success: function(data) {
600 ok( check, "check beforeSend was executed" );
606 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
608 var request = jQuery.ajax({
609 url: url("data/name.html"),
610 beforeSend: function() {
611 ok( true, "beforeSend got called, canceling" );
614 success: function() {
615 ok( false, "request didn't get canceled" );
617 complete: function() {
618 ok( false, "request didn't get canceled" );
621 ok( false, "request didn't get canceled" );
624 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
627 test("jQuery.ajax - beforeSend, cancel request manually", function() {
629 var request = jQuery.ajax({
630 url: url("data/name.html"),
631 beforeSend: function(xhr) {
632 ok( true, "beforeSend got called, canceling" );
635 success: function() {
636 ok( false, "request didn't get canceled" );
638 complete: function() {
639 ok( false, "request didn't get canceled" );
642 ok( false, "request didn't get canceled" );
645 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
648 window.foobar = null;
649 window.testFoo = undefined;
651 test("jQuery.ajax - dataType html", function() {
655 var verifyEvaluation = function() {
656 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
657 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
664 url: url("data/test.html"),
665 success: function(data) {
666 jQuery("#ap").html(data);
667 ok( data.match(/^html text/), 'Check content for datatype html' );
668 setTimeout(verifyEvaluation, 600);
673 test("serialize()", function() {
676 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
677 jQuery("#search").after(
678 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
679 '<input type="number" id="html5number" name="number" value="43" />'
682 equals( jQuery('#form').serialize(),
683 "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",
684 'Check form serialization as query string');
686 equals( jQuery('#form :input').serialize(),
687 "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",
688 'Check input serialization as query string');
690 equals( jQuery('#testForm').serialize(),
691 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
692 'Check form serialization as query string');
694 equals( jQuery('#testForm :input').serialize(),
695 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
696 'Check input serialization as query string');
698 equals( jQuery('#form, #testForm').serialize(),
699 "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%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
700 'Multiple form serialization as query string');
702 /* Temporarily disabled. Opera 10 has problems with form serialization.
703 equals( jQuery('#form, #testForm :input').serialize(),
704 "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%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
705 'Mixed form/input serialization as query string');
707 jQuery("#html5email, #html5number").remove();
710 test("jQuery.param()", function() {
713 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
715 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
716 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
718 params = {someName: [1, 2, 3], regularThing: "blah" };
719 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
721 params = {foo: ['a', 'b', 'c']};
722 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
724 params = {foo: ["baz", 42, "All your base are belong to us"] };
725 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
727 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
728 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" );
730 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?" };
731 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" );
733 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 ] };
734 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" );
736 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?" };
737 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" );
739 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." );
741 // Make sure empty arrays and objects are handled #6481
742 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
743 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
744 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
746 jQuery.ajaxSetup({ traditional: true });
748 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
749 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
751 params = {someName: [1, 2, 3], regularThing: "blah" };
752 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
754 params = {foo: ['a', 'b', 'c']};
755 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
757 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
758 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
760 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
761 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" );
763 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?" };
764 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" );
766 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 ] };
767 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)" );
769 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?" };
770 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" );
772 params = { param1: null };
773 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
776 test("synchronous request", function() {
778 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
781 test("synchronous request with callbacks", function() {
784 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
785 ok( /^{ "data"/.test( result ), "check returned text" );
788 test("pass-through request object", function() {
792 var target = "data/name.html";
793 var successCount = 0;
796 var success = function() {
799 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
801 errorEx += ": " + xml.status;
803 jQuery("#foo").one('ajaxStop', function () {
804 equals(successCount, 5, "Check all ajax calls successful");
805 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
806 jQuery("#foo").unbind('ajaxError');
811 ok( jQuery.get(url(target), success), "get" );
812 ok( jQuery.post(url(target), success), "post" );
813 ok( jQuery.getScript(url("data/test.js"), success), "script" );
814 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
815 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
818 test("ajax cache", function () {
825 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
826 var re = /_=(.*?)(&|$)/g;
828 for (var i = 0; i < 6; i++) {
829 var ret = re.exec(s.url);
835 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
836 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
841 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
842 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
843 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
844 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
845 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
846 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
851 * The assertions expect that the passed-in object will be modified,
852 * which shouldn't be the case. Fixes #5439.
853 test("global ajaxSettings", function() {
856 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
857 var orig = { url: "data/with_fries.xml" };
860 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
862 t = jQuery.extend({}, orig);
865 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
867 t = jQuery.extend({}, orig);
868 t.data = { zoo: 'a', ping: 'b' };
870 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' }" );
872 jQuery.ajaxSettings = tmp;
876 test("load(String)", function() {
878 stop(); // check if load can be called with only url
879 jQuery('#first').load("data/name.html", start);
882 test("load('url selector')", function() {
884 stop(); // check if load can be called with only url
885 jQuery('#first').load("data/test3.html div.user", function(){
886 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
891 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
894 jQuery.ajaxSetup({ dataType: "json" });
895 jQuery("#first").ajaxComplete(function (e, xml, s) {
896 equals( s.dataType, "html", "Verify the load() dataType was html" );
897 jQuery("#first").unbind("ajaxComplete");
898 jQuery.ajaxSetup({ dataType: "" });
901 jQuery('#first').load("data/test3.html");
904 test("load(String, Function) - simple: inject text into DOM", function() {
907 jQuery('#first').load(url("data/name.html"), function() {
908 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
913 test("load(String, Function) - check scripts", function() {
917 var verifyEvaluation = function() {
918 equals( foobar, "bar", 'Check if script src was evaluated after load' );
919 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
923 jQuery('#first').load(url('data/test.html'), function() {
924 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
925 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
926 equals( testFoo, "foo", 'Check if script was evaluated after load' );
927 setTimeout(verifyEvaluation, 600);
931 test("load(String, Function) - check file with only a script tag", function() {
935 jQuery('#first').load(url('data/test2.html'), function() {
936 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
937 equals( testFoo, "foo", 'Check if script was evaluated after load' );
943 test("load(String, Object, Function)", function() {
947 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
948 var $post = jQuery(this).find('#post');
949 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
950 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
955 test("load(String, String, Function)", function() {
959 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
960 var $get = jQuery(this).find('#get');
961 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
962 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
967 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
970 jQuery.get(url('data/dashboard.xml'), function(xml) {
972 jQuery('tab', xml).each(function() {
973 content.push(jQuery(this).text());
975 equals( content[0], 'blabla', 'Check first tab');
976 equals( content[1], 'blublu', 'Check second tab');
981 test("jQuery.getScript(String, Function) - with callback", function() {
984 jQuery.getScript(url("data/test.js"), function() {
985 equals( foobar, "bar", 'Check if script was evaluated' );
986 setTimeout(start, 100);
990 test("jQuery.getScript(String, Function) - no callback", function() {
993 jQuery.getScript(url("data/test.js"), function(){
998 test("jQuery.ajax() - JSONP, Local", function() {
1002 function plus(){ if ( ++count == 9 ) start(); }
1007 url: "data/jsonp.php",
1009 success: function(data){
1010 ok( data.data, "JSON results returned (GET, no callback)" );
1013 error: function(data){
1014 ok( false, "Ajax error JSON (GET, no callback)" );
1020 url: "data/jsonp.php?callback=?",
1022 success: function(data){
1023 ok( data.data, "JSON results returned (GET, url callback)" );
1026 error: function(data){
1027 ok( false, "Ajax error JSON (GET, url callback)" );
1033 url: "data/jsonp.php",
1036 success: function(data){
1037 ok( data.data, "JSON results returned (GET, data callback)" );
1040 error: function(data){
1041 ok( false, "Ajax error JSON (GET, data callback)" );
1047 url: "data/jsonp.php",
1050 success: function(data){
1051 ok( data.data, "JSON results returned (GET, data obj callback)" );
1054 error: function(data){
1055 ok( false, "Ajax error JSON (GET, data obj callback)" );
1061 url: "data/jsonp.php",
1063 jsonpCallback: "jsonpResults",
1064 success: function(data){
1065 ok( data.data, "JSON results returned (GET, custom callback name)" );
1068 error: function(data){
1069 ok( false, "Ajax error JSON (GET, custom callback name)" );
1076 url: "data/jsonp.php",
1078 success: function(data){
1079 ok( data.data, "JSON results returned (POST, no callback)" );
1082 error: function(data){
1083 ok( false, "Ajax error JSON (GET, data obj callback)" );
1090 url: "data/jsonp.php",
1093 success: function(data){
1094 ok( data.data, "JSON results returned (POST, data callback)" );
1097 error: function(data){
1098 ok( false, "Ajax error JSON (POST, data callback)" );
1105 url: "data/jsonp.php",
1108 success: function(data){
1109 ok( data.data, "JSON results returned (POST, data obj callback)" );
1112 error: function(data){
1113 ok( false, "Ajax error JSON (POST, data obj callback)" );
1120 url: "data/jsonp.php",
1122 beforeSend: function(){
1123 strictEqual( this.cache, false, "cache must be false on JSON request" );
1130 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1134 window.jsonpResults = function(data) {
1135 ok( data.data, "JSON results returned (GET, custom callback function)" );
1136 window.jsonpResults = undefined;
1141 url: "data/jsonp.php",
1143 jsonpCallback: "jsonpResults"
1147 test("jQuery.ajax() - JSONP, Remote", function() {
1151 function plus(){ if ( ++count == 4 ) start(); }
1153 var base = window.location.href.replace(/[^\/]*$/, "");
1158 url: base + "data/jsonp.php",
1160 success: function(data){
1161 ok( data.data, "JSON results returned (GET, no callback)" );
1164 error: function(data){
1165 ok( false, "Ajax error JSON (GET, no callback)" );
1171 url: base + "data/jsonp.php?callback=?",
1173 success: function(data){
1174 ok( data.data, "JSON results returned (GET, url callback)" );
1177 error: function(data){
1178 ok( false, "Ajax error JSON (GET, url callback)" );
1184 url: base + "data/jsonp.php",
1187 success: function(data){
1188 ok( data.data, "JSON results returned (GET, data callback)" );
1191 error: function(data){
1192 ok( false, "Ajax error JSON (GET, data callback)" );
1198 url: base + "data/jsonp.php",
1201 success: function(data){
1202 ok( data.data, "JSON results returned (GET, data obj callback)" );
1205 error: function(data){
1206 ok( false, "Ajax error JSON (GET, data obj callback)" );
1212 test("jQuery.ajax() - script, Remote", function() {
1215 var base = window.location.href.replace(/[^\/]*$/, "");
1220 url: base + "data/test.js",
1222 success: function(data){
1223 ok( foobar, "Script results returned (GET, no callback)" );
1229 test("jQuery.ajax() - script, Remote with POST", function() {
1232 var base = window.location.href.replace(/[^\/]*$/, "");
1237 url: base + "data/test.js",
1240 success: function(data, status){
1241 ok( foobar, "Script results returned (POST, no callback)" );
1242 equals( status, "success", "Script results returned (POST, no callback)" );
1245 error: function(xhr) {
1246 ok( false, "ajax error, status code: " + xhr.status );
1252 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1255 var base = window.location.href.replace(/[^\/]*$/, "");
1256 base = base.replace(/^.*?\/\//, "//");
1261 url: base + "data/test.js",
1263 success: function(data){
1264 ok( foobar, "Script results returned (GET, no callback)" );
1270 test("jQuery.ajax() - malformed JSON", function() {
1276 url: "data/badjson.js",
1278 success: function(){
1279 ok( false, "Success." );
1282 error: function(xhr, msg, detailedMsg) {
1283 equals( "parsererror", msg, "A parse error occurred." );
1284 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1290 test("jQuery.ajax() - script by content-type", function() {
1296 url: "data/script.php",
1297 data: { header: "script" },
1298 success: function() {
1304 test("jQuery.ajax() - json by content-type", function() {
1310 url: "data/json.php",
1311 data: { header: "json", json: "array" },
1312 success: function( json ) {
1313 ok( json.length >= 2, "Check length");
1314 equals( json[0].name, 'John', 'Check JSON: first, name' );
1315 equals( json[0].age, 21, 'Check JSON: first, age' );
1316 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1317 equals( json[1].age, 25, 'Check JSON: second, age' );
1323 test("jQuery.ajax() - json by content-type disabled with options", function() {
1329 url: url("data/json.php"),
1330 data: { header: "json", json: "array" },
1334 success: function( text ) {
1335 equals( typeof text , "string" , "json wasn't auto-determined" );
1336 var json = this.dataConverters["text json"]( text );
1337 ok( json.length >= 2, "Check length");
1338 equals( json[0].name, 'John', 'Check JSON: first, name' );
1339 equals( json[0].age, 21, 'Check JSON: first, age' );
1340 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1341 equals( json[1].age, 25, 'Check JSON: second, age' );
1347 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1350 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1351 ok( json.length >= 2, "Check length");
1352 equals( json[0].name, 'John', 'Check JSON: first, name' );
1353 equals( json[0].age, 21, 'Check JSON: first, age' );
1354 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1355 equals( json[1].age, 25, 'Check JSON: second, age' );
1360 test("jQuery.getJSON(String, Function) - JSON object", function() {
1363 jQuery.getJSON(url("data/json.php"), function(json) {
1364 if (json && json.data) {
1365 equals( json.data.lang, 'en', 'Check JSON: lang' );
1366 equals( json.data.length, 25, 'Check JSON: length' );
1372 test("jQuery.getJSON - Using Native JSON", function() {
1375 var old = window.JSON;
1377 parse: function(str){
1378 ok( true, "Verifying that parse method was run" );
1384 jQuery.getJSON(url("data/json.php"), function(json) {
1386 equals( json, true, "Verifying return value" );
1391 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1394 var base = window.location.href.replace(/[^\/]*$/, "");
1397 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1398 equals( json.data.lang, 'en', 'Check JSON: lang' );
1399 equals( json.data.length, 25, 'Check JSON: length' );
1404 test("jQuery.post - data", function() {
1408 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1409 jQuery('math', xml).each(function() {
1410 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1411 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1417 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1422 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1423 jQuery('math', xml).each(function() {
1424 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1425 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1427 if ( ++done === 2 ) start();
1430 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1431 jQuery('math', xml).each(function() {
1432 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1433 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1435 if ( ++done === 2 ) start();
1439 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1444 jQuery.ajaxSetup({timeout: 1000});
1446 var pass = function() {
1448 if ( passed == 2 ) {
1449 ok( true, 'Check local and global callbacks after timeout' );
1450 jQuery('#main').unbind("ajaxError");
1455 var fail = function(a,b,c) {
1456 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1460 jQuery('#main').ajaxError(pass);
1464 url: url("data/name.php?wait=5"),
1470 jQuery.ajaxSetup({timeout: 0});
1473 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1475 jQuery.ajaxSetup({timeout: 50});
1480 url: url("data/name.php?wait=1"),
1482 ok( false, 'Check for local timeout failed' );
1485 success: function() {
1486 ok( true, 'Check for local timeout' );
1492 jQuery.ajaxSetup({timeout: 0});
1495 test("jQuery.ajax - simple get", function() {
1500 url: url("data/name.php?name=foo"),
1501 success: function(msg){
1502 equals( msg, 'bar', 'Check for GET' );
1508 test("jQuery.ajax - simple post", function() {
1513 url: url("data/name.php"),
1515 success: function(msg){
1516 equals( msg, 'pan', 'Check for POST' );
1522 test("ajaxSetup()", function() {
1526 url: url("data/name.php?name=foo"),
1527 success: function(msg){
1528 equals( msg, 'bar', 'Check for GET' );
1536 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1539 url: "data/name.php?wait=1",
1541 error: function(request, status) {
1542 ok( status != null, "status shouldn't be null in error handler" );
1543 equals( "timeout", status );
1550 test("data option: evaluate function values (#2806)", function() {
1553 url: "data/echoQuery.php",
1559 success: function(result) {
1560 equals( result, "key=value" );
1566 test("data option: empty bodies for non-GET requests", function() {
1569 url: "data/echoData.php",
1572 success: function(result) {
1573 equals( result, "" );
1579 test("jQuery.ajax - If-Modified-Since support", function() {
1584 var url = "data/if_modified_since.php?ts=" + new Date();
1589 success: function(data, status) {
1590 equals(status, "success");
1595 success: function(data, status) {
1596 if ( data === "FAIL" ) {
1597 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1598 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1600 equals(status, "notmodified");
1601 ok(data == null, "response body should be empty")
1606 // Do this because opera simply refuses to implement 304 handling :(
1607 // A feature-driven way of detecting this would be appreciated
1608 // See: http://gist.github.com/599419
1609 ok(jQuery.browser.opera, "error");
1610 ok(jQuery.browser.opera, "error");
1616 equals(false, "error");
1617 // Do this because opera simply refuses to implement 304 handling :(
1618 // A feature-driven way of detecting this would be appreciated
1619 // See: http://gist.github.com/599419
1620 ok(jQuery.browser.opera, "error");
1626 test("jQuery.ajax - Etag support", function() {
1631 var url = "data/etag.php?ts=" + new Date();
1636 success: function(data, status) {
1637 equals(status, "success");
1642 success: function(data, status) {
1643 if ( data === "FAIL" ) {
1644 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1645 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1647 equals(status, "notmodified");
1648 ok(data == null, "response body should be empty")
1653 // Do this because opera simply refuses to implement 304 handling :(
1654 // A feature-driven way of detecting this would be appreciated
1655 // See: http://gist.github.com/599419
1656 ok(jQuery.browser.opera, "error");
1657 ok(jQuery.browser.opera, "error");
1663 // Do this because opera simply refuses to implement 304 handling :(
1664 // A feature-driven way of detecting this would be appreciated
1665 // See: http://gist.github.com/599419
1666 ok(jQuery.browser.opera, "error");
1672 test("jQuery ajax - failing cross-domain", function() {
1681 url: 'http://somewebsitethatdoesnotexist.com',
1682 success: function(){ ok( false , "success" ); },
1683 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1684 complete: function() { if ( ! --i ) start(); }
1688 url: 'http://www.google.com',
1689 success: function(){ ok( false , "success" ); },
1690 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1691 complete: function() { if ( ! --i ) start(); }
1696 test("jQuery ajax - atom+xml", function() {
1701 url: url( 'data/atom+xml.php' ),
1702 success: function(){ ok( true , "success" ); },
1703 error: function(){ ok( false , "error" ); },
1704 complete: function() { start(); }
1709 test("jQuery.ajax - active counter", function() {
1710 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1713 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1714 var success = false;
1716 var xhr = jQuery.ajax({ url: window.location });
1721 ok( success, "document.location did not generate exception" );