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() - headers" , function() {
266 var requestHeaders = {
268 "SometHing-elsE": "other value",
269 OthEr: "something else"
274 for( i in requestHeaders ) {
278 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
279 headers: requestHeaders,
280 success: function( data , _ , xhr ) {
282 for ( i in requestHeaders ) {
283 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
285 tmp = tmp.join( "" );
287 equals( data , tmp , "Headers were sent" );
288 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
291 error: function(){ ok(false, "error"); }
296 test(".ajax() - contentType" , function() {
310 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
312 success: function( data ) {
313 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
315 complete: function() {
320 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
322 success: function( data ) {
323 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
325 complete: function() {
332 test(".ajax() - hash", function() {
336 url: "data/name.html#foo",
337 beforeSend: function( xhr, settings ) {
338 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
344 url: "data/name.html?abc#foo",
345 beforeSend: function( xhr, settings ) {
346 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
352 url: "data/name.html?abc#foo",
353 data: { "test": 123 },
354 beforeSend: function( xhr, settings ) {
355 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
361 test("jQuery ajax - cross-domain detection", function() {
365 var loc = document.location,
366 otherPort = loc.port === 666 ? 667 : 666,
367 otherProtocol = loc.protocol === "http:" ? "https:" : "http:",
373 url: otherProtocol + "//" + loc.host,
374 beforeSend: function( _ , s ) {
376 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
380 if ( ! protocolFlag ) {
381 ok( ! jQuery.support.cors , "Test different protocols are detected as cross-domain (no transport)" );
386 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
387 beforeSend: function( _ , s ) {
389 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
394 ok( ! jQuery.support.cors , "Test different hostnames are detected as cross-domain (no transport)" );
399 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
400 beforeSend: function( _ , s ) {
402 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
407 ok( ! jQuery.support.cors , "Test different ports are detected as cross-domain (no transport)" );
413 test(".ajax() - 304", function() {
418 url: url("data/notmodified.php"),
419 success: function(){ ok(true, "304 ok"); },
420 // Do this because opera simply refuses to implement 304 handling :(
421 // A feature-driven way of detecting this would be appreciated
422 // See: http://gist.github.com/599419
423 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
424 complete: function(xhr){ start(); }
428 test(".load()) - 404 error callbacks", function() {
432 jQuery('#foo').ajaxStart(function(){
433 ok( true, "ajaxStart" );
434 }).ajaxStop(function(){
435 ok( true, "ajaxStop" );
437 }).ajaxSend(function(){
438 ok( true, "ajaxSend" );
439 }).ajaxComplete(function(){
440 ok( true, "ajaxComplete" );
441 }).ajaxError(function(){
442 ok( true, "ajaxError" );
443 }).ajaxSuccess(function(){
444 ok( false, "ajaxSuccess" );
447 jQuery("<div/>").load("data/404.html", function(){
448 ok(true, "complete");
452 test("jQuery.ajax() - abort", function() {
456 jQuery('#foo').ajaxStart(function(){
457 ok( true, "ajaxStart" );
458 }).ajaxStop(function(){
459 ok( true, "ajaxStop" );
461 }).ajaxSend(function(){
462 ok( true, "ajaxSend" );
463 }).ajaxComplete(function(){
464 ok( true, "ajaxComplete" );
467 var xhr = jQuery.ajax({
468 url: url("data/name.php?wait=5"),
469 beforeSend: function(){ ok(true, "beforeSend"); },
470 complete: function(){ ok(true, "complete"); }
473 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
476 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
479 test("Ajax events with context", function() {
483 var context = document.createElement("div");
486 equals( this, context, e.type );
489 function callback(msg){
491 equals( this, context, "context is preserved on callback " + msg );
495 function nocallback(msg){
497 equals( typeof this.url, "string", "context is settings on callback " + msg );
501 jQuery('#foo').add(context)
508 url: url("data/name.html"),
509 beforeSend: callback("beforeSend"),
510 success: callback("success"),
511 error: callback("error"),
513 callback("complete").call(this);
516 url: url("data/404.html"),
518 beforeSend: callback("beforeSend"),
519 error: callback("error"),
520 complete: function(){
521 callback("complete").call(this);
523 jQuery('#foo').add(context).unbind();
526 url: url("data/404.html"),
527 beforeSend: nocallback("beforeSend"),
528 error: nocallback("error"),
529 complete: function(){
530 nocallback("complete").call(this);
541 test("jQuery.ajax context modification", function() {
549 url: url("data/name.html"),
551 beforeSend: function(){
554 complete: function() {
559 equals( obj.test, "foo", "Make sure the original object is maintained." );
562 test("jQuery.ajax() - disabled globals", function() {
566 jQuery('#foo').ajaxStart(function(){
567 ok( false, "ajaxStart" );
568 }).ajaxStop(function(){
569 ok( false, "ajaxStop" );
570 }).ajaxSend(function(){
571 ok( false, "ajaxSend" );
572 }).ajaxComplete(function(){
573 ok( false, "ajaxComplete" );
574 }).ajaxError(function(){
575 ok( false, "ajaxError" );
576 }).ajaxSuccess(function(){
577 ok( false, "ajaxSuccess" );
582 url: url("data/name.html"),
583 beforeSend: function(){ ok(true, "beforeSend"); },
584 success: function(){ ok(true, "success"); },
585 error: function(){ ok(false, "error"); },
586 complete: function(){
587 ok(true, "complete");
588 setTimeout(function(){ start(); }, 13);
593 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
597 url: url("data/with_fries.xml"),
599 success: function(resp) {
600 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
601 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
602 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
608 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
612 url: url("data/with_fries_over_jsonp.php"),
613 dataType: "jsonp xml",
614 success: function(resp) {
615 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
616 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
617 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
620 error: function(_1,_2,error) {
627 test("jQuery.ajax - HEAD requests", function() {
632 url: url("data/name.html"),
634 success: function(data, status, xhr){
635 var h = xhr.getAllResponseHeaders();
636 ok( /Date/i.test(h), 'No Date in HEAD response' );
639 url: url("data/name.html"),
640 data: { whip_it: "good" },
642 success: function(data, status, xhr){
643 var h = xhr.getAllResponseHeaders();
644 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
653 test("jQuery.ajax - beforeSend", function() {
659 jQuery.ajaxSetup({ timeout: 0 });
662 url: url("data/name.html"),
663 beforeSend: function(xml) {
666 success: function(data) {
667 ok( check, "check beforeSend was executed" );
673 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
675 var request = jQuery.ajax({
676 url: url("data/name.html"),
677 beforeSend: function() {
678 ok( true, "beforeSend got called, canceling" );
681 success: function() {
682 ok( false, "request didn't get canceled" );
684 complete: function() {
685 ok( false, "request didn't get canceled" );
688 ok( false, "request didn't get canceled" );
691 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
694 test("jQuery.ajax - beforeSend, cancel request manually", function() {
696 var request = jQuery.ajax({
697 url: url("data/name.html"),
698 beforeSend: function(xhr) {
699 ok( true, "beforeSend got called, canceling" );
702 success: function() {
703 ok( false, "request didn't get canceled" );
705 complete: function() {
706 ok( false, "request didn't get canceled" );
709 ok( false, "request didn't get canceled" );
712 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
715 window.foobar = null;
716 window.testFoo = undefined;
718 test("jQuery.ajax - dataType html", function() {
722 var verifyEvaluation = function() {
723 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
724 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
731 url: url("data/test.html"),
732 success: function(data) {
733 jQuery("#ap").html(data);
734 ok( data.match(/^html text/), 'Check content for datatype html' );
735 setTimeout(verifyEvaluation, 600);
740 test("serialize()", function() {
743 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
744 jQuery("#search").after(
745 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
746 '<input type="number" id="html5number" name="number" value="43" />'
749 equals( jQuery('#form').serialize(),
750 "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",
751 'Check form serialization as query string');
753 equals( jQuery('#form :input').serialize(),
754 "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",
755 'Check input serialization as query string');
757 equals( jQuery('#testForm').serialize(),
758 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
759 'Check form serialization as query string');
761 equals( jQuery('#testForm :input').serialize(),
762 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
763 'Check input serialization as query string');
765 equals( jQuery('#form, #testForm').serialize(),
766 "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=",
767 'Multiple form serialization as query string');
769 /* Temporarily disabled. Opera 10 has problems with form serialization.
770 equals( jQuery('#form, #testForm :input').serialize(),
771 "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=",
772 'Mixed form/input serialization as query string');
774 jQuery("#html5email, #html5number").remove();
777 test("jQuery.param()", function() {
780 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
782 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
783 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
785 params = {someName: [1, 2, 3], regularThing: "blah" };
786 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
788 params = {foo: ['a', 'b', 'c']};
789 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
791 params = {foo: ["baz", 42, "All your base are belong to us"] };
792 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
794 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
795 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" );
797 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?" };
798 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" );
800 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 ] };
801 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" );
803 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?" };
804 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" );
806 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." );
808 // Make sure empty arrays and objects are handled #6481
809 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
810 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
811 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
813 jQuery.ajaxSetup({ traditional: true });
815 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
816 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
818 params = {someName: [1, 2, 3], regularThing: "blah" };
819 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
821 params = {foo: ['a', 'b', 'c']};
822 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
824 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
825 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
827 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
828 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" );
830 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?" };
831 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" );
833 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 ] };
834 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)" );
836 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?" };
837 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" );
839 params = { param1: null };
840 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
843 test("synchronous request", function() {
845 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
848 test("synchronous request with callbacks", function() {
851 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
852 ok( /^{ "data"/.test( result ), "check returned text" );
855 test("pass-through request object", function() {
859 var target = "data/name.html";
860 var successCount = 0;
863 var success = function() {
866 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
868 errorEx += ": " + xml.status;
870 jQuery("#foo").one('ajaxStop', function () {
871 equals(successCount, 5, "Check all ajax calls successful");
872 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
873 jQuery("#foo").unbind('ajaxError');
878 ok( jQuery.get(url(target), success), "get" );
879 ok( jQuery.post(url(target), success), "post" );
880 ok( jQuery.getScript(url("data/test.js"), success), "script" );
881 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
882 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
885 test("ajax cache", function () {
892 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
893 var re = /_=(.*?)(&|$)/g;
895 for (var i = 0; i < 6; i++) {
896 var ret = re.exec(s.url);
902 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
903 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
908 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
909 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
910 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
911 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
912 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
913 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
918 * The assertions expect that the passed-in object will be modified,
919 * which shouldn't be the case. Fixes #5439.
920 test("global ajaxSettings", function() {
923 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
924 var orig = { url: "data/with_fries.xml" };
927 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
929 t = jQuery.extend({}, orig);
932 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
934 t = jQuery.extend({}, orig);
935 t.data = { zoo: 'a', ping: 'b' };
937 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' }" );
939 jQuery.ajaxSettings = tmp;
943 test("load(String)", function() {
945 stop(); // check if load can be called with only url
946 jQuery('#first').load("data/name.html", start);
949 test("load('url selector')", function() {
951 stop(); // check if load can be called with only url
952 jQuery('#first').load("data/test3.html div.user", function(){
953 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
958 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
961 jQuery.ajaxSetup({ dataType: "json" });
962 jQuery("#first").ajaxComplete(function (e, xml, s) {
963 equals( s.dataType, "html", "Verify the load() dataType was html" );
964 jQuery("#first").unbind("ajaxComplete");
965 jQuery.ajaxSetup({ dataType: "" });
968 jQuery('#first').load("data/test3.html");
971 test("load(String, Function) - simple: inject text into DOM", function() {
974 jQuery('#first').load(url("data/name.html"), function() {
975 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
980 test("load(String, Function) - check scripts", function() {
984 var verifyEvaluation = function() {
985 equals( foobar, "bar", 'Check if script src was evaluated after load' );
986 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
990 jQuery('#first').load(url('data/test.html'), function() {
991 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
992 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
993 equals( testFoo, "foo", 'Check if script was evaluated after load' );
994 setTimeout(verifyEvaluation, 600);
998 test("load(String, Function) - check file with only a script tag", function() {
1002 jQuery('#first').load(url('data/test2.html'), function() {
1003 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1004 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1010 test("load(String, Object, Function)", function() {
1014 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1015 var $post = jQuery(this).find('#post');
1016 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1017 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1022 test("load(String, String, Function)", function() {
1026 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1027 var $get = jQuery(this).find('#get');
1028 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1029 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1034 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1037 jQuery.get(url('data/dashboard.xml'), function(xml) {
1039 jQuery('tab', xml).each(function() {
1040 content.push(jQuery(this).text());
1042 equals( content[0], 'blabla', 'Check first tab');
1043 equals( content[1], 'blublu', 'Check second tab');
1048 test("jQuery.getScript(String, Function) - with callback", function() {
1051 jQuery.getScript(url("data/test.js"), function() {
1052 equals( foobar, "bar", 'Check if script was evaluated' );
1053 setTimeout(start, 100);
1057 test("jQuery.getScript(String, Function) - no callback", function() {
1060 jQuery.getScript(url("data/test.js"), function(){
1065 test("jQuery.ajax() - JSONP, Local", function() {
1069 function plus(){ if ( ++count == 9 ) start(); }
1074 url: "data/jsonp.php",
1076 success: function(data){
1077 ok( data.data, "JSON results returned (GET, no callback)" );
1080 error: function(data){
1081 ok( false, "Ajax error JSON (GET, no callback)" );
1087 url: "data/jsonp.php?callback=?",
1089 success: function(data){
1090 ok( data.data, "JSON results returned (GET, url callback)" );
1093 error: function(data){
1094 ok( false, "Ajax error JSON (GET, url callback)" );
1100 url: "data/jsonp.php",
1103 success: function(data){
1104 ok( data.data, "JSON results returned (GET, data callback)" );
1107 error: function(data){
1108 ok( false, "Ajax error JSON (GET, data callback)" );
1114 url: "data/jsonp.php",
1117 success: function(data){
1118 ok( data.data, "JSON results returned (GET, data obj callback)" );
1121 error: function(data){
1122 ok( false, "Ajax error JSON (GET, data obj callback)" );
1128 url: "data/jsonp.php",
1130 jsonpCallback: "jsonpResults",
1131 success: function(data){
1132 ok( data.data, "JSON results returned (GET, custom callback name)" );
1135 error: function(data){
1136 ok( false, "Ajax error JSON (GET, custom callback name)" );
1143 url: "data/jsonp.php",
1145 success: function(data){
1146 ok( data.data, "JSON results returned (POST, no callback)" );
1149 error: function(data){
1150 ok( false, "Ajax error JSON (GET, data obj callback)" );
1157 url: "data/jsonp.php",
1160 success: function(data){
1161 ok( data.data, "JSON results returned (POST, data callback)" );
1164 error: function(data){
1165 ok( false, "Ajax error JSON (POST, data callback)" );
1172 url: "data/jsonp.php",
1175 success: function(data){
1176 ok( data.data, "JSON results returned (POST, data obj callback)" );
1179 error: function(data){
1180 ok( false, "Ajax error JSON (POST, data obj callback)" );
1187 url: "data/jsonp.php",
1189 beforeSend: function(){
1190 strictEqual( this.cache, false, "cache must be false on JSON request" );
1197 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1201 window.jsonpResults = function(data) {
1202 ok( data.data, "JSON results returned (GET, custom callback function)" );
1203 window.jsonpResults = undefined;
1208 url: "data/jsonp.php",
1210 jsonpCallback: "jsonpResults"
1214 test("jQuery.ajax() - JSONP, Remote", function() {
1218 function plus(){ if ( ++count == 4 ) start(); }
1220 var base = window.location.href.replace(/[^\/]*$/, "");
1225 url: base + "data/jsonp.php",
1227 success: function(data){
1228 ok( data.data, "JSON results returned (GET, no callback)" );
1231 error: function(data){
1232 ok( false, "Ajax error JSON (GET, no callback)" );
1238 url: base + "data/jsonp.php?callback=?",
1240 success: function(data){
1241 ok( data.data, "JSON results returned (GET, url callback)" );
1244 error: function(data){
1245 ok( false, "Ajax error JSON (GET, url callback)" );
1251 url: base + "data/jsonp.php",
1254 success: function(data){
1255 ok( data.data, "JSON results returned (GET, data callback)" );
1258 error: function(data){
1259 ok( false, "Ajax error JSON (GET, data callback)" );
1265 url: base + "data/jsonp.php",
1268 success: function(data){
1269 ok( data.data, "JSON results returned (GET, data obj callback)" );
1272 error: function(data){
1273 ok( false, "Ajax error JSON (GET, data obj callback)" );
1279 test("jQuery.ajax() - script, Remote", function() {
1282 var base = window.location.href.replace(/[^\/]*$/, "");
1287 url: base + "data/test.js",
1289 success: function(data){
1290 ok( foobar, "Script results returned (GET, no callback)" );
1296 test("jQuery.ajax() - script, Remote with POST", function() {
1299 var base = window.location.href.replace(/[^\/]*$/, "");
1304 url: base + "data/test.js",
1307 success: function(data, status){
1308 ok( foobar, "Script results returned (POST, no callback)" );
1309 equals( status, "success", "Script results returned (POST, no callback)" );
1312 error: function(xhr) {
1313 ok( false, "ajax error, status code: " + xhr.status );
1319 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1322 var base = window.location.href.replace(/[^\/]*$/, "");
1323 base = base.replace(/^.*?\/\//, "//");
1328 url: base + "data/test.js",
1330 success: function(data){
1331 ok( foobar, "Script results returned (GET, no callback)" );
1337 test("jQuery.ajax() - malformed JSON", function() {
1343 url: "data/badjson.js",
1345 success: function(){
1346 ok( false, "Success." );
1349 error: function(xhr, msg, detailedMsg) {
1350 equals( "parsererror", msg, "A parse error occurred." );
1351 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1357 test("jQuery.ajax() - script by content-type", function() {
1363 url: "data/script.php",
1364 data: { header: "script" },
1365 success: function() {
1371 test("jQuery.ajax() - json by content-type", function() {
1377 url: "data/json.php",
1378 data: { header: "json", json: "array" },
1379 success: function( json ) {
1380 ok( json.length >= 2, "Check length");
1381 equals( json[0].name, 'John', 'Check JSON: first, name' );
1382 equals( json[0].age, 21, 'Check JSON: first, age' );
1383 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1384 equals( json[1].age, 25, 'Check JSON: second, age' );
1390 test("jQuery.ajax() - json by content-type disabled with options", function() {
1396 url: url("data/json.php"),
1397 data: { header: "json", json: "array" },
1401 success: function( text ) {
1402 equals( typeof text , "string" , "json wasn't auto-determined" );
1403 var json = jQuery.parseJSON( text );
1404 ok( json.length >= 2, "Check length");
1405 equals( json[0].name, 'John', 'Check JSON: first, name' );
1406 equals( json[0].age, 21, 'Check JSON: first, age' );
1407 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1408 equals( json[1].age, 25, 'Check JSON: second, age' );
1414 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1417 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1418 ok( json.length >= 2, "Check length");
1419 equals( json[0].name, 'John', 'Check JSON: first, name' );
1420 equals( json[0].age, 21, 'Check JSON: first, age' );
1421 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1422 equals( json[1].age, 25, 'Check JSON: second, age' );
1427 test("jQuery.getJSON(String, Function) - JSON object", function() {
1430 jQuery.getJSON(url("data/json.php"), function(json) {
1431 if (json && json.data) {
1432 equals( json.data.lang, 'en', 'Check JSON: lang' );
1433 equals( json.data.length, 25, 'Check JSON: length' );
1439 test("jQuery.getJSON - Using Native JSON", function() {
1442 var old = window.JSON;
1444 parse: function(str){
1445 ok( true, "Verifying that parse method was run" );
1451 jQuery.getJSON(url("data/json.php"), function(json) {
1453 equals( json, true, "Verifying return value" );
1458 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1461 var base = window.location.href.replace(/[^\/]*$/, "");
1464 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1465 equals( json.data.lang, 'en', 'Check JSON: lang' );
1466 equals( json.data.length, 25, 'Check JSON: length' );
1471 test("jQuery.post - data", function() {
1475 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1476 jQuery('math', xml).each(function() {
1477 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1478 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1484 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1489 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1490 jQuery('math', xml).each(function() {
1491 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1492 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1494 if ( ++done === 2 ) start();
1497 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1498 jQuery('math', xml).each(function() {
1499 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1500 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1502 if ( ++done === 2 ) start();
1506 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1511 jQuery.ajaxSetup({timeout: 1000});
1513 var pass = function() {
1515 if ( passed == 2 ) {
1516 ok( true, 'Check local and global callbacks after timeout' );
1517 jQuery('#main').unbind("ajaxError");
1522 var fail = function(a,b,c) {
1523 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1527 jQuery('#main').ajaxError(pass);
1531 url: url("data/name.php?wait=5"),
1537 jQuery.ajaxSetup({timeout: 0});
1540 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1542 jQuery.ajaxSetup({timeout: 50});
1547 url: url("data/name.php?wait=1"),
1549 ok( false, 'Check for local timeout failed' );
1552 success: function() {
1553 ok( true, 'Check for local timeout' );
1559 jQuery.ajaxSetup({timeout: 0});
1562 test("jQuery.ajax - simple get", function() {
1567 url: url("data/name.php?name=foo"),
1568 success: function(msg){
1569 equals( msg, 'bar', 'Check for GET' );
1575 test("jQuery.ajax - simple post", function() {
1580 url: url("data/name.php"),
1582 success: function(msg){
1583 equals( msg, 'pan', 'Check for POST' );
1589 test("ajaxSetup()", function() {
1593 url: url("data/name.php?name=foo"),
1594 success: function(msg){
1595 equals( msg, 'bar', 'Check for GET' );
1603 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1606 url: "data/name.php?wait=1",
1608 error: function(request, status) {
1609 ok( status != null, "status shouldn't be null in error handler" );
1610 equals( "timeout", status );
1617 test("data option: evaluate function values (#2806)", function() {
1620 url: "data/echoQuery.php",
1626 success: function(result) {
1627 equals( result, "key=value" );
1633 test("data option: empty bodies for non-GET requests", function() {
1636 url: "data/echoData.php",
1639 success: function(result) {
1640 equals( result, "" );
1646 test("jQuery.ajax - If-Modified-Since support", function() {
1651 var url = "data/if_modified_since.php?ts=" + new Date();
1656 success: function(data, status) {
1657 equals(status, "success");
1662 success: function(data, status) {
1663 if ( data === "FAIL" ) {
1664 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1665 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1667 equals(status, "notmodified");
1668 ok(data == null, "response body should be empty");
1673 // Do this because opera simply refuses to implement 304 handling :(
1674 // A feature-driven way of detecting this would be appreciated
1675 // See: http://gist.github.com/599419
1676 ok(jQuery.browser.opera, "error");
1677 ok(jQuery.browser.opera, "error");
1683 equals(false, "error");
1684 // Do this because opera simply refuses to implement 304 handling :(
1685 // A feature-driven way of detecting this would be appreciated
1686 // See: http://gist.github.com/599419
1687 ok(jQuery.browser.opera, "error");
1693 test("jQuery.ajax - Etag support", function() {
1698 var url = "data/etag.php?ts=" + new Date();
1703 success: function(data, status) {
1704 equals(status, "success");
1709 success: function(data, status) {
1710 if ( data === "FAIL" ) {
1711 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1712 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1714 equals(status, "notmodified");
1715 ok(data == null, "response body should be empty");
1720 // Do this because opera simply refuses to implement 304 handling :(
1721 // A feature-driven way of detecting this would be appreciated
1722 // See: http://gist.github.com/599419
1723 ok(jQuery.browser.opera, "error");
1724 ok(jQuery.browser.opera, "error");
1730 // Do this because opera simply refuses to implement 304 handling :(
1731 // A feature-driven way of detecting this would be appreciated
1732 // See: http://gist.github.com/599419
1733 ok(jQuery.browser.opera, "error");
1739 test("jQuery ajax - failing cross-domain", function() {
1748 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1749 success: function(){ ok( false , "success" ); },
1750 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1751 complete: function() { if ( ! --i ) start(); }
1753 ok( true , "no transport" );
1754 if ( ! --i ) start();
1758 url: 'http://www.google.com',
1759 success: function(){ ok( false , "success" ); },
1760 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1761 complete: function() { if ( ! --i ) start(); }
1763 ok( true , "no transport" );
1764 if ( ! --i ) start();
1769 test("jQuery ajax - atom+xml", function() {
1774 url: url( 'data/atom+xml.php' ),
1775 success: function(){ ok( true , "success" ); },
1776 error: function(){ ok( false , "error" ); },
1777 complete: function() { start(); }
1782 test("jQuery.ajax - active counter", function() {
1783 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1786 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1787 var success = false;
1789 var xhr = jQuery.ajax({ url: window.location });
1794 ok( success, "document.location did not generate exception" );