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:";
394 url: otherProtocol + "//" + loc.host,
395 beforeSend: function( _ , s ) {
396 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
403 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
404 beforeSend: function( _ , s ) {
405 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
412 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
413 beforeSend: function( _ , s ) {
414 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
421 url: loc.protocol + "//" + loc.host,
423 beforeSend: function( _ , s ) {
424 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
431 test(".ajax() - 304", function() {
436 url: url("data/notmodified.php"),
437 success: function(){ ok(true, "304 ok"); },
438 // Do this because opera simply refuses to implement 304 handling :(
439 // A feature-driven way of detecting this would be appreciated
440 // See: http://gist.github.com/599419
441 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
442 complete: function(xhr){ start(); }
446 test(".load()) - 404 error callbacks", function() {
450 jQuery('#foo').ajaxStart(function(){
451 ok( true, "ajaxStart" );
452 }).ajaxStop(function(){
453 ok( true, "ajaxStop" );
455 }).ajaxSend(function(){
456 ok( true, "ajaxSend" );
457 }).ajaxComplete(function(){
458 ok( true, "ajaxComplete" );
459 }).ajaxError(function(){
460 ok( true, "ajaxError" );
461 }).ajaxSuccess(function(){
462 ok( false, "ajaxSuccess" );
465 jQuery("<div/>").load("data/404.html", function(){
466 ok(true, "complete");
470 test("jQuery.ajax() - abort", function() {
474 jQuery('#foo').ajaxStart(function(){
475 ok( true, "ajaxStart" );
476 }).ajaxStop(function(){
477 ok( true, "ajaxStop" );
479 }).ajaxSend(function(){
480 ok( true, "ajaxSend" );
481 }).ajaxComplete(function(){
482 ok( true, "ajaxComplete" );
485 var xhr = jQuery.ajax({
486 url: url("data/name.php?wait=5"),
487 beforeSend: function(){ ok(true, "beforeSend"); },
488 complete: function(){ ok(true, "complete"); }
491 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
494 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
497 test("Ajax events with context", function() {
501 var context = document.createElement("div");
504 equals( this, context, e.type );
507 function callback(msg){
509 equals( this, context, "context is preserved on callback " + msg );
513 function nocallback(msg){
515 equals( typeof this.url, "string", "context is settings on callback " + msg );
519 jQuery('#foo').add(context)
526 url: url("data/name.html"),
527 beforeSend: callback("beforeSend"),
528 success: callback("success"),
529 error: callback("error"),
531 callback("complete").call(this);
534 url: url("data/404.html"),
536 beforeSend: callback("beforeSend"),
537 error: callback("error"),
538 complete: function(){
539 callback("complete").call(this);
541 jQuery('#foo').add(context).unbind();
544 url: url("data/404.html"),
545 beforeSend: nocallback("beforeSend"),
546 error: nocallback("error"),
547 complete: function(){
548 nocallback("complete").call(this);
559 test("jQuery.ajax context modification", function() {
567 url: url("data/name.html"),
569 beforeSend: function(){
572 complete: function() {
577 equals( obj.test, "foo", "Make sure the original object is maintained." );
580 test("jQuery.ajax() - disabled globals", function() {
584 jQuery('#foo').ajaxStart(function(){
585 ok( false, "ajaxStart" );
586 }).ajaxStop(function(){
587 ok( false, "ajaxStop" );
588 }).ajaxSend(function(){
589 ok( false, "ajaxSend" );
590 }).ajaxComplete(function(){
591 ok( false, "ajaxComplete" );
592 }).ajaxError(function(){
593 ok( false, "ajaxError" );
594 }).ajaxSuccess(function(){
595 ok( false, "ajaxSuccess" );
600 url: url("data/name.html"),
601 beforeSend: function(){ ok(true, "beforeSend"); },
602 success: function(){ ok(true, "success"); },
603 error: function(){ ok(false, "error"); },
604 complete: function(){
605 ok(true, "complete");
606 setTimeout(function(){ start(); }, 13);
611 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
615 url: url("data/with_fries.xml"),
617 success: function(resp) {
618 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
619 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
620 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
626 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
630 url: url("data/with_fries_over_jsonp.php"),
631 dataType: "jsonp xml",
632 success: function(resp) {
633 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
634 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
635 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
638 error: function(_1,_2,error) {
645 test("jQuery.ajax - HEAD requests", function() {
650 url: url("data/name.html"),
652 success: function(data, status, xhr){
653 var h = xhr.getAllResponseHeaders();
654 ok( /Date/i.test(h), 'No Date in HEAD response' );
657 url: url("data/name.html"),
658 data: { whip_it: "good" },
660 success: function(data, status, xhr){
661 var h = xhr.getAllResponseHeaders();
662 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
671 test("jQuery.ajax - beforeSend", function() {
677 jQuery.ajaxSetup({ timeout: 0 });
680 url: url("data/name.html"),
681 beforeSend: function(xml) {
684 success: function(data) {
685 ok( check, "check beforeSend was executed" );
691 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
693 var request = jQuery.ajax({
694 url: url("data/name.html"),
695 beforeSend: function() {
696 ok( true, "beforeSend got called, canceling" );
699 success: function() {
700 ok( false, "request didn't get canceled" );
702 complete: function() {
703 ok( false, "request didn't get canceled" );
706 ok( false, "request didn't get canceled" );
709 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
712 test("jQuery.ajax - beforeSend, cancel request manually", function() {
714 var request = jQuery.ajax({
715 url: url("data/name.html"),
716 beforeSend: function(xhr) {
717 ok( true, "beforeSend got called, canceling" );
720 success: function() {
721 ok( false, "request didn't get canceled" );
723 complete: function() {
724 ok( false, "request didn't get canceled" );
727 ok( false, "request didn't get canceled" );
730 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
733 window.foobar = null;
734 window.testFoo = undefined;
736 test("jQuery.ajax - dataType html", function() {
740 var verifyEvaluation = function() {
741 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
742 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
749 url: url("data/test.html"),
750 success: function(data) {
751 jQuery("#ap").html(data);
752 ok( data.match(/^html text/), 'Check content for datatype html' );
753 setTimeout(verifyEvaluation, 600);
758 test("serialize()", function() {
761 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
762 jQuery("#search").after(
763 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
764 '<input type="number" id="html5number" name="number" value="43" />'
767 equals( jQuery('#form').serialize(),
768 "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",
769 'Check form serialization as query string');
771 equals( jQuery('#form :input').serialize(),
772 "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",
773 'Check input serialization as query string');
775 equals( jQuery('#testForm').serialize(),
776 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
777 'Check form serialization as query string');
779 equals( jQuery('#testForm :input').serialize(),
780 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
781 'Check input serialization as query string');
783 equals( jQuery('#form, #testForm').serialize(),
784 "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=",
785 'Multiple form serialization as query string');
787 /* Temporarily disabled. Opera 10 has problems with form serialization.
788 equals( jQuery('#form, #testForm :input').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&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
790 'Mixed form/input serialization as query string');
792 jQuery("#html5email, #html5number").remove();
795 test("jQuery.param()", function() {
798 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
800 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
801 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
803 params = {someName: [1, 2, 3], regularThing: "blah" };
804 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
806 params = {foo: ['a', 'b', 'c']};
807 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
809 params = {foo: ["baz", 42, "All your base are belong to us"] };
810 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
812 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
813 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" );
815 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?" };
816 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" );
818 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 ] };
819 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" );
821 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?" };
822 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" );
824 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." );
826 // Make sure empty arrays and objects are handled #6481
827 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
828 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
829 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
831 jQuery.ajaxSetup({ traditional: true });
833 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
834 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
836 params = {someName: [1, 2, 3], regularThing: "blah" };
837 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
839 params = {foo: ['a', 'b', 'c']};
840 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
842 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
843 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
845 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
846 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" );
848 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?" };
849 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" );
851 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 ] };
852 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)" );
854 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?" };
855 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" );
857 params = { param1: null };
858 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
861 test("synchronous request", function() {
863 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
866 test("synchronous request with callbacks", function() {
869 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
870 ok( /^{ "data"/.test( result ), "check returned text" );
873 test("pass-through request object", function() {
877 var target = "data/name.html";
878 var successCount = 0;
881 var success = function() {
884 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
886 errorEx += ": " + xml.status;
888 jQuery("#foo").one('ajaxStop', function () {
889 equals(successCount, 5, "Check all ajax calls successful");
890 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
891 jQuery("#foo").unbind('ajaxError');
896 ok( jQuery.get(url(target), success), "get" );
897 ok( jQuery.post(url(target), success), "post" );
898 ok( jQuery.getScript(url("data/test.js"), success), "script" );
899 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
900 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
903 test("ajax cache", function () {
910 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
911 var re = /_=(.*?)(&|$)/g;
913 for (var i = 0; i < 6; i++) {
914 var ret = re.exec(s.url);
920 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
921 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
926 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
927 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
928 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
929 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
930 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
931 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
936 * The assertions expect that the passed-in object will be modified,
937 * which shouldn't be the case. Fixes #5439.
938 test("global ajaxSettings", function() {
941 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
942 var orig = { url: "data/with_fries.xml" };
945 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
947 t = jQuery.extend({}, orig);
950 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
952 t = jQuery.extend({}, orig);
953 t.data = { zoo: 'a', ping: 'b' };
955 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' }" );
957 jQuery.ajaxSettings = tmp;
961 test("load(String)", function() {
963 stop(); // check if load can be called with only url
964 jQuery('#first').load("data/name.html", start);
967 test("load('url selector')", function() {
969 stop(); // check if load can be called with only url
970 jQuery('#first').load("data/test3.html div.user", function(){
971 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
976 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
979 jQuery.ajaxSetup({ dataType: "json" });
980 jQuery("#first").ajaxComplete(function (e, xml, s) {
981 equals( s.dataType, "html", "Verify the load() dataType was html" );
982 jQuery("#first").unbind("ajaxComplete");
983 jQuery.ajaxSetup({ dataType: "" });
986 jQuery('#first').load("data/test3.html");
989 test("load(String, Function) - simple: inject text into DOM", function() {
992 jQuery('#first').load(url("data/name.html"), function() {
993 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
998 test("load(String, Function) - check scripts", function() {
1002 var verifyEvaluation = function() {
1003 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1004 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1008 jQuery('#first').load(url('data/test.html'), function() {
1009 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1010 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1011 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1012 setTimeout(verifyEvaluation, 600);
1016 test("load(String, Function) - check file with only a script tag", function() {
1020 jQuery('#first').load(url('data/test2.html'), function() {
1021 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1022 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1028 test("load(String, Object, Function)", function() {
1032 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1033 var $post = jQuery(this).find('#post');
1034 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1035 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1040 test("load(String, String, Function)", function() {
1044 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1045 var $get = jQuery(this).find('#get');
1046 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1047 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1052 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1055 jQuery.get(url('data/dashboard.xml'), function(xml) {
1057 jQuery('tab', xml).each(function() {
1058 content.push(jQuery(this).text());
1060 equals( content[0], 'blabla', 'Check first tab');
1061 equals( content[1], 'blublu', 'Check second tab');
1066 test("jQuery.getScript(String, Function) - with callback", function() {
1069 jQuery.getScript(url("data/test.js"), function() {
1070 equals( foobar, "bar", 'Check if script was evaluated' );
1071 setTimeout(start, 100);
1075 test("jQuery.getScript(String, Function) - no callback", function() {
1078 jQuery.getScript(url("data/test.js"), function(){
1083 test("jQuery.ajax() - JSONP, Local", function() {
1087 function plus(){ if ( ++count == 10 ) start(); }
1092 url: "data/jsonp.php",
1094 success: function(data){
1095 ok( data.data, "JSON results returned (GET, no callback)" );
1098 error: function(data){
1099 ok( false, "Ajax error JSON (GET, no callback)" );
1105 url: "data/jsonp.php?callback=?",
1107 success: function(data){
1108 ok( data.data, "JSON results returned (GET, url callback)" );
1111 error: function(data){
1112 ok( false, "Ajax error JSON (GET, url callback)" );
1118 url: "data/jsonp.php",
1121 success: function(data){
1122 ok( data.data, "JSON results returned (GET, data callback)" );
1125 error: function(data){
1126 ok( false, "Ajax error JSON (GET, data callback)" );
1132 url: "data/jsonp.php",
1137 success: function(data){
1138 ok( data.data, "JSON results returned (GET, processed data callback)" );
1141 error: function(data){
1142 ok( false, "Ajax error JSON (GET, processed data callback)" );
1148 url: "data/jsonp.php",
1151 success: function(data){
1152 ok( data.data, "JSON results returned (GET, data obj callback)" );
1155 error: function(data){
1156 ok( false, "Ajax error JSON (GET, data obj callback)" );
1162 url: "data/jsonp.php",
1164 jsonpCallback: "jsonpResults",
1165 success: function(data){
1166 ok( data.data, "JSON results returned (GET, custom callback name)" );
1169 error: function(data){
1170 ok( false, "Ajax error JSON (GET, custom callback name)" );
1177 url: "data/jsonp.php",
1179 success: function(data){
1180 ok( data.data, "JSON results returned (POST, no callback)" );
1183 error: function(data){
1184 ok( false, "Ajax error JSON (GET, data obj callback)" );
1191 url: "data/jsonp.php",
1194 success: function(data){
1195 ok( data.data, "JSON results returned (POST, data callback)" );
1198 error: function(data){
1199 ok( false, "Ajax error JSON (POST, data callback)" );
1206 url: "data/jsonp.php",
1209 success: function(data){
1210 ok( data.data, "JSON results returned (POST, data obj callback)" );
1213 error: function(data){
1214 ok( false, "Ajax error JSON (POST, data obj callback)" );
1221 url: "data/jsonp.php",
1223 beforeSend: function(){
1224 strictEqual( this.cache, false, "cache must be false on JSON request" );
1231 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1235 window.jsonpResults = function(data) {
1236 ok( data.data, "JSON results returned (GET, custom callback function)" );
1237 window.jsonpResults = undefined;
1242 url: "data/jsonp.php",
1244 jsonpCallback: "jsonpResults"
1248 test("jQuery.ajax() - JSONP, Remote", function() {
1252 function plus(){ if ( ++count == 4 ) start(); }
1254 var base = window.location.href.replace(/[^\/]*$/, "");
1259 url: base + "data/jsonp.php",
1261 success: function(data){
1262 ok( data.data, "JSON results returned (GET, no callback)" );
1265 error: function(data){
1266 ok( false, "Ajax error JSON (GET, no callback)" );
1272 url: base + "data/jsonp.php?callback=?",
1274 success: function(data){
1275 ok( data.data, "JSON results returned (GET, url callback)" );
1278 error: function(data){
1279 ok( false, "Ajax error JSON (GET, url callback)" );
1285 url: base + "data/jsonp.php",
1288 success: function(data){
1289 ok( data.data, "JSON results returned (GET, data callback)" );
1292 error: function(data){
1293 ok( false, "Ajax error JSON (GET, data callback)" );
1299 url: base + "data/jsonp.php",
1302 success: function(data){
1303 ok( data.data, "JSON results returned (GET, data obj callback)" );
1306 error: function(data){
1307 ok( false, "Ajax error JSON (GET, data obj callback)" );
1313 test("jQuery.ajax() - script, Remote", function() {
1316 var base = window.location.href.replace(/[^\/]*$/, "");
1321 url: base + "data/test.js",
1323 success: function(data){
1324 ok( foobar, "Script results returned (GET, no callback)" );
1330 test("jQuery.ajax() - script, Remote with POST", function() {
1333 var base = window.location.href.replace(/[^\/]*$/, "");
1338 url: base + "data/test.js",
1341 success: function(data, status){
1342 ok( foobar, "Script results returned (POST, no callback)" );
1343 equals( status, "success", "Script results returned (POST, no callback)" );
1346 error: function(xhr) {
1347 ok( false, "ajax error, status code: " + xhr.status );
1353 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1356 var base = window.location.href.replace(/[^\/]*$/, "");
1357 base = base.replace(/^.*?\/\//, "//");
1362 url: base + "data/test.js",
1364 success: function(data){
1365 ok( foobar, "Script results returned (GET, no callback)" );
1371 test("jQuery.ajax() - malformed JSON", function() {
1377 url: "data/badjson.js",
1379 success: function(){
1380 ok( false, "Success." );
1383 error: function(xhr, msg, detailedMsg) {
1384 equals( "parsererror", msg, "A parse error occurred." );
1385 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1391 test("jQuery.ajax() - script by content-type", function() {
1397 url: "data/script.php",
1398 data: { header: "script" },
1399 success: function() {
1405 test("jQuery.ajax() - json by content-type", function() {
1411 url: "data/json.php",
1412 data: { header: "json", json: "array" },
1413 success: function( json ) {
1414 ok( json.length >= 2, "Check length");
1415 equals( json[0].name, 'John', 'Check JSON: first, name' );
1416 equals( json[0].age, 21, 'Check JSON: first, age' );
1417 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1418 equals( json[1].age, 25, 'Check JSON: second, age' );
1424 test("jQuery.ajax() - json by content-type disabled with options", function() {
1430 url: url("data/json.php"),
1431 data: { header: "json", json: "array" },
1435 success: function( text ) {
1436 equals( typeof text , "string" , "json wasn't auto-determined" );
1437 var json = jQuery.parseJSON( text );
1438 ok( json.length >= 2, "Check length");
1439 equals( json[0].name, 'John', 'Check JSON: first, name' );
1440 equals( json[0].age, 21, 'Check JSON: first, age' );
1441 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1442 equals( json[1].age, 25, 'Check JSON: second, age' );
1448 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1451 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1452 ok( json.length >= 2, "Check length");
1453 equals( json[0].name, 'John', 'Check JSON: first, name' );
1454 equals( json[0].age, 21, 'Check JSON: first, age' );
1455 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1456 equals( json[1].age, 25, 'Check JSON: second, age' );
1461 test("jQuery.getJSON(String, Function) - JSON object", function() {
1464 jQuery.getJSON(url("data/json.php"), function(json) {
1465 if (json && json.data) {
1466 equals( json.data.lang, 'en', 'Check JSON: lang' );
1467 equals( json.data.length, 25, 'Check JSON: length' );
1473 test("jQuery.getJSON - Using Native JSON", function() {
1476 var old = window.JSON;
1478 parse: function(str){
1479 ok( true, "Verifying that parse method was run" );
1485 jQuery.getJSON(url("data/json.php"), function(json) {
1487 equals( json, true, "Verifying return value" );
1492 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1495 var base = window.location.href.replace(/[^\/]*$/, "");
1498 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1499 equals( json.data.lang, 'en', 'Check JSON: lang' );
1500 equals( json.data.length, 25, 'Check JSON: length' );
1505 test("jQuery.post - data", function() {
1509 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1510 jQuery('math', xml).each(function() {
1511 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1512 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1518 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1523 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1524 jQuery('math', xml).each(function() {
1525 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1526 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1528 if ( ++done === 2 ) start();
1531 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1532 jQuery('math', xml).each(function() {
1533 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1534 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1536 if ( ++done === 2 ) start();
1540 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1545 jQuery.ajaxSetup({timeout: 1000});
1547 var pass = function() {
1549 if ( passed == 2 ) {
1550 ok( true, 'Check local and global callbacks after timeout' );
1551 jQuery('#main').unbind("ajaxError");
1556 var fail = function(a,b,c) {
1557 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1561 jQuery('#main').ajaxError(pass);
1565 url: url("data/name.php?wait=5"),
1571 jQuery.ajaxSetup({timeout: 0});
1574 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1576 jQuery.ajaxSetup({timeout: 50});
1581 url: url("data/name.php?wait=1"),
1583 ok( false, 'Check for local timeout failed' );
1586 success: function() {
1587 ok( true, 'Check for local timeout' );
1593 jQuery.ajaxSetup({timeout: 0});
1596 test("jQuery.ajax - simple get", function() {
1601 url: url("data/name.php?name=foo"),
1602 success: function(msg){
1603 equals( msg, 'bar', 'Check for GET' );
1609 test("jQuery.ajax - simple post", function() {
1614 url: url("data/name.php"),
1616 success: function(msg){
1617 equals( msg, 'pan', 'Check for POST' );
1623 test("ajaxSetup()", function() {
1627 url: url("data/name.php?name=foo"),
1628 success: function(msg){
1629 equals( msg, 'bar', 'Check for GET' );
1637 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1640 url: "data/name.php?wait=1",
1642 error: function(request, status) {
1643 ok( status != null, "status shouldn't be null in error handler" );
1644 equals( "timeout", status );
1651 test("data option: evaluate function values (#2806)", function() {
1654 url: "data/echoQuery.php",
1660 success: function(result) {
1661 equals( result, "key=value" );
1667 test("data option: empty bodies for non-GET requests", function() {
1670 url: "data/echoData.php",
1673 success: function(result) {
1674 equals( result, "" );
1680 test("jQuery.ajax - If-Modified-Since support", function() {
1685 var url = "data/if_modified_since.php?ts=" + new Date();
1690 success: function(data, status) {
1691 equals(status, "success");
1696 success: function(data, status) {
1697 if ( data === "FAIL" ) {
1698 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1699 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1701 equals(status, "notmodified");
1702 ok(data == null, "response body should be empty");
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");
1711 ok(jQuery.browser.opera, "error");
1717 equals(false, "error");
1718 // Do this because opera simply refuses to implement 304 handling :(
1719 // A feature-driven way of detecting this would be appreciated
1720 // See: http://gist.github.com/599419
1721 ok(jQuery.browser.opera, "error");
1727 test("jQuery.ajax - Etag support", function() {
1732 var url = "data/etag.php?ts=" + new Date();
1737 success: function(data, status) {
1738 equals(status, "success");
1743 success: function(data, status) {
1744 if ( data === "FAIL" ) {
1745 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1746 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1748 equals(status, "notmodified");
1749 ok(data == null, "response body should be empty");
1754 // Do this because opera simply refuses to implement 304 handling :(
1755 // A feature-driven way of detecting this would be appreciated
1756 // See: http://gist.github.com/599419
1757 ok(jQuery.browser.opera, "error");
1758 ok(jQuery.browser.opera, "error");
1764 // Do this because opera simply refuses to implement 304 handling :(
1765 // A feature-driven way of detecting this would be appreciated
1766 // See: http://gist.github.com/599419
1767 ok(jQuery.browser.opera, "error");
1773 test("jQuery ajax - failing cross-domain", function() {
1782 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1783 success: function(){ ok( false , "success" ); },
1784 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1785 complete: function() { if ( ! --i ) start(); }
1787 ok( true , "no transport" );
1788 if ( ! --i ) start();
1792 url: 'http://www.google.com',
1793 success: function(){ ok( false , "success" ); },
1794 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1795 complete: function() { if ( ! --i ) start(); }
1797 ok( true , "no transport" );
1798 if ( ! --i ) start();
1803 test("jQuery ajax - atom+xml", function() {
1808 url: url( 'data/atom+xml.php' ),
1809 success: function(){ ok( true , "success" ); },
1810 error: function(){ ok( false , "error" ); },
1811 complete: function() { start(); }
1816 test("jQuery.ajax - active counter", function() {
1817 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1820 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1821 var success = false;
1823 var xhr = jQuery.ajax({ url: window.location });
1828 ok( success, "document.location did not generate exception" );