1 module("ajax", { teardown: moduleTeardown });
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() - textStatus and errorThrown values", function() {
257 url: url("data/nonExistingURL"),
258 error: function( _ , textStatus , errorThrown ){
259 strictEqual( textStatus, "error", "textStatus is 'error' for 404" );
260 strictEqual( errorThrown, "Not Found", "errorThrown is 'Not Found' for 404");
266 url: url("data/name.php?wait=5"),
267 error: function( _ , textStatus , errorThrown ){
268 strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
269 strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort");
275 url: url("data/name.php?wait=5"),
276 error: function( _ , textStatus , errorThrown ){
277 strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
278 strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')");
281 }).abort( "mystatus" );
284 test("jQuery.ajax() - responseText on error", function() {
291 url: url("data/errorWithText.php"),
292 error: function(xhr) {
293 strictEqual( xhr.responseText , "plain text message" , "Test jXHR.responseText is filled for HTTP errors" );
295 complete: function() {
301 test(".ajax() - retry with jQuery.ajax( this )", function() {
310 url: url("data/errorWithText.php"),
316 ok( true , "Test retrying with jQuery.ajax(this) works" );
324 test(".ajax() - headers" , function() {
330 var requestHeaders = {
332 "SometHing-elsE": "other value",
333 OthEr: "something else"
338 for( i in requestHeaders ) {
342 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
343 headers: requestHeaders,
344 success: function( data , _ , xhr ) {
346 for ( i in requestHeaders ) {
347 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
349 tmp = tmp.join( "" );
351 equals( data , tmp , "Headers were sent" );
352 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
355 error: function(){ ok(false, "error"); }
360 test(".ajax() - Accept header" , function() {
366 jQuery.ajax(url("data/headers.php?keys=accept"), {
368 Accept: "very wrong accept value"
370 beforeSend: function( xhr ) {
371 xhr.setRequestHeader( "Accept", "*/*" );
373 success: function( data ) {
374 strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
377 error: function(){ ok(false, "error"); }
382 test(".ajax() - contentType" , function() {
396 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
398 success: function( data ) {
399 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
401 complete: function() {
406 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
408 success: function( data ) {
409 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
411 complete: function() {
418 test(".ajax() - protocol-less urls", function() {
422 url: "//somedomain.com",
423 beforeSend: function( xhr, settings ) {
424 equals(settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added.");
430 test(".ajax() - hash", function() {
434 url: "data/name.html#foo",
435 beforeSend: function( xhr, settings ) {
436 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
442 url: "data/name.html?abc#foo",
443 beforeSend: function( xhr, settings ) {
444 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
450 url: "data/name.html?abc#foo",
451 data: { "test": 123 },
452 beforeSend: function( xhr, settings ) {
453 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
459 test("jQuery ajax - cross-domain detection", function() {
463 var loc = document.location,
464 otherPort = loc.port === 666 ? 667 : 666,
465 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
469 url: otherProtocol + "//" + loc.host,
470 beforeSend: function( _ , s ) {
471 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
478 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
479 beforeSend: function( _ , s ) {
480 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
487 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
488 beforeSend: function( _ , s ) {
489 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
496 url: loc.protocol + "//" + loc.host,
498 beforeSend: function( _ , s ) {
499 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
506 test(".ajax() - 304", function() {
511 url: url("data/notmodified.php"),
512 success: function(){ ok(true, "304 ok"); },
513 // Do this because opera simply refuses to implement 304 handling :(
514 // A feature-driven way of detecting this would be appreciated
515 // See: http://gist.github.com/599419
516 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
517 complete: function(xhr){ start(); }
521 test(".load()) - 404 error callbacks", function() {
525 jQuery('#foo').ajaxStart(function(){
526 ok( true, "ajaxStart" );
527 }).ajaxStop(function(){
528 ok( true, "ajaxStop" );
530 }).ajaxSend(function(){
531 ok( true, "ajaxSend" );
532 }).ajaxComplete(function(){
533 ok( true, "ajaxComplete" );
534 }).ajaxError(function(){
535 ok( true, "ajaxError" );
536 }).ajaxSuccess(function(){
537 ok( false, "ajaxSuccess" );
540 jQuery("<div/>").load("data/404.html", function(){
541 ok(true, "complete");
545 test("jQuery.ajax() - abort", function() {
549 jQuery('#foo').ajaxStart(function(){
550 ok( true, "ajaxStart" );
551 }).ajaxStop(function(){
552 ok( true, "ajaxStop" );
554 }).ajaxSend(function(){
555 ok( true, "ajaxSend" );
556 }).ajaxComplete(function(){
557 ok( true, "ajaxComplete" );
560 var xhr = jQuery.ajax({
561 url: url("data/name.php?wait=5"),
562 beforeSend: function(){ ok(true, "beforeSend"); },
563 complete: function(){ ok(true, "complete"); }
566 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
569 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
572 test("Ajax events with context", function() {
576 var context = document.createElement("div");
579 equals( this, context, e.type );
582 function callback(msg){
584 equals( this, context, "context is preserved on callback " + msg );
588 function nocallback(msg){
590 equals( typeof this.url, "string", "context is settings on callback " + msg );
594 jQuery('#foo').add(context)
601 url: url("data/name.html"),
602 beforeSend: callback("beforeSend"),
603 success: callback("success"),
604 error: callback("error"),
606 callback("complete").call(this);
609 url: url("data/404.html"),
611 beforeSend: callback("beforeSend"),
612 error: callback("error"),
613 complete: function(){
614 callback("complete").call(this);
616 jQuery('#foo').add(context).unbind();
619 url: url("data/404.html"),
620 beforeSend: nocallback("beforeSend"),
621 error: nocallback("error"),
622 complete: function(){
623 nocallback("complete").call(this);
634 test("jQuery.ajax context modification", function() {
642 url: url("data/name.html"),
644 beforeSend: function(){
647 complete: function() {
652 equals( obj.test, "foo", "Make sure the original object is maintained." );
655 test("jQuery.ajax context modification through ajaxSetup", function() {
666 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
669 url: url("data/name.html"),
670 complete: function() {
671 strictEqual( this, obj, "Make sure the original object is maintained." );
673 url: url("data/name.html"),
675 complete: function() {
676 ok( this !== obj, "Make sure overidding context is possible." );
681 url: url("data/name.html"),
682 beforeSend: function(){
685 complete: function() {
686 ok( this !== obj, "Make sure unsetting context is possible." );
696 test("jQuery.ajax() - disabled globals", function() {
700 jQuery('#foo').ajaxStart(function(){
701 ok( false, "ajaxStart" );
702 }).ajaxStop(function(){
703 ok( false, "ajaxStop" );
704 }).ajaxSend(function(){
705 ok( false, "ajaxSend" );
706 }).ajaxComplete(function(){
707 ok( false, "ajaxComplete" );
708 }).ajaxError(function(){
709 ok( false, "ajaxError" );
710 }).ajaxSuccess(function(){
711 ok( false, "ajaxSuccess" );
716 url: url("data/name.html"),
717 beforeSend: function(){ ok(true, "beforeSend"); },
718 success: function(){ ok(true, "success"); },
719 error: function(){ ok(false, "error"); },
720 complete: function(){
721 ok(true, "complete");
722 setTimeout(function(){ start(); }, 13);
727 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
731 url: url("data/with_fries.xml"),
733 success: function(resp) {
734 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
735 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
736 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
742 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
746 url: url("data/with_fries_over_jsonp.php"),
747 dataType: "jsonp xml",
748 success: function(resp) {
749 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
750 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
751 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
754 error: function(_1,_2,error) {
761 test("jQuery.ajax - HEAD requests", function() {
766 url: url("data/name.html"),
768 success: function(data, status, xhr){
769 var h = xhr.getAllResponseHeaders();
770 ok( /Date/i.test(h), 'No Date in HEAD response' );
773 url: url("data/name.html"),
774 data: { whip_it: "good" },
776 success: function(data, status, xhr){
777 var h = xhr.getAllResponseHeaders();
778 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
787 test("jQuery.ajax - beforeSend", function() {
793 jQuery.ajaxSetup({ timeout: 0 });
796 url: url("data/name.html"),
797 beforeSend: function(xml) {
800 success: function(data) {
801 ok( check, "check beforeSend was executed" );
807 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
809 var request = jQuery.ajax({
810 url: url("data/name.html"),
811 beforeSend: function() {
812 ok( true, "beforeSend got called, canceling" );
815 success: function() {
816 ok( false, "request didn't get canceled" );
818 complete: function() {
819 ok( false, "request didn't get canceled" );
822 ok( false, "request didn't get canceled" );
825 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
828 test("jQuery.ajax - beforeSend, cancel request manually", function() {
830 var request = jQuery.ajax({
831 url: url("data/name.html"),
832 beforeSend: function(xhr) {
833 ok( true, "beforeSend got called, canceling" );
836 success: function() {
837 ok( false, "request didn't get canceled" );
839 complete: function() {
840 ok( false, "request didn't get canceled" );
843 ok( false, "request didn't get canceled" );
846 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
849 window.foobar = null;
850 window.testFoo = undefined;
852 test("jQuery.ajax - dataType html", function() {
856 var verifyEvaluation = function() {
857 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
858 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
865 url: url("data/test.html"),
866 success: function(data) {
867 jQuery("#ap").html(data);
868 ok( data.match(/^html text/), 'Check content for datatype html' );
869 setTimeout(verifyEvaluation, 600);
874 test("serialize()", function() {
877 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
878 jQuery("#search").after(
879 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
880 '<input type="number" id="html5number" name="number" value="43" />'
883 equals( jQuery('#form').serialize(),
884 "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",
885 'Check form serialization as query string');
887 equals( jQuery('#form :input').serialize(),
888 "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",
889 'Check input serialization as query string');
891 equals( jQuery('#testForm').serialize(),
892 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
893 'Check form serialization as query string');
895 equals( jQuery('#testForm :input').serialize(),
896 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
897 'Check input serialization as query string');
899 equals( jQuery('#form, #testForm').serialize(),
900 "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=",
901 'Multiple form serialization as query string');
903 /* Temporarily disabled. Opera 10 has problems with form serialization.
904 equals( jQuery('#form, #testForm :input').serialize(),
905 "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=",
906 'Mixed form/input serialization as query string');
908 jQuery("#html5email, #html5number").remove();
911 test("jQuery.param()", function() {
914 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
916 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
917 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
919 params = {someName: [1, 2, 3], regularThing: "blah" };
920 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
922 params = {foo: ['a', 'b', 'c']};
923 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
925 params = {foo: ["baz", 42, "All your base are belong to us"] };
926 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
928 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
929 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" );
931 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?" };
932 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" );
934 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 ] };
935 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" );
937 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?" };
938 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" );
940 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." );
942 // Make sure empty arrays and objects are handled #6481
943 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
944 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
945 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
947 jQuery.ajaxSetup({ traditional: true });
949 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
950 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
952 params = {someName: [1, 2, 3], regularThing: "blah" };
953 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
955 params = {foo: ['a', 'b', 'c']};
956 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
958 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
959 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
961 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
962 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" );
964 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?" };
965 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" );
967 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 ] };
968 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)" );
970 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?" };
971 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" );
973 params = { param1: null };
974 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
977 test("synchronous request", function() {
979 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
982 test("synchronous request with callbacks", function() {
985 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
986 ok( /^{ "data"/.test( result ), "check returned text" );
989 test("pass-through request object", function() {
993 var target = "data/name.html";
994 var successCount = 0;
997 var success = function() {
1000 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1002 errorEx += ": " + xml.status;
1004 jQuery("#foo").one('ajaxStop', function () {
1005 equals(successCount, 5, "Check all ajax calls successful");
1006 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1007 jQuery("#foo").unbind('ajaxError');
1012 ok( jQuery.get(url(target), success), "get" );
1013 ok( jQuery.post(url(target), success), "post" );
1014 ok( jQuery.getScript(url("data/test.js"), success), "script" );
1015 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1016 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1019 test("ajax cache", function () {
1026 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1027 var re = /_=(.*?)(&|$)/g;
1029 for (var i = 0; i < 6; i++) {
1030 var ret = re.exec(s.url);
1036 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1037 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1042 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1043 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1044 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1045 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1046 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1047 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1052 * The assertions expect that the passed-in object will be modified,
1053 * which shouldn't be the case. Fixes #5439.
1054 test("global ajaxSettings", function() {
1057 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1058 var orig = { url: "data/with_fries.xml" };
1061 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1063 t = jQuery.extend({}, orig);
1066 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1068 t = jQuery.extend({}, orig);
1069 t.data = { zoo: 'a', ping: 'b' };
1071 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' }" );
1073 jQuery.ajaxSettings = tmp;
1077 test("load(String)", function() {
1079 stop(); // check if load can be called with only url
1080 jQuery('#first').load("data/name.html", start);
1083 test("load('url selector')", function() {
1085 stop(); // check if load can be called with only url
1086 jQuery('#first').load("data/test3.html div.user", function(){
1087 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1092 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1095 jQuery.ajaxSetup({ dataType: "json" });
1096 jQuery("#first").ajaxComplete(function (e, xml, s) {
1097 equals( s.dataType, "html", "Verify the load() dataType was html" );
1098 jQuery("#first").unbind("ajaxComplete");
1099 jQuery.ajaxSetup({ dataType: "" });
1102 jQuery('#first').load("data/test3.html");
1105 test("load(String, Function) - simple: inject text into DOM", function() {
1108 jQuery('#first').load(url("data/name.html"), function() {
1109 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1114 test("load(String, Function) - check scripts", function() {
1118 var verifyEvaluation = function() {
1119 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1120 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1124 jQuery('#first').load(url('data/test.html'), function() {
1125 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1126 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1127 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1128 setTimeout(verifyEvaluation, 600);
1132 test("load(String, Function) - check file with only a script tag", function() {
1136 jQuery('#first').load(url('data/test2.html'), function() {
1137 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1138 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1144 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1147 jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1148 var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1149 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1150 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1151 jQuery.ajaxSetup({ dataFilter: 0 });
1156 test("load(String, Object, Function)", function() {
1160 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1161 var $post = jQuery(this).find('#post');
1162 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1163 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1168 test("load(String, String, Function)", function() {
1172 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1173 var $get = jQuery(this).find('#get');
1174 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1175 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1180 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1183 jQuery.get(url('data/dashboard.xml'), function(xml) {
1185 jQuery('tab', xml).each(function() {
1186 content.push(jQuery(this).text());
1188 equals( content[0], 'blabla', 'Check first tab');
1189 equals( content[1], 'blublu', 'Check second tab');
1194 test("jQuery.getScript(String, Function) - with callback", function() {
1197 jQuery.getScript(url("data/test.js"), function() {
1198 equals( foobar, "bar", 'Check if script was evaluated' );
1199 setTimeout(start, 100);
1203 test("jQuery.getScript(String, Function) - no callback", function() {
1206 jQuery.getScript(url("data/test.js"), function(){
1211 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1213 test("jQuery.ajax() - JSONP, " + label, function() {
1217 function plus(){ if ( ++count == 16 ) start(); }
1222 url: "data/jsonp.php",
1224 crossDomain: crossDomain,
1225 success: function(data){
1226 ok( data.data, "JSON results returned (GET, no callback)" );
1229 error: function(data){
1230 ok( false, "Ajax error JSON (GET, no callback)" );
1236 url: "data/jsonp.php?callback=?",
1238 crossDomain: crossDomain,
1239 success: function(data){
1240 ok( data.data, "JSON results returned (GET, url callback)" );
1243 error: function(data){
1244 ok( false, "Ajax error JSON (GET, url callback)" );
1250 url: "data/jsonp.php",
1252 crossDomain: crossDomain,
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: "data/jsonp.php?callback=??",
1267 crossDomain: crossDomain,
1268 success: function(data){
1269 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1272 error: function(data){
1273 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1279 url: "data/jsonp.php",
1281 crossDomain: crossDomain,
1282 data: "callback=??",
1283 success: function(data){
1284 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1287 error: function(data){
1288 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1294 url: "data/jsonp.php/??",
1296 crossDomain: crossDomain,
1297 success: function(data){
1298 ok( data.data, "JSON results returned (GET, REST-like)" );
1301 error: function(data){
1302 ok( false, "Ajax error JSON (GET, REST-like)" );
1308 url: "data/jsonp.php/???json=1",
1310 crossDomain: crossDomain,
1311 success: function(data){
1312 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1315 error: function(data){
1316 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1322 url: "data/jsonp.php",
1324 crossDomain: crossDomain,
1326 success: function(data){
1327 ok( data.data, "JSON results returned (GET, data obj callback)" );
1330 error: function(data){
1331 ok( false, "Ajax error JSON (GET, data obj callback)" );
1336 window.jsonpResults = function(data) {
1337 ok( data.data, "JSON results returned (GET, custom callback function)" );
1338 window.jsonpResults = undefined;
1343 url: "data/jsonp.php",
1345 crossDomain: crossDomain,
1346 jsonpCallback: "jsonpResults",
1347 success: function(data){
1348 ok( data.data, "JSON results returned (GET, custom callback name)" );
1351 error: function(data){
1352 ok( false, "Ajax error JSON (GET, custom callback name)" );
1359 url: "data/jsonp.php",
1361 crossDomain: crossDomain,
1362 success: function(data){
1363 ok( data.data, "JSON results returned (POST, no callback)" );
1366 error: function(data){
1367 ok( false, "Ajax error JSON (GET, data obj callback)" );
1374 url: "data/jsonp.php",
1377 crossDomain: crossDomain,
1378 success: function(data){
1379 ok( data.data, "JSON results returned (POST, data callback)" );
1382 error: function(data){
1383 ok( false, "Ajax error JSON (POST, data callback)" );
1390 url: "data/jsonp.php",
1393 crossDomain: crossDomain,
1394 success: function(data){
1395 ok( data.data, "JSON results returned (POST, data obj callback)" );
1398 error: function(data){
1399 ok( false, "Ajax error JSON (POST, data obj callback)" );
1406 url: "data/jsonp.php",
1408 crossDomain: crossDomain,
1409 beforeSend: function(){
1410 strictEqual( this.cache, false, "cache must be false on JSON request" );
1417 url: "data/jsonp.php?callback=XXX",
1420 jsonpCallback: "XXX",
1421 crossDomain: crossDomain,
1422 beforeSend: function() {
1423 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1424 "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1427 success: function(data){
1428 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1431 error: function(data){
1432 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1440 test("jQuery.ajax() - script, Remote", function() {
1443 var base = window.location.href.replace(/[^\/]*$/, "");
1448 url: base + "data/test.js",
1450 success: function(data){
1451 ok( foobar, "Script results returned (GET, no callback)" );
1457 test("jQuery.ajax() - script, Remote with POST", function() {
1460 var base = window.location.href.replace(/[^\/]*$/, "");
1465 url: base + "data/test.js",
1468 success: function(data, status){
1469 ok( foobar, "Script results returned (POST, no callback)" );
1470 equals( status, "success", "Script results returned (POST, no callback)" );
1473 error: function(xhr) {
1474 ok( false, "ajax error, status code: " + xhr.status );
1480 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1483 var base = window.location.href.replace(/[^\/]*$/, "");
1484 base = base.replace(/^.*?\/\//, "//");
1489 url: base + "data/test.js",
1491 success: function(data){
1492 ok( foobar, "Script results returned (GET, no callback)" );
1498 test("jQuery.ajax() - malformed JSON", function() {
1504 url: "data/badjson.js",
1506 success: function(){
1507 ok( false, "Success." );
1510 error: function(xhr, msg, detailedMsg) {
1511 equals( "parsererror", msg, "A parse error occurred." );
1512 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1518 test("jQuery.ajax() - script by content-type", function() {
1524 url: "data/script.php",
1525 data: { header: "script" },
1526 success: function() {
1532 test("jQuery.ajax() - json by content-type", function() {
1538 url: "data/json.php",
1539 data: { header: "json", json: "array" },
1540 success: function( json ) {
1541 ok( json.length >= 2, "Check length");
1542 equals( json[0].name, 'John', 'Check JSON: first, name' );
1543 equals( json[0].age, 21, 'Check JSON: first, age' );
1544 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1545 equals( json[1].age, 25, 'Check JSON: second, age' );
1551 test("jQuery.ajax() - json by content-type disabled with options", function() {
1557 url: url("data/json.php"),
1558 data: { header: "json", json: "array" },
1562 success: function( text ) {
1563 equals( typeof text , "string" , "json wasn't auto-determined" );
1564 var json = jQuery.parseJSON( text );
1565 ok( json.length >= 2, "Check length");
1566 equals( json[0].name, 'John', 'Check JSON: first, name' );
1567 equals( json[0].age, 21, 'Check JSON: first, age' );
1568 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1569 equals( json[1].age, 25, 'Check JSON: second, age' );
1575 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1578 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1579 ok( json.length >= 2, "Check length");
1580 equals( json[0].name, 'John', 'Check JSON: first, name' );
1581 equals( json[0].age, 21, 'Check JSON: first, age' );
1582 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1583 equals( json[1].age, 25, 'Check JSON: second, age' );
1588 test("jQuery.getJSON(String, Function) - JSON object", function() {
1591 jQuery.getJSON(url("data/json.php"), function(json) {
1592 if (json && json.data) {
1593 equals( json.data.lang, 'en', 'Check JSON: lang' );
1594 equals( json.data.length, 25, 'Check JSON: length' );
1600 test("jQuery.getJSON - Using Native JSON", function() {
1603 var old = window.JSON;
1605 parse: function(str){
1606 ok( true, "Verifying that parse method was run" );
1612 jQuery.getJSON(url("data/json.php"), function(json) {
1614 equals( json, true, "Verifying return value" );
1619 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1622 var base = window.location.href.replace(/[^\/]*$/, "");
1625 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1626 equals( json.data.lang, 'en', 'Check JSON: lang' );
1627 equals( json.data.length, 25, 'Check JSON: length' );
1632 test("jQuery.post - data", function() {
1636 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1637 jQuery('math', xml).each(function() {
1638 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1639 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1645 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1650 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1651 jQuery('math', xml).each(function() {
1652 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1653 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1655 if ( ++done === 2 ) start();
1658 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1659 jQuery('math', xml).each(function() {
1660 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1661 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1663 if ( ++done === 2 ) start();
1667 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1672 jQuery.ajaxSetup({timeout: 1000});
1674 var pass = function() {
1676 if ( passed == 2 ) {
1677 ok( true, 'Check local and global callbacks after timeout' );
1678 jQuery('#main').unbind("ajaxError");
1683 var fail = function(a,b,c) {
1684 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1688 jQuery('#main').ajaxError(pass);
1692 url: url("data/name.php?wait=5"),
1698 jQuery.ajaxSetup({timeout: 0});
1701 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1703 jQuery.ajaxSetup({timeout: 50});
1708 url: url("data/name.php?wait=1"),
1710 ok( false, 'Check for local timeout failed' );
1713 success: function() {
1714 ok( true, 'Check for local timeout' );
1720 jQuery.ajaxSetup({timeout: 0});
1723 test("jQuery.ajax - simple get", function() {
1728 url: url("data/name.php?name=foo"),
1729 success: function(msg){
1730 equals( msg, 'bar', 'Check for GET' );
1736 test("jQuery.ajax - simple post", function() {
1741 url: url("data/name.php"),
1743 success: function(msg){
1744 equals( msg, 'pan', 'Check for POST' );
1750 test("ajaxSetup()", function() {
1754 url: url("data/name.php?name=foo"),
1755 success: function(msg){
1756 equals( msg, 'bar', 'Check for GET' );
1764 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1767 url: "data/name.php?wait=1",
1769 error: function(request, status) {
1770 ok( status != null, "status shouldn't be null in error handler" );
1771 equals( "timeout", status );
1778 test("data option: evaluate function values (#2806)", function() {
1781 url: "data/echoQuery.php",
1787 success: function(result) {
1788 equals( result, "key=value" );
1794 test("data option: empty bodies for non-GET requests", function() {
1797 url: "data/echoData.php",
1800 success: function(result) {
1801 equals( result, "" );
1807 test("jQuery.ajax - If-Modified-Since support", function() {
1812 var url = "data/if_modified_since.php?ts=" + new Date();
1817 success: function(data, status) {
1818 equals(status, "success");
1823 success: function(data, status) {
1824 if ( data === "FAIL" ) {
1825 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1826 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1828 equals(status, "notmodified");
1829 ok(data == null, "response body should be empty");
1834 // Do this because opera simply refuses to implement 304 handling :(
1835 // A feature-driven way of detecting this would be appreciated
1836 // See: http://gist.github.com/599419
1837 ok(jQuery.browser.opera, "error");
1838 ok(jQuery.browser.opera, "error");
1844 equals(false, "error");
1845 // Do this because opera simply refuses to implement 304 handling :(
1846 // A feature-driven way of detecting this would be appreciated
1847 // See: http://gist.github.com/599419
1848 ok(jQuery.browser.opera, "error");
1854 test("jQuery.ajax - Etag support", function() {
1859 var url = "data/etag.php?ts=" + new Date();
1864 success: function(data, status) {
1865 equals(status, "success");
1870 success: function(data, status) {
1871 if ( data === "FAIL" ) {
1872 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1873 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1875 equals(status, "notmodified");
1876 ok(data == null, "response body should be empty");
1881 // Do this because opera simply refuses to implement 304 handling :(
1882 // A feature-driven way of detecting this would be appreciated
1883 // See: http://gist.github.com/599419
1884 ok(jQuery.browser.opera, "error");
1885 ok(jQuery.browser.opera, "error");
1891 // Do this because opera simply refuses to implement 304 handling :(
1892 // A feature-driven way of detecting this would be appreciated
1893 // See: http://gist.github.com/599419
1894 ok(jQuery.browser.opera, "error");
1900 test("jQuery ajax - failing cross-domain", function() {
1909 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1910 success: function(){ ok( false , "success" ); },
1911 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1912 complete: function() { if ( ! --i ) start(); }
1916 url: 'http://www.google.com',
1917 success: function(){ ok( false , "success" ); },
1918 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1919 complete: function() { if ( ! --i ) start(); }
1924 test("jQuery ajax - atom+xml", function() {
1929 url: url( 'data/atom+xml.php' ),
1930 success: function(){ ok( true , "success" ); },
1931 error: function(){ ok( false , "error" ); },
1932 complete: function() { start(); }
1937 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1938 var success = false;
1940 var xhr = jQuery.ajax({ url: window.location });
1945 ok( success, "document.location did not generate exception" );
1948 test( "jQuery.ajax - statusCode" , function() {
1955 function countComplete() {
1961 function createStatusCodes( name , isSuccess ) {
1962 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1965 ok( isSuccess , name );
1968 ok( ! isSuccess , name );
1974 "data/name.html": true,
1975 "data/someFileThatDoesNotExist.html": false
1976 } , function( uri , isSuccess ) {
1978 jQuery.ajax( url( uri ) , {
1979 statusCode: createStatusCodes( "in options" , isSuccess ),
1980 complete: countComplete
1983 jQuery.ajax( url( uri ) , {
1984 complete: countComplete
1985 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
1987 jQuery.ajax( url( uri ) , {
1988 complete: function(jXHR) {
1989 jXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
1994 jQuery.ajax( url( uri ) , {
1995 complete: function(jXHR) {
1996 setTimeout( function() {
1997 jXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2003 jQuery.ajax( url( uri ) , {
2004 statusCode: createStatusCodes( "all (options)" , isSuccess ),
2005 complete: function(jXHR) {
2006 jXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2007 setTimeout( function() {
2008 jXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2012 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2014 var testString = "";
2016 jQuery.ajax( url( uri ), {
2017 success: function( a , b , jXHR ) {
2018 ok( isSuccess , "success" );
2019 var statusCode = {};
2020 statusCode[ jXHR.status ] = function() {
2023 jXHR.statusCode( statusCode );
2026 error: function( jXHR ) {
2027 ok( ! isSuccess , "error" );
2028 var statusCode = {};
2029 statusCode[ jXHR.status ] = function() {
2032 jXHR.statusCode( statusCode );
2035 complete: function() {
2036 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2037 ( isSuccess ? "success" : "error" ) + " callbacks" );
2045 test("jQuery.ajax - transitive conversions", function() {
2053 jQuery.ajax( url("data/json.php") , {
2055 "json myjson": function( data ) {
2056 ok( true , "converter called" );
2061 success: function() {
2062 ok( true , "Transitive conversion worked" );
2063 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2064 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2068 jQuery.ajax( url("data/json.php") , {
2070 "json myjson": function( data ) {
2071 ok( true , "converter called (*)" );
2075 contents: false, /* headers are wrong so we ignore them */
2076 dataType: "* myjson",
2077 success: function() {
2078 ok( true , "Transitive conversion worked (*)" );
2079 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2080 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2084 ).then( start , start );
2088 test("jQuery.ajax - active counter", function() {
2089 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );