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 callbacks (late binding)", function() {
76 jQuery.ajaxSetup({ timeout: 0 });
80 setTimeout(function(){
81 jQuery('#foo').ajaxStart(function(){
82 ok( true, "ajaxStart" );
83 }).ajaxStop(function(){
84 ok( true, "ajaxStop" );
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" );
97 url: url("data/name.html"),
98 beforeSend: function(){ ok(true, "beforeSend"); }
100 .complete(function(){ ok(true, "complete"); })
101 .success(function(){ ok(true, "success"); })
102 .error(function(){ ok(false, "error"); });
106 test("jQuery.ajax() - success callbacks (oncomplete binding)", function() {
109 jQuery.ajaxSetup({ timeout: 0 });
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" );
129 url: url("data/name.html"),
130 beforeSend: function(){ ok(true, "beforeSend"); },
131 complete: function(xhr) {
133 .complete(function(){ ok(true, "complete"); })
134 .success(function(){ ok(true, "success"); })
135 .error(function(){ ok(false, "error"); })
136 .complete(function(){ start(); });
142 test("jQuery.ajax() - success callbacks (very late binding)", function() {
145 jQuery.ajaxSetup({ timeout: 0 });
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" );
165 url: url("data/name.html"),
166 beforeSend: function(){ ok(true, "beforeSend"); },
167 complete: function(xhr) {
168 setTimeout (function() {
170 .complete(function(){ ok(true, "complete"); })
171 .success(function(){ ok(true, "success"); })
172 .error(function(){ ok(false, "error"); })
173 .complete(function(){ start(); });
180 test("jQuery.ajax() - success callbacks (order)", function() {
183 jQuery.ajaxSetup({ timeout: 0 });
189 setTimeout(function(){
191 url: url("data/name.html"),
192 success: function( _1 , _2 , xhr ) {
193 xhr.success(function() {
194 xhr.success(function() {
201 complete: function() {
202 strictEqual(testString, "ABCDE", "Proper order");
205 }).success(function() {
207 }).success(function() {
213 test("jQuery.ajax() - error callbacks", function() {
217 jQuery('#foo').ajaxStart(function(){
218 ok( true, "ajaxStart" );
219 }).ajaxStop(function(){
220 ok( true, "ajaxStop" );
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" );
232 jQuery.ajaxSetup({ timeout: 500 });
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"); }
243 test("jQuery.ajax() - responseText on error", function() {
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" );
254 complete: function() {
260 test(".ajax() - retry with jQuery.ajax( this )", function() {
269 url: url("data/errorWithText.php"),
275 ok( true , "Test retrying with jQuery.ajax(this) works" );
283 test(".ajax() - headers" , function() {
289 var requestHeaders = {
291 "SometHing-elsE": "other value",
292 OthEr: "something else"
297 for( i in requestHeaders ) {
301 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
302 headers: requestHeaders,
303 success: function( data , _ , xhr ) {
305 for ( i in requestHeaders ) {
306 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
308 tmp = tmp.join( "" );
310 equals( data , tmp , "Headers were sent" );
311 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
314 error: function(){ ok(false, "error"); }
319 test(".ajax() - Accept header" , function() {
325 jQuery.ajax(url("data/headers.php?keys=accept"), {
327 Accept: "very wrong accept value"
329 beforeSend: function( xhr ) {
330 xhr.setRequestHeader( "Accept", "*/*" );
332 success: function( data ) {
333 strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
336 error: function(){ ok(false, "error"); }
341 test(".ajax() - contentType" , function() {
355 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
357 success: function( data ) {
358 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
360 complete: function() {
365 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
367 success: function( data ) {
368 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
370 complete: function() {
377 test(".ajax() - hash", function() {
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.");
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.");
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.");
406 test("jQuery ajax - cross-domain detection", function() {
410 var loc = document.location,
411 otherPort = loc.port === 666 ? 667 : 666,
412 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
416 url: otherProtocol + "//" + loc.host,
417 beforeSend: function( _ , s ) {
418 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
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" );
434 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
435 beforeSend: function( _ , s ) {
436 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
443 url: loc.protocol + "//" + loc.host,
445 beforeSend: function( _ , s ) {
446 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
453 test(".ajax() - 304", function() {
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(); }
468 test(".load()) - 404 error callbacks", function() {
472 jQuery('#foo').ajaxStart(function(){
473 ok( true, "ajaxStart" );
474 }).ajaxStop(function(){
475 ok( true, "ajaxStop" );
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" );
487 jQuery("<div/>").load("data/404.html", function(){
488 ok(true, "complete");
492 test("jQuery.ajax() - abort", function() {
496 jQuery('#foo').ajaxStart(function(){
497 ok( true, "ajaxStart" );
498 }).ajaxStop(function(){
499 ok( true, "ajaxStop" );
501 }).ajaxSend(function(){
502 ok( true, "ajaxSend" );
503 }).ajaxComplete(function(){
504 ok( true, "ajaxComplete" );
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"); }
513 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
516 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
519 test("Ajax events with context", function() {
523 var context = document.createElement("div");
526 equals( this, context, e.type );
529 function callback(msg){
531 equals( this, context, "context is preserved on callback " + msg );
535 function nocallback(msg){
537 equals( typeof this.url, "string", "context is settings on callback " + msg );
541 jQuery('#foo').add(context)
548 url: url("data/name.html"),
549 beforeSend: callback("beforeSend"),
550 success: callback("success"),
551 error: callback("error"),
553 callback("complete").call(this);
556 url: url("data/404.html"),
558 beforeSend: callback("beforeSend"),
559 error: callback("error"),
560 complete: function(){
561 callback("complete").call(this);
563 jQuery('#foo').add(context).unbind();
566 url: url("data/404.html"),
567 beforeSend: nocallback("beforeSend"),
568 error: nocallback("error"),
569 complete: function(){
570 nocallback("complete").call(this);
581 test("jQuery.ajax context modification", function() {
589 url: url("data/name.html"),
591 beforeSend: function(){
594 complete: function() {
599 equals( obj.test, "foo", "Make sure the original object is maintained." );
602 test("jQuery.ajax() - disabled globals", function() {
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" );
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);
633 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
637 url: url("data/with_fries.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' );
648 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
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' );
660 error: function(_1,_2,error) {
667 test("jQuery.ajax - HEAD requests", function() {
672 url: url("data/name.html"),
674 success: function(data, status, xhr){
675 var h = xhr.getAllResponseHeaders();
676 ok( /Date/i.test(h), 'No Date in HEAD response' );
679 url: url("data/name.html"),
680 data: { whip_it: "good" },
682 success: function(data, status, xhr){
683 var h = xhr.getAllResponseHeaders();
684 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
693 test("jQuery.ajax - beforeSend", function() {
699 jQuery.ajaxSetup({ timeout: 0 });
702 url: url("data/name.html"),
703 beforeSend: function(xml) {
706 success: function(data) {
707 ok( check, "check beforeSend was executed" );
713 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
715 var request = jQuery.ajax({
716 url: url("data/name.html"),
717 beforeSend: function() {
718 ok( true, "beforeSend got called, canceling" );
721 success: function() {
722 ok( false, "request didn't get canceled" );
724 complete: function() {
725 ok( false, "request didn't get canceled" );
728 ok( false, "request didn't get canceled" );
731 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
734 test("jQuery.ajax - beforeSend, cancel request manually", function() {
736 var request = jQuery.ajax({
737 url: url("data/name.html"),
738 beforeSend: function(xhr) {
739 ok( true, "beforeSend got called, canceling" );
742 success: function() {
743 ok( false, "request didn't get canceled" );
745 complete: function() {
746 ok( false, "request didn't get canceled" );
749 ok( false, "request didn't get canceled" );
752 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
755 window.foobar = null;
756 window.testFoo = undefined;
758 test("jQuery.ajax - dataType html", function() {
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' );
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);
780 test("serialize()", function() {
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" />'
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');
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');
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');
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');
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');
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');
814 jQuery("#html5email, #html5number").remove();
817 test("jQuery.param()", function() {
820 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
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" );
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®ularThing=blah", "with array" );
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" );
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" );
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" );
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" );
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" );
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" );
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." );
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" );
853 jQuery.ajaxSetup({ traditional: true });
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" );
858 params = {someName: [1, 2, 3], regularThing: "blah" };
859 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
861 params = {foo: ['a', 'b', 'c']};
862 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
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" );
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" );
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" );
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)" );
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" );
879 params = { param1: null };
880 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
883 test("synchronous request", function() {
885 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
888 test("synchronous request with callbacks", function() {
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" );
895 test("pass-through request object", function() {
899 var target = "data/name.html";
900 var successCount = 0;
903 var success = function() {
906 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
908 errorEx += ": " + xml.status;
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');
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" );
925 test("ajax cache", function () {
932 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
933 var re = /_=(.*?)(&|$)/g;
935 for (var i = 0; i < 6; i++) {
936 var ret = re.exec(s.url);
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");
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" );
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() {
963 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
964 var orig = { url: "data/with_fries.xml" };
967 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
969 t = jQuery.extend({}, orig);
972 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
974 t = jQuery.extend({}, orig);
975 t.data = { zoo: 'a', ping: 'b' };
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' }" );
979 jQuery.ajaxSettings = tmp;
983 test("load(String)", function() {
985 stop(); // check if load can be called with only url
986 jQuery('#first').load("data/name.html", start);
989 test("load('url selector')", function() {
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" );
998 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
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: "" });
1008 jQuery('#first').load("data/test3.html");
1011 test("load(String, Function) - simple: inject text into DOM", function() {
1014 jQuery('#first').load(url("data/name.html"), function() {
1015 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1020 test("load(String, Function) - check scripts", function() {
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');
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);
1038 test("load(String, Function) - check file with only a script tag", function() {
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' );
1050 test("load(String, Function) - dataFilter in ajaxSettings", function() {
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 });
1062 test("load(String, Object, Function)", function() {
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');
1074 test("load(String, String, Function)", function() {
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');
1086 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1089 jQuery.get(url('data/dashboard.xml'), function(xml) {
1091 jQuery('tab', xml).each(function() {
1092 content.push(jQuery(this).text());
1094 equals( content[0], 'blabla', 'Check first tab');
1095 equals( content[1], 'blublu', 'Check second tab');
1100 test("jQuery.getScript(String, Function) - with callback", function() {
1103 jQuery.getScript(url("data/test.js"), function() {
1104 equals( foobar, "bar", 'Check if script was evaluated' );
1105 setTimeout(start, 100);
1109 test("jQuery.getScript(String, Function) - no callback", function() {
1112 jQuery.getScript(url("data/test.js"), function(){
1117 test("jQuery.ajax() - JSONP, Local", function() {
1121 function plus(){ if ( ++count == 14 ) start(); }
1126 url: "data/jsonp.php",
1128 success: function(data){
1129 ok( data.data, "JSON results returned (GET, no callback)" );
1132 error: function(data){
1133 ok( false, "Ajax error JSON (GET, no callback)" );
1139 url: "data/jsonp.php?callback=?",
1141 success: function(data){
1142 ok( data.data, "JSON results returned (GET, url callback)" );
1145 error: function(data){
1146 ok( false, "Ajax error JSON (GET, url callback)" );
1152 url: "data/jsonp.php",
1155 success: function(data){
1156 ok( data.data, "JSON results returned (GET, data callback)" );
1159 error: function(data){
1160 ok( false, "Ajax error JSON (GET, data callback)" );
1166 url: "data/jsonp.php?callback=??",
1168 success: function(data){
1169 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1172 error: function(data){
1173 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1179 url: "data/jsonp.php",
1181 data: "callback=??",
1182 success: function(data){
1183 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1186 error: function(data){
1187 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1193 url: "data/jsonp.php/??",
1195 success: function(data){
1196 ok( data.data, "JSON results returned (GET, REST-like)" );
1199 error: function(data){
1200 ok( false, "Ajax error JSON (GET, REST-like)" );
1206 url: "data/jsonp.php/???json=1",
1208 success: function(data){
1209 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1212 error: function(data){
1213 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1219 url: "data/jsonp.php",
1224 success: function(data){
1225 ok( data.data, "JSON results returned (GET, processed data callback)" );
1228 error: function(data){
1229 ok( false, "Ajax error JSON (GET, processed data callback)" );
1235 url: "data/jsonp.php",
1238 success: function(data){
1239 ok( data.data, "JSON results returned (GET, data obj callback)" );
1242 error: function(data){
1243 ok( false, "Ajax error JSON (GET, data obj callback)" );
1249 url: "data/jsonp.php",
1251 jsonpCallback: "jsonpResults",
1252 success: function(data){
1253 ok( data.data, "JSON results returned (GET, custom callback name)" );
1256 error: function(data){
1257 ok( false, "Ajax error JSON (GET, custom callback name)" );
1264 url: "data/jsonp.php",
1266 success: function(data){
1267 ok( data.data, "JSON results returned (POST, no callback)" );
1270 error: function(data){
1271 ok( false, "Ajax error JSON (GET, data obj callback)" );
1278 url: "data/jsonp.php",
1281 success: function(data){
1282 ok( data.data, "JSON results returned (POST, data callback)" );
1285 error: function(data){
1286 ok( false, "Ajax error JSON (POST, data callback)" );
1293 url: "data/jsonp.php",
1296 success: function(data){
1297 ok( data.data, "JSON results returned (POST, data obj callback)" );
1300 error: function(data){
1301 ok( false, "Ajax error JSON (POST, data obj callback)" );
1308 url: "data/jsonp.php",
1310 beforeSend: function(){
1311 strictEqual( this.cache, false, "cache must be false on JSON request" );
1318 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1322 window.jsonpResults = function(data) {
1323 ok( data.data, "JSON results returned (GET, custom callback function)" );
1324 window.jsonpResults = undefined;
1329 url: "data/jsonp.php",
1331 jsonpCallback: "jsonpResults"
1335 test("jQuery.ajax() - JSONP, Remote", function() {
1339 function plus(){ if ( ++count == 4 ) start(); }
1341 var base = window.location.href.replace(/[^\/]*$/, "");
1346 url: base + "data/jsonp.php",
1348 success: function(data){
1349 ok( data.data, "JSON results returned (GET, no callback)" );
1352 error: function(data){
1353 ok( false, "Ajax error JSON (GET, no callback)" );
1359 url: base + "data/jsonp.php?callback=?",
1361 success: function(data){
1362 ok( data.data, "JSON results returned (GET, url callback)" );
1365 error: function(data){
1366 ok( false, "Ajax error JSON (GET, url callback)" );
1372 url: base + "data/jsonp.php",
1375 success: function(data){
1376 ok( data.data, "JSON results returned (GET, data callback)" );
1379 error: function(data){
1380 ok( false, "Ajax error JSON (GET, data callback)" );
1386 url: base + "data/jsonp.php",
1389 success: function(data){
1390 ok( data.data, "JSON results returned (GET, data obj callback)" );
1393 error: function(data){
1394 ok( false, "Ajax error JSON (GET, data obj callback)" );
1400 test("jQuery.ajax() - script, Remote", function() {
1403 var base = window.location.href.replace(/[^\/]*$/, "");
1408 url: base + "data/test.js",
1410 success: function(data){
1411 ok( foobar, "Script results returned (GET, no callback)" );
1417 test("jQuery.ajax() - script, Remote with POST", function() {
1420 var base = window.location.href.replace(/[^\/]*$/, "");
1425 url: base + "data/test.js",
1428 success: function(data, status){
1429 ok( foobar, "Script results returned (POST, no callback)" );
1430 equals( status, "success", "Script results returned (POST, no callback)" );
1433 error: function(xhr) {
1434 ok( false, "ajax error, status code: " + xhr.status );
1440 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1443 var base = window.location.href.replace(/[^\/]*$/, "");
1444 base = base.replace(/^.*?\/\//, "//");
1449 url: base + "data/test.js",
1451 success: function(data){
1452 ok( foobar, "Script results returned (GET, no callback)" );
1458 test("jQuery.ajax() - malformed JSON", function() {
1464 url: "data/badjson.js",
1466 success: function(){
1467 ok( false, "Success." );
1470 error: function(xhr, msg, detailedMsg) {
1471 equals( "parsererror", msg, "A parse error occurred." );
1472 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1478 test("jQuery.ajax() - script by content-type", function() {
1484 url: "data/script.php",
1485 data: { header: "script" },
1486 success: function() {
1492 test("jQuery.ajax() - json by content-type", function() {
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' );
1511 test("jQuery.ajax() - json by content-type disabled with options", function() {
1517 url: url("data/json.php"),
1518 data: { header: "json", json: "array" },
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' );
1535 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
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' );
1548 test("jQuery.getJSON(String, Function) - JSON object", function() {
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' );
1560 test("jQuery.getJSON - Using Native JSON", function() {
1563 var old = window.JSON;
1565 parse: function(str){
1566 ok( true, "Verifying that parse method was run" );
1572 jQuery.getJSON(url("data/json.php"), function(json) {
1574 equals( json, true, "Verifying return value" );
1579 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1582 var base = window.location.href.replace(/[^\/]*$/, "");
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' );
1592 test("jQuery.post - data", function() {
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' );
1605 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
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' );
1615 if ( ++done === 2 ) start();
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' );
1623 if ( ++done === 2 ) start();
1627 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1632 jQuery.ajaxSetup({timeout: 1000});
1634 var pass = function() {
1636 if ( passed == 2 ) {
1637 ok( true, 'Check local and global callbacks after timeout' );
1638 jQuery('#main').unbind("ajaxError");
1643 var fail = function(a,b,c) {
1644 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1648 jQuery('#main').ajaxError(pass);
1652 url: url("data/name.php?wait=5"),
1658 jQuery.ajaxSetup({timeout: 0});
1661 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1663 jQuery.ajaxSetup({timeout: 50});
1668 url: url("data/name.php?wait=1"),
1670 ok( false, 'Check for local timeout failed' );
1673 success: function() {
1674 ok( true, 'Check for local timeout' );
1680 jQuery.ajaxSetup({timeout: 0});
1683 test("jQuery.ajax - simple get", function() {
1688 url: url("data/name.php?name=foo"),
1689 success: function(msg){
1690 equals( msg, 'bar', 'Check for GET' );
1696 test("jQuery.ajax - simple post", function() {
1701 url: url("data/name.php"),
1703 success: function(msg){
1704 equals( msg, 'pan', 'Check for POST' );
1710 test("ajaxSetup()", function() {
1714 url: url("data/name.php?name=foo"),
1715 success: function(msg){
1716 equals( msg, 'bar', 'Check for GET' );
1724 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1727 url: "data/name.php?wait=1",
1729 error: function(request, status) {
1730 ok( status != null, "status shouldn't be null in error handler" );
1731 equals( "timeout", status );
1738 test("data option: evaluate function values (#2806)", function() {
1741 url: "data/echoQuery.php",
1747 success: function(result) {
1748 equals( result, "key=value" );
1754 test("data option: empty bodies for non-GET requests", function() {
1757 url: "data/echoData.php",
1760 success: function(result) {
1761 equals( result, "" );
1767 test("jQuery.ajax - If-Modified-Since support", function() {
1772 var url = "data/if_modified_since.php?ts=" + new Date();
1777 success: function(data, status) {
1778 equals(status, "success");
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').");
1788 equals(status, "notmodified");
1789 ok(data == null, "response body should be empty");
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");
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");
1814 test("jQuery.ajax - Etag support", function() {
1819 var url = "data/etag.php?ts=" + new Date();
1824 success: function(data, status) {
1825 equals(status, "success");
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').");
1835 equals(status, "notmodified");
1836 ok(data == null, "response body should be empty");
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");
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");
1860 test("jQuery ajax - failing cross-domain", function() {
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(); }
1874 ok( true , "no transport" );
1875 if ( ! --i ) start();
1879 url: 'http://www.google.com',
1880 success: function(){ ok( false , "success" ); },
1881 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1882 complete: function() { if ( ! --i ) start(); }
1884 ok( true , "no transport" );
1885 if ( ! --i ) start();
1890 test("jQuery ajax - atom+xml", function() {
1895 url: url( 'data/atom+xml.php' ),
1896 success: function(){ ok( true , "success" ); },
1897 error: function(){ ok( false , "error" ); },
1898 complete: function() { start(); }
1903 test("jQuery.ajax - active counter", function() {
1904 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1907 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1908 var success = false;
1910 var xhr = jQuery.ajax({ url: window.location });
1915 ok( success, "document.location did not generate exception" );