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() - contentType" , function() {
333 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
335 success: function( data ) {
336 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
338 complete: function() {
343 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
345 success: function( data ) {
346 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
348 complete: function() {
355 test(".ajax() - hash", function() {
359 url: "data/name.html#foo",
360 beforeSend: function( xhr, settings ) {
361 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
367 url: "data/name.html?abc#foo",
368 beforeSend: function( xhr, settings ) {
369 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
375 url: "data/name.html?abc#foo",
376 data: { "test": 123 },
377 beforeSend: function( xhr, settings ) {
378 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
384 test("jQuery ajax - cross-domain detection", function() {
388 var loc = document.location,
389 otherPort = loc.port === 666 ? 667 : 666,
390 otherProtocol = loc.protocol === "http:" ? "https:" : "http:",
396 url: otherProtocol + "//" + loc.host,
397 beforeSend: function( _ , s ) {
399 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
403 if ( ! protocolFlag ) {
404 ok( ! jQuery.support.cors , "Test different protocols are detected as cross-domain (no transport)" );
409 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
410 beforeSend: function( _ , s ) {
412 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
417 ok( ! jQuery.support.cors , "Test different hostnames are detected as cross-domain (no transport)" );
422 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
423 beforeSend: function( _ , s ) {
425 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
430 ok( ! jQuery.support.cors , "Test different ports are detected as cross-domain (no transport)" );
436 test(".ajax() - 304", function() {
441 url: url("data/notmodified.php"),
442 success: function(){ ok(true, "304 ok"); },
443 // Do this because opera simply refuses to implement 304 handling :(
444 // A feature-driven way of detecting this would be appreciated
445 // See: http://gist.github.com/599419
446 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
447 complete: function(xhr){ start(); }
451 test(".load()) - 404 error callbacks", function() {
455 jQuery('#foo').ajaxStart(function(){
456 ok( true, "ajaxStart" );
457 }).ajaxStop(function(){
458 ok( true, "ajaxStop" );
460 }).ajaxSend(function(){
461 ok( true, "ajaxSend" );
462 }).ajaxComplete(function(){
463 ok( true, "ajaxComplete" );
464 }).ajaxError(function(){
465 ok( true, "ajaxError" );
466 }).ajaxSuccess(function(){
467 ok( false, "ajaxSuccess" );
470 jQuery("<div/>").load("data/404.html", function(){
471 ok(true, "complete");
475 test("jQuery.ajax() - abort", function() {
479 jQuery('#foo').ajaxStart(function(){
480 ok( true, "ajaxStart" );
481 }).ajaxStop(function(){
482 ok( true, "ajaxStop" );
484 }).ajaxSend(function(){
485 ok( true, "ajaxSend" );
486 }).ajaxComplete(function(){
487 ok( true, "ajaxComplete" );
490 var xhr = jQuery.ajax({
491 url: url("data/name.php?wait=5"),
492 beforeSend: function(){ ok(true, "beforeSend"); },
493 complete: function(){ ok(true, "complete"); }
496 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
499 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
502 test("Ajax events with context", function() {
506 var context = document.createElement("div");
509 equals( this, context, e.type );
512 function callback(msg){
514 equals( this, context, "context is preserved on callback " + msg );
518 function nocallback(msg){
520 equals( typeof this.url, "string", "context is settings on callback " + msg );
524 jQuery('#foo').add(context)
531 url: url("data/name.html"),
532 beforeSend: callback("beforeSend"),
533 success: callback("success"),
534 error: callback("error"),
536 callback("complete").call(this);
539 url: url("data/404.html"),
541 beforeSend: callback("beforeSend"),
542 error: callback("error"),
543 complete: function(){
544 callback("complete").call(this);
546 jQuery('#foo').add(context).unbind();
549 url: url("data/404.html"),
550 beforeSend: nocallback("beforeSend"),
551 error: nocallback("error"),
552 complete: function(){
553 nocallback("complete").call(this);
564 test("jQuery.ajax context modification", function() {
572 url: url("data/name.html"),
574 beforeSend: function(){
577 complete: function() {
582 equals( obj.test, "foo", "Make sure the original object is maintained." );
585 test("jQuery.ajax() - disabled globals", function() {
589 jQuery('#foo').ajaxStart(function(){
590 ok( false, "ajaxStart" );
591 }).ajaxStop(function(){
592 ok( false, "ajaxStop" );
593 }).ajaxSend(function(){
594 ok( false, "ajaxSend" );
595 }).ajaxComplete(function(){
596 ok( false, "ajaxComplete" );
597 }).ajaxError(function(){
598 ok( false, "ajaxError" );
599 }).ajaxSuccess(function(){
600 ok( false, "ajaxSuccess" );
605 url: url("data/name.html"),
606 beforeSend: function(){ ok(true, "beforeSend"); },
607 success: function(){ ok(true, "success"); },
608 error: function(){ ok(false, "error"); },
609 complete: function(){
610 ok(true, "complete");
611 setTimeout(function(){ start(); }, 13);
616 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
620 url: url("data/with_fries.xml"),
622 success: function(resp) {
623 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
624 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
625 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
631 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
635 url: url("data/with_fries_over_jsonp.php"),
636 dataType: "jsonp xml",
637 success: function(resp) {
638 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
639 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
640 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
643 error: function(_1,_2,error) {
650 test("jQuery.ajax - HEAD requests", function() {
655 url: url("data/name.html"),
657 success: function(data, status, xhr){
658 var h = xhr.getAllResponseHeaders();
659 ok( /Date/i.test(h), 'No Date in HEAD response' );
662 url: url("data/name.html"),
663 data: { whip_it: "good" },
665 success: function(data, status, xhr){
666 var h = xhr.getAllResponseHeaders();
667 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
676 test("jQuery.ajax - beforeSend", function() {
682 jQuery.ajaxSetup({ timeout: 0 });
685 url: url("data/name.html"),
686 beforeSend: function(xml) {
689 success: function(data) {
690 ok( check, "check beforeSend was executed" );
696 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
698 var request = jQuery.ajax({
699 url: url("data/name.html"),
700 beforeSend: function() {
701 ok( true, "beforeSend got called, canceling" );
704 success: function() {
705 ok( false, "request didn't get canceled" );
707 complete: function() {
708 ok( false, "request didn't get canceled" );
711 ok( false, "request didn't get canceled" );
714 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
717 test("jQuery.ajax - beforeSend, cancel request manually", function() {
719 var request = jQuery.ajax({
720 url: url("data/name.html"),
721 beforeSend: function(xhr) {
722 ok( true, "beforeSend got called, canceling" );
725 success: function() {
726 ok( false, "request didn't get canceled" );
728 complete: function() {
729 ok( false, "request didn't get canceled" );
732 ok( false, "request didn't get canceled" );
735 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
738 window.foobar = null;
739 window.testFoo = undefined;
741 test("jQuery.ajax - dataType html", function() {
745 var verifyEvaluation = function() {
746 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
747 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
754 url: url("data/test.html"),
755 success: function(data) {
756 jQuery("#ap").html(data);
757 ok( data.match(/^html text/), 'Check content for datatype html' );
758 setTimeout(verifyEvaluation, 600);
763 test("serialize()", function() {
766 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
767 jQuery("#search").after(
768 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
769 '<input type="number" id="html5number" name="number" value="43" />'
772 equals( jQuery('#form').serialize(),
773 "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",
774 'Check form serialization as query string');
776 equals( jQuery('#form :input').serialize(),
777 "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",
778 'Check input serialization as query string');
780 equals( jQuery('#testForm').serialize(),
781 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
782 'Check form serialization as query string');
784 equals( jQuery('#testForm :input').serialize(),
785 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
786 'Check input serialization as query string');
788 equals( jQuery('#form, #testForm').serialize(),
789 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
790 'Multiple form serialization as query string');
792 /* Temporarily disabled. Opera 10 has problems with form serialization.
793 equals( jQuery('#form, #testForm :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&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
795 'Mixed form/input serialization as query string');
797 jQuery("#html5email, #html5number").remove();
800 test("jQuery.param()", function() {
803 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
805 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
806 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
808 params = {someName: [1, 2, 3], regularThing: "blah" };
809 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
811 params = {foo: ['a', 'b', 'c']};
812 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
814 params = {foo: ["baz", 42, "All your base are belong to us"] };
815 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
817 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
818 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" );
820 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?" };
821 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" );
823 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 ] };
824 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" );
826 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?" };
827 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" );
829 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." );
831 // Make sure empty arrays and objects are handled #6481
832 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
833 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
834 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
836 jQuery.ajaxSetup({ traditional: true });
838 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
839 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
841 params = {someName: [1, 2, 3], regularThing: "blah" };
842 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
844 params = {foo: ['a', 'b', 'c']};
845 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
847 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
848 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
850 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
851 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" );
853 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?" };
854 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" );
856 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 ] };
857 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)" );
859 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?" };
860 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" );
862 params = { param1: null };
863 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
866 test("synchronous request", function() {
868 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
871 test("synchronous request with callbacks", function() {
874 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
875 ok( /^{ "data"/.test( result ), "check returned text" );
878 test("pass-through request object", function() {
882 var target = "data/name.html";
883 var successCount = 0;
886 var success = function() {
889 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
891 errorEx += ": " + xml.status;
893 jQuery("#foo").one('ajaxStop', function () {
894 equals(successCount, 5, "Check all ajax calls successful");
895 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
896 jQuery("#foo").unbind('ajaxError');
901 ok( jQuery.get(url(target), success), "get" );
902 ok( jQuery.post(url(target), success), "post" );
903 ok( jQuery.getScript(url("data/test.js"), success), "script" );
904 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
905 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
908 test("ajax cache", function () {
915 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
916 var re = /_=(.*?)(&|$)/g;
918 for (var i = 0; i < 6; i++) {
919 var ret = re.exec(s.url);
925 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
926 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
931 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
932 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
933 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
934 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
935 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
936 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
941 * The assertions expect that the passed-in object will be modified,
942 * which shouldn't be the case. Fixes #5439.
943 test("global ajaxSettings", function() {
946 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
947 var orig = { url: "data/with_fries.xml" };
950 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
952 t = jQuery.extend({}, orig);
955 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
957 t = jQuery.extend({}, orig);
958 t.data = { zoo: 'a', ping: 'b' };
960 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' }" );
962 jQuery.ajaxSettings = tmp;
966 test("load(String)", function() {
968 stop(); // check if load can be called with only url
969 jQuery('#first').load("data/name.html", start);
972 test("load('url selector')", function() {
974 stop(); // check if load can be called with only url
975 jQuery('#first').load("data/test3.html div.user", function(){
976 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
981 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
984 jQuery.ajaxSetup({ dataType: "json" });
985 jQuery("#first").ajaxComplete(function (e, xml, s) {
986 equals( s.dataType, "html", "Verify the load() dataType was html" );
987 jQuery("#first").unbind("ajaxComplete");
988 jQuery.ajaxSetup({ dataType: "" });
991 jQuery('#first').load("data/test3.html");
994 test("load(String, Function) - simple: inject text into DOM", function() {
997 jQuery('#first').load(url("data/name.html"), function() {
998 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1003 test("load(String, Function) - check scripts", function() {
1007 var verifyEvaluation = function() {
1008 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1009 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1013 jQuery('#first').load(url('data/test.html'), function() {
1014 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1015 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1016 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1017 setTimeout(verifyEvaluation, 600);
1021 test("load(String, Function) - check file with only a script tag", function() {
1025 jQuery('#first').load(url('data/test2.html'), function() {
1026 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1027 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1033 test("load(String, Object, Function)", function() {
1037 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1038 var $post = jQuery(this).find('#post');
1039 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1040 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1045 test("load(String, String, Function)", function() {
1049 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1050 var $get = jQuery(this).find('#get');
1051 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1052 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1057 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1060 jQuery.get(url('data/dashboard.xml'), function(xml) {
1062 jQuery('tab', xml).each(function() {
1063 content.push(jQuery(this).text());
1065 equals( content[0], 'blabla', 'Check first tab');
1066 equals( content[1], 'blublu', 'Check second tab');
1071 test("jQuery.getScript(String, Function) - with callback", function() {
1074 jQuery.getScript(url("data/test.js"), function() {
1075 equals( foobar, "bar", 'Check if script was evaluated' );
1076 setTimeout(start, 100);
1080 test("jQuery.getScript(String, Function) - no callback", function() {
1083 jQuery.getScript(url("data/test.js"), function(){
1088 test("jQuery.ajax() - JSONP, Local", function() {
1092 function plus(){ if ( ++count == 9 ) start(); }
1097 url: "data/jsonp.php",
1099 success: function(data){
1100 ok( data.data, "JSON results returned (GET, no callback)" );
1103 error: function(data){
1104 ok( false, "Ajax error JSON (GET, no callback)" );
1110 url: "data/jsonp.php?callback=?",
1112 success: function(data){
1113 ok( data.data, "JSON results returned (GET, url callback)" );
1116 error: function(data){
1117 ok( false, "Ajax error JSON (GET, url callback)" );
1123 url: "data/jsonp.php",
1126 success: function(data){
1127 ok( data.data, "JSON results returned (GET, data callback)" );
1130 error: function(data){
1131 ok( false, "Ajax error JSON (GET, data callback)" );
1137 url: "data/jsonp.php",
1140 success: function(data){
1141 ok( data.data, "JSON results returned (GET, data obj callback)" );
1144 error: function(data){
1145 ok( false, "Ajax error JSON (GET, data obj callback)" );
1151 url: "data/jsonp.php",
1153 jsonpCallback: "jsonpResults",
1154 success: function(data){
1155 ok( data.data, "JSON results returned (GET, custom callback name)" );
1158 error: function(data){
1159 ok( false, "Ajax error JSON (GET, custom callback name)" );
1166 url: "data/jsonp.php",
1168 success: function(data){
1169 ok( data.data, "JSON results returned (POST, no callback)" );
1172 error: function(data){
1173 ok( false, "Ajax error JSON (GET, data obj callback)" );
1180 url: "data/jsonp.php",
1183 success: function(data){
1184 ok( data.data, "JSON results returned (POST, data callback)" );
1187 error: function(data){
1188 ok( false, "Ajax error JSON (POST, data callback)" );
1195 url: "data/jsonp.php",
1198 success: function(data){
1199 ok( data.data, "JSON results returned (POST, data obj callback)" );
1202 error: function(data){
1203 ok( false, "Ajax error JSON (POST, data obj callback)" );
1210 url: "data/jsonp.php",
1212 beforeSend: function(){
1213 strictEqual( this.cache, false, "cache must be false on JSON request" );
1220 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1224 window.jsonpResults = function(data) {
1225 ok( data.data, "JSON results returned (GET, custom callback function)" );
1226 window.jsonpResults = undefined;
1231 url: "data/jsonp.php",
1233 jsonpCallback: "jsonpResults"
1237 test("jQuery.ajax() - JSONP, Remote", function() {
1241 function plus(){ if ( ++count == 4 ) start(); }
1243 var base = window.location.href.replace(/[^\/]*$/, "");
1248 url: base + "data/jsonp.php",
1250 success: function(data){
1251 ok( data.data, "JSON results returned (GET, no callback)" );
1254 error: function(data){
1255 ok( false, "Ajax error JSON (GET, no callback)" );
1261 url: base + "data/jsonp.php?callback=?",
1263 success: function(data){
1264 ok( data.data, "JSON results returned (GET, url callback)" );
1267 error: function(data){
1268 ok( false, "Ajax error JSON (GET, url callback)" );
1274 url: base + "data/jsonp.php",
1277 success: function(data){
1278 ok( data.data, "JSON results returned (GET, data callback)" );
1281 error: function(data){
1282 ok( false, "Ajax error JSON (GET, data callback)" );
1288 url: base + "data/jsonp.php",
1291 success: function(data){
1292 ok( data.data, "JSON results returned (GET, data obj callback)" );
1295 error: function(data){
1296 ok( false, "Ajax error JSON (GET, data obj callback)" );
1302 test("jQuery.ajax() - script, Remote", function() {
1305 var base = window.location.href.replace(/[^\/]*$/, "");
1310 url: base + "data/test.js",
1312 success: function(data){
1313 ok( foobar, "Script results returned (GET, no callback)" );
1319 test("jQuery.ajax() - script, Remote with POST", function() {
1322 var base = window.location.href.replace(/[^\/]*$/, "");
1327 url: base + "data/test.js",
1330 success: function(data, status){
1331 ok( foobar, "Script results returned (POST, no callback)" );
1332 equals( status, "success", "Script results returned (POST, no callback)" );
1335 error: function(xhr) {
1336 ok( false, "ajax error, status code: " + xhr.status );
1342 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1345 var base = window.location.href.replace(/[^\/]*$/, "");
1346 base = base.replace(/^.*?\/\//, "//");
1351 url: base + "data/test.js",
1353 success: function(data){
1354 ok( foobar, "Script results returned (GET, no callback)" );
1360 test("jQuery.ajax() - malformed JSON", function() {
1366 url: "data/badjson.js",
1368 success: function(){
1369 ok( false, "Success." );
1372 error: function(xhr, msg, detailedMsg) {
1373 equals( "parsererror", msg, "A parse error occurred." );
1374 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1380 test("jQuery.ajax() - script by content-type", function() {
1386 url: "data/script.php",
1387 data: { header: "script" },
1388 success: function() {
1394 test("jQuery.ajax() - json by content-type", function() {
1400 url: "data/json.php",
1401 data: { header: "json", json: "array" },
1402 success: function( json ) {
1403 ok( json.length >= 2, "Check length");
1404 equals( json[0].name, 'John', 'Check JSON: first, name' );
1405 equals( json[0].age, 21, 'Check JSON: first, age' );
1406 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1407 equals( json[1].age, 25, 'Check JSON: second, age' );
1413 test("jQuery.ajax() - json by content-type disabled with options", function() {
1419 url: url("data/json.php"),
1420 data: { header: "json", json: "array" },
1424 success: function( text ) {
1425 equals( typeof text , "string" , "json wasn't auto-determined" );
1426 var json = jQuery.parseJSON( text );
1427 ok( json.length >= 2, "Check length");
1428 equals( json[0].name, 'John', 'Check JSON: first, name' );
1429 equals( json[0].age, 21, 'Check JSON: first, age' );
1430 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1431 equals( json[1].age, 25, 'Check JSON: second, age' );
1437 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1440 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1441 ok( json.length >= 2, "Check length");
1442 equals( json[0].name, 'John', 'Check JSON: first, name' );
1443 equals( json[0].age, 21, 'Check JSON: first, age' );
1444 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1445 equals( json[1].age, 25, 'Check JSON: second, age' );
1450 test("jQuery.getJSON(String, Function) - JSON object", function() {
1453 jQuery.getJSON(url("data/json.php"), function(json) {
1454 if (json && json.data) {
1455 equals( json.data.lang, 'en', 'Check JSON: lang' );
1456 equals( json.data.length, 25, 'Check JSON: length' );
1462 test("jQuery.getJSON - Using Native JSON", function() {
1465 var old = window.JSON;
1467 parse: function(str){
1468 ok( true, "Verifying that parse method was run" );
1474 jQuery.getJSON(url("data/json.php"), function(json) {
1476 equals( json, true, "Verifying return value" );
1481 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1484 var base = window.location.href.replace(/[^\/]*$/, "");
1487 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1488 equals( json.data.lang, 'en', 'Check JSON: lang' );
1489 equals( json.data.length, 25, 'Check JSON: length' );
1494 test("jQuery.post - data", function() {
1498 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1499 jQuery('math', xml).each(function() {
1500 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1501 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1507 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1512 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1513 jQuery('math', xml).each(function() {
1514 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1515 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1517 if ( ++done === 2 ) start();
1520 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1521 jQuery('math', xml).each(function() {
1522 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1523 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1525 if ( ++done === 2 ) start();
1529 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1534 jQuery.ajaxSetup({timeout: 1000});
1536 var pass = function() {
1538 if ( passed == 2 ) {
1539 ok( true, 'Check local and global callbacks after timeout' );
1540 jQuery('#main').unbind("ajaxError");
1545 var fail = function(a,b,c) {
1546 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1550 jQuery('#main').ajaxError(pass);
1554 url: url("data/name.php?wait=5"),
1560 jQuery.ajaxSetup({timeout: 0});
1563 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1565 jQuery.ajaxSetup({timeout: 50});
1570 url: url("data/name.php?wait=1"),
1572 ok( false, 'Check for local timeout failed' );
1575 success: function() {
1576 ok( true, 'Check for local timeout' );
1582 jQuery.ajaxSetup({timeout: 0});
1585 test("jQuery.ajax - simple get", function() {
1590 url: url("data/name.php?name=foo"),
1591 success: function(msg){
1592 equals( msg, 'bar', 'Check for GET' );
1598 test("jQuery.ajax - simple post", function() {
1603 url: url("data/name.php"),
1605 success: function(msg){
1606 equals( msg, 'pan', 'Check for POST' );
1612 test("ajaxSetup()", function() {
1616 url: url("data/name.php?name=foo"),
1617 success: function(msg){
1618 equals( msg, 'bar', 'Check for GET' );
1626 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1629 url: "data/name.php?wait=1",
1631 error: function(request, status) {
1632 ok( status != null, "status shouldn't be null in error handler" );
1633 equals( "timeout", status );
1640 test("data option: evaluate function values (#2806)", function() {
1643 url: "data/echoQuery.php",
1649 success: function(result) {
1650 equals( result, "key=value" );
1656 test("data option: empty bodies for non-GET requests", function() {
1659 url: "data/echoData.php",
1662 success: function(result) {
1663 equals( result, "" );
1669 test("jQuery.ajax - If-Modified-Since support", function() {
1674 var url = "data/if_modified_since.php?ts=" + new Date();
1679 success: function(data, status) {
1680 equals(status, "success");
1685 success: function(data, status) {
1686 if ( data === "FAIL" ) {
1687 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1688 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1690 equals(status, "notmodified");
1691 ok(data == null, "response body should be empty");
1696 // Do this because opera simply refuses to implement 304 handling :(
1697 // A feature-driven way of detecting this would be appreciated
1698 // See: http://gist.github.com/599419
1699 ok(jQuery.browser.opera, "error");
1700 ok(jQuery.browser.opera, "error");
1706 equals(false, "error");
1707 // Do this because opera simply refuses to implement 304 handling :(
1708 // A feature-driven way of detecting this would be appreciated
1709 // See: http://gist.github.com/599419
1710 ok(jQuery.browser.opera, "error");
1716 test("jQuery.ajax - Etag support", function() {
1721 var url = "data/etag.php?ts=" + new Date();
1726 success: function(data, status) {
1727 equals(status, "success");
1732 success: function(data, status) {
1733 if ( data === "FAIL" ) {
1734 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1735 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1737 equals(status, "notmodified");
1738 ok(data == null, "response body should be empty");
1743 // Do this because opera simply refuses to implement 304 handling :(
1744 // A feature-driven way of detecting this would be appreciated
1745 // See: http://gist.github.com/599419
1746 ok(jQuery.browser.opera, "error");
1747 ok(jQuery.browser.opera, "error");
1753 // Do this because opera simply refuses to implement 304 handling :(
1754 // A feature-driven way of detecting this would be appreciated
1755 // See: http://gist.github.com/599419
1756 ok(jQuery.browser.opera, "error");
1762 test("jQuery ajax - failing cross-domain", function() {
1771 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1772 success: function(){ ok( false , "success" ); },
1773 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1774 complete: function() { if ( ! --i ) start(); }
1776 ok( true , "no transport" );
1777 if ( ! --i ) start();
1781 url: 'http://www.google.com',
1782 success: function(){ ok( false , "success" ); },
1783 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1784 complete: function() { if ( ! --i ) start(); }
1786 ok( true , "no transport" );
1787 if ( ! --i ) start();
1792 test("jQuery ajax - atom+xml", function() {
1797 url: url( 'data/atom+xml.php' ),
1798 success: function(){ ok( true , "success" ); },
1799 error: function(){ ok( false , "error" ); },
1800 complete: function() { start(); }
1805 test("jQuery.ajax - active counter", function() {
1806 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1809 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1810 var success = false;
1812 var xhr = jQuery.ajax({ url: window.location });
1817 ok( success, "document.location did not generate exception" );