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 - multiple method signatures introduced in 1.5 ( #8107)", function() {
250 jQuery.ajax().success(function() { ok( true, 'With no arguments' ); }),
251 jQuery.ajax('data/name.html').success(function() { ok( true, 'With only string URL argument' ); }),
252 jQuery.ajax('data/name.html', {} ).success(function() { ok( true, 'With string URL param and map' ); }),
253 jQuery.ajax({ url: 'data/name.html'} ).success(function() { ok( true, 'With only map' ); })
254 ).then( start, start );
258 test("jQuery.ajax() - textStatus and errorThrown values", function() {
272 Safari 3.x returns "OK" instead of "Not Found"
273 Safari 4.x doesn't have this issue so the test should be re-instated once
274 we drop support for 3.x
277 url: url("data/nonExistingURL"),
278 error: function( _ , textStatus , errorThrown ){
279 strictEqual( textStatus, "error", "textStatus is 'error' for 404" );
280 strictEqual( errorThrown, "Not Found", "errorThrown is 'Not Found' for 404");
287 url: url("data/name.php?wait=5"),
288 error: function( _ , textStatus , errorThrown ){
289 strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
290 strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort");
296 url: url("data/name.php?wait=5"),
297 error: function( _ , textStatus , errorThrown ){
298 strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
299 strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')");
302 }).abort( "mystatus" );
305 test("jQuery.ajax() - responseText on error", function() {
312 url: url("data/errorWithText.php"),
313 error: function(xhr) {
314 strictEqual( xhr.responseText , "plain text message" , "Test jqXHR.responseText is filled for HTTP errors" );
316 complete: function() {
322 test(".ajax() - retry with jQuery.ajax( this )", function() {
331 url: url("data/errorWithText.php"),
337 ok( true , "Test retrying with jQuery.ajax(this) works" );
345 test(".ajax() - headers" , function() {
351 jQuery('#foo').ajaxSend(function( evt, xhr ) {
352 xhr.setRequestHeader( "ajax-send", "test" );
355 var requestHeaders = {
357 "SometHing-elsE": "other value",
358 OthEr: "something else"
363 for( i in requestHeaders ) {
366 list.push( "ajax-send" );
368 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
370 headers: requestHeaders,
371 success: function( data , _ , xhr ) {
373 for ( i in requestHeaders ) {
374 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
376 tmp.push( "ajax-send: test\n" );
377 tmp = tmp.join( "" );
379 strictEqual( data , tmp , "Headers were sent" );
380 strictEqual( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
381 if ( jQuery.browser.mozilla ) {
382 ok( true, "Firefox doesn't support empty headers" );
384 strictEqual( xhr.getResponseHeader( "Empty-Header" ) , "" , "Empty header received" );
386 strictEqual( xhr.getResponseHeader( "Sample-Header2" ) , "Hello World 2" , "Second sample header received" );
388 error: function(){ ok(false, "error"); }
390 }).then( start, start );
394 test(".ajax() - Accept header" , function() {
400 jQuery.ajax(url("data/headers.php?keys=accept"), {
402 Accept: "very wrong accept value"
404 beforeSend: function( xhr ) {
405 xhr.setRequestHeader( "Accept", "*/*" );
407 success: function( data ) {
408 strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
411 error: function(){ ok(false, "error"); }
416 test(".ajax() - contentType" , function() {
430 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
432 success: function( data ) {
433 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
435 complete: function() {
440 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
442 success: function( data ) {
443 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
445 complete: function() {
452 test(".ajax() - protocol-less urls", function() {
456 url: "//somedomain.com",
457 beforeSend: function( xhr, settings ) {
458 equals(settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added.");
464 test(".ajax() - hash", function() {
468 url: "data/name.html#foo",
469 beforeSend: function( xhr, settings ) {
470 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
476 url: "data/name.html?abc#foo",
477 beforeSend: function( xhr, settings ) {
478 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
484 url: "data/name.html?abc#foo",
485 data: { "test": 123 },
486 beforeSend: function( xhr, settings ) {
487 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
493 test("jQuery ajax - cross-domain detection", function() {
497 var loc = document.location,
498 otherPort = loc.port === 666 ? 667 : 666,
499 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
503 url: otherProtocol + "//" + loc.host,
504 beforeSend: function( _ , s ) {
505 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
512 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
513 beforeSend: function( _ , s ) {
514 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
521 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
522 beforeSend: function( _ , s ) {
523 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
530 url: loc.protocol + "//" + loc.host,
532 beforeSend: function( _ , s ) {
533 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
540 test(".load() - 404 error callbacks", function() {
544 jQuery('#foo').ajaxStart(function(){
545 ok( true, "ajaxStart" );
546 }).ajaxStop(function(){
547 ok( true, "ajaxStop" );
549 }).ajaxSend(function(){
550 ok( true, "ajaxSend" );
551 }).ajaxComplete(function(){
552 ok( true, "ajaxComplete" );
553 }).ajaxError(function(){
554 ok( true, "ajaxError" );
555 }).ajaxSuccess(function(){
556 ok( false, "ajaxSuccess" );
559 jQuery("<div/>").load("data/404.html", function(){
560 ok(true, "complete");
564 test("jQuery.ajax() - abort", function() {
568 jQuery('#foo').ajaxStart(function(){
569 ok( true, "ajaxStart" );
570 }).ajaxStop(function(){
571 ok( true, "ajaxStop" );
573 }).ajaxSend(function(){
574 ok( true, "ajaxSend" );
575 }).ajaxComplete(function(){
576 ok( true, "ajaxComplete" );
579 var xhr = jQuery.ajax({
580 url: url("data/name.php?wait=5"),
581 beforeSend: function(){ ok(true, "beforeSend"); },
582 complete: function(){ ok(true, "complete"); }
585 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
588 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
591 test("Ajax events with context", function() {
595 var context = document.createElement("div");
598 equals( this, context, e.type );
601 function callback(msg){
603 equals( this, context, "context is preserved on callback " + msg );
607 function nocallback(msg){
609 equals( typeof this.url, "string", "context is settings on callback " + msg );
613 jQuery('#foo').add(context)
620 url: url("data/name.html"),
621 beforeSend: callback("beforeSend"),
622 success: callback("success"),
623 error: callback("error"),
625 callback("complete").call(this);
628 url: url("data/404.html"),
630 beforeSend: callback("beforeSend"),
631 error: callback("error"),
632 complete: function(){
633 callback("complete").call(this);
635 jQuery('#foo').add(context).unbind();
638 url: url("data/404.html"),
639 beforeSend: nocallback("beforeSend"),
640 error: nocallback("error"),
641 complete: function(){
642 nocallback("complete").call(this);
653 test("jQuery.ajax context modification", function() {
661 url: url("data/name.html"),
663 beforeSend: function(){
666 complete: function() {
671 equals( obj.test, "foo", "Make sure the original object is maintained." );
674 test("jQuery.ajax context modification through ajaxSetup", function() {
685 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
688 url: url("data/name.html"),
689 complete: function() {
690 strictEqual( this, obj, "Make sure the original object is maintained." );
692 url: url("data/name.html"),
694 complete: function() {
695 ok( this !== obj, "Make sure overidding context is possible." );
700 url: url("data/name.html"),
701 beforeSend: function(){
704 complete: function() {
705 ok( this !== obj, "Make sure unsetting context is possible." );
715 test("jQuery.ajax() - disabled globals", function() {
719 jQuery('#foo').ajaxStart(function(){
720 ok( false, "ajaxStart" );
721 }).ajaxStop(function(){
722 ok( false, "ajaxStop" );
723 }).ajaxSend(function(){
724 ok( false, "ajaxSend" );
725 }).ajaxComplete(function(){
726 ok( false, "ajaxComplete" );
727 }).ajaxError(function(){
728 ok( false, "ajaxError" );
729 }).ajaxSuccess(function(){
730 ok( false, "ajaxSuccess" );
735 url: url("data/name.html"),
736 beforeSend: function(){ ok(true, "beforeSend"); },
737 success: function(){ ok(true, "success"); },
738 error: function(){ ok(false, "error"); },
739 complete: function(){
740 ok(true, "complete");
741 setTimeout(function(){ start(); }, 13);
746 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
750 url: url("data/with_fries.xml"),
752 success: function(resp) {
753 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
754 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
755 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
761 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
765 url: url("data/with_fries_over_jsonp.php"),
766 dataType: "jsonp xml",
767 success: function(resp) {
768 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
769 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
770 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
773 error: function(_1,_2,error) {
780 test("jQuery.ajax - HEAD requests", function() {
785 url: url("data/name.html"),
787 success: function(data, status, xhr){
788 var h = xhr.getAllResponseHeaders();
789 ok( /Date/i.test(h), 'No Date in HEAD response' );
792 url: url("data/name.html"),
793 data: { whip_it: "good" },
795 success: function(data, status, xhr){
796 var h = xhr.getAllResponseHeaders();
797 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
806 test("jQuery.ajax - beforeSend", function() {
812 jQuery.ajaxSetup({ timeout: 0 });
815 url: url("data/name.html"),
816 beforeSend: function(xml) {
819 success: function(data) {
820 ok( check, "check beforeSend was executed" );
826 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
828 var request = jQuery.ajax({
829 url: url("data/name.html"),
830 beforeSend: function() {
831 ok( true, "beforeSend got called, canceling" );
834 success: function() {
835 ok( false, "request didn't get canceled" );
837 complete: function() {
838 ok( false, "request didn't get canceled" );
841 ok( false, "request didn't get canceled" );
844 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
847 test("jQuery.ajax - beforeSend, cancel request manually", function() {
849 var request = jQuery.ajax({
850 url: url("data/name.html"),
851 beforeSend: function(xhr) {
852 ok( true, "beforeSend got called, canceling" );
855 success: function() {
856 ok( false, "request didn't get canceled" );
858 complete: function() {
859 ok( false, "request didn't get canceled" );
862 ok( false, "request didn't get canceled" );
865 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
868 window.foobar = null;
869 window.testFoo = undefined;
871 test("jQuery.ajax - dataType html", function() {
875 var verifyEvaluation = function() {
876 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
877 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
884 url: url("data/test.html"),
885 success: function(data) {
886 jQuery("#ap").html(data);
887 ok( data.match(/^html text/), 'Check content for datatype html' );
888 setTimeout(verifyEvaluation, 600);
893 test("serialize()", function() {
896 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
897 jQuery("#search").after(
898 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
899 '<input type="number" id="html5number" name="number" value="43" />'
902 equals( jQuery('#form').serialize(),
903 "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",
904 'Check form serialization as query string');
906 equals( jQuery('#form :input').serialize(),
907 "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",
908 'Check input serialization as query string');
910 equals( jQuery('#testForm').serialize(),
911 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
912 'Check form serialization as query string');
914 equals( jQuery('#testForm :input').serialize(),
915 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
916 'Check input serialization as query string');
918 equals( jQuery('#form, #testForm').serialize(),
919 "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=",
920 'Multiple form serialization as query string');
922 /* Temporarily disabled. Opera 10 has problems with form serialization.
923 equals( jQuery('#form, #testForm :input').serialize(),
924 "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=",
925 'Mixed form/input serialization as query string');
927 jQuery("#html5email, #html5number").remove();
930 test("jQuery.param()", function() {
933 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
935 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
936 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
938 params = {someName: [1, 2, 3], regularThing: "blah" };
939 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
941 params = {foo: ['a', 'b', 'c']};
942 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
944 params = {foo: ["baz", 42, "All your base are belong to us"] };
945 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
947 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
948 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" );
950 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?" };
951 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" );
953 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 ] };
954 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" );
956 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?" };
957 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" );
959 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." );
961 // Make sure empty arrays and objects are handled #6481
962 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
963 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
964 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
967 equals( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
969 jQuery.ajaxSetup({ traditional: true });
971 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
972 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
974 params = {someName: [1, 2, 3], regularThing: "blah" };
975 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
977 params = {foo: ['a', 'b', 'c']};
978 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
980 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
981 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
983 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
984 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" );
986 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?" };
987 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" );
989 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 ] };
990 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)" );
992 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?" };
993 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" );
995 params = { param1: null };
996 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
998 params = {'test': {'length': 3, 'foo': 'bar'} };
999 equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
1002 test("synchronous request", function() {
1004 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
1007 test("synchronous request with callbacks", function() {
1010 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1011 ok( /^{ "data"/.test( result ), "check returned text" );
1014 test("pass-through request object", function() {
1018 var target = "data/name.html";
1019 var successCount = 0;
1022 var success = function() {
1025 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1027 errorEx += ": " + xml.status;
1029 jQuery("#foo").one('ajaxStop', function () {
1030 equals(successCount, 5, "Check all ajax calls successful");
1031 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1032 jQuery("#foo").unbind('ajaxError');
1037 ok( jQuery.get(url(target), success), "get" );
1038 ok( jQuery.post(url(target), success), "post" );
1039 ok( jQuery.getScript(url("data/test.js"), success), "script" );
1040 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1041 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1044 test("ajax cache", function () {
1051 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1052 var re = /_=(.*?)(&|$)/g;
1054 for (var i = 0; i < 6; i++) {
1055 var ret = re.exec(s.url);
1061 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1062 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1067 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1068 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1069 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1070 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1071 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1072 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1077 * The assertions expect that the passed-in object will be modified,
1078 * which shouldn't be the case. Fixes #5439.
1079 test("global ajaxSettings", function() {
1082 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1083 var orig = { url: "data/with_fries.xml" };
1086 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1088 t = jQuery.extend({}, orig);
1091 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1093 t = jQuery.extend({}, orig);
1094 t.data = { zoo: 'a', ping: 'b' };
1096 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' }" );
1098 jQuery.ajaxSettings = tmp;
1102 test("load(String)", function() {
1104 stop(); // check if load can be called with only url
1105 jQuery('#first').load("data/name.html", start);
1108 test("load('url selector')", function() {
1110 stop(); // check if load can be called with only url
1111 jQuery('#first').load("data/test3.html div.user", function(){
1112 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1117 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1120 jQuery.ajaxSetup({ dataType: "json" });
1121 jQuery("#first").ajaxComplete(function (e, xml, s) {
1122 equals( s.dataType, "html", "Verify the load() dataType was html" );
1123 jQuery("#first").unbind("ajaxComplete");
1124 jQuery.ajaxSetup({ dataType: "" });
1127 jQuery('#first').load("data/test3.html");
1130 test("load(String, Function) - simple: inject text into DOM", function() {
1133 jQuery('#first').load(url("data/name.html"), function() {
1134 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1139 test("load(String, Function) - check scripts", function() {
1143 var verifyEvaluation = function() {
1144 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1145 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1149 jQuery('#first').load(url('data/test.html'), function() {
1150 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1151 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1152 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1153 setTimeout(verifyEvaluation, 600);
1157 test("load(String, Function) - check file with only a script tag", function() {
1161 jQuery('#first').load(url('data/test2.html'), function() {
1162 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1163 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1169 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1172 jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1173 var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1174 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1175 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1176 jQuery.ajaxSetup({ dataFilter: 0 });
1181 test("load(String, Object, Function)", function() {
1185 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1186 var $post = jQuery(this).find('#post');
1187 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1188 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1193 test("load(String, String, Function)", function() {
1197 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1198 var $get = jQuery(this).find('#get');
1199 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1200 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1205 test("jQuery.get(String, Function) - data in ajaxSettings (#8277)", function() {
1211 jQuery.get(url('data/echoQuery.php'), function(data) {
1212 ok( /helloworld$/.test( data ), 'Data from ajaxSettings was used');
1220 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1223 jQuery.get(url('data/dashboard.xml'), function(xml) {
1225 jQuery('tab', xml).each(function() {
1226 content.push(jQuery(this).text());
1228 equals( content[0], 'blabla', 'Check first tab');
1229 equals( content[1], 'blublu', 'Check second tab');
1234 test("jQuery.getScript(String, Function) - with callback", function() {
1237 jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1238 equals( foobar, "bar", 'Check if script was evaluated' );
1239 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1240 setTimeout(start, 100);
1244 test("jQuery.getScript(String, Function) - no callback", function() {
1247 jQuery.getScript(url("data/test.js"), function(){
1252 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1254 test("jQuery.ajax() - JSONP, " + label, function() {
1258 function plus(){ if ( ++count == 18 ) start(); }
1263 url: "data/jsonp.php",
1265 crossDomain: crossDomain,
1266 success: function(data){
1267 ok( data.data, "JSON results returned (GET, no callback)" );
1270 error: function(data){
1271 ok( false, "Ajax error JSON (GET, no callback)" );
1277 url: "data/jsonp.php?callback=?",
1279 crossDomain: crossDomain,
1280 success: function(data){
1281 ok( data.data, "JSON results returned (GET, url callback)" );
1284 error: function(data){
1285 ok( false, "Ajax error JSON (GET, url callback)" );
1291 url: "data/jsonp.php",
1293 crossDomain: crossDomain,
1295 success: function(data){
1296 ok( data.data, "JSON results returned (GET, data callback)" );
1299 error: function(data){
1300 ok( false, "Ajax error JSON (GET, data callback)" );
1306 url: "data/jsonp.php?callback=??",
1308 crossDomain: crossDomain,
1309 success: function(data){
1310 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1313 error: function(data){
1314 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1320 url: "data/jsonp.php",
1322 crossDomain: crossDomain,
1323 data: "callback=??",
1324 success: function(data){
1325 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1328 error: function(data){
1329 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1335 url: "data/jsonp.php/??",
1337 crossDomain: crossDomain,
1338 success: function(data){
1339 ok( data.data, "JSON results returned (GET, REST-like)" );
1342 error: function(data){
1343 ok( false, "Ajax error JSON (GET, REST-like)" );
1349 url: "data/jsonp.php/???json=1",
1351 crossDomain: crossDomain,
1352 success: function(data){
1353 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1356 error: function(data){
1357 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1363 url: "data/jsonp.php",
1365 crossDomain: crossDomain,
1367 success: function(data){
1368 ok( data.data, "JSON results returned (GET, data obj callback)" );
1371 error: function(data){
1372 ok( false, "Ajax error JSON (GET, data obj callback)" );
1377 window.jsonpResults = function(data) {
1378 ok( data.data, "JSON results returned (GET, custom callback function)" );
1379 window.jsonpResults = undefined;
1384 url: "data/jsonp.php",
1386 crossDomain: crossDomain,
1387 jsonpCallback: "jsonpResults",
1388 success: function(data){
1389 ok( data.data, "JSON results returned (GET, custom callback name)" );
1392 error: function(data){
1393 ok( false, "Ajax error JSON (GET, custom callback name)" );
1399 url: "data/jsonp.php",
1401 crossDomain: crossDomain,
1402 jsonpCallback: "functionToCleanUp",
1403 success: function(data){
1404 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1405 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1409 url: "data/jsonp.php",
1411 crossDomain: crossDomain,
1412 jsonpCallback: "functionToCleanUp",
1413 beforeSend: function( jqXHR ) {
1418 xhr.error(function() {
1419 ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1420 strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1424 error: function(data){
1425 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1432 url: "data/jsonp.php",
1434 crossDomain: crossDomain,
1435 success: function(data){
1436 ok( data.data, "JSON results returned (POST, no callback)" );
1439 error: function(data){
1440 ok( false, "Ajax error JSON (GET, data obj callback)" );
1447 url: "data/jsonp.php",
1450 crossDomain: crossDomain,
1451 success: function(data){
1452 ok( data.data, "JSON results returned (POST, data callback)" );
1455 error: function(data){
1456 ok( false, "Ajax error JSON (POST, data callback)" );
1463 url: "data/jsonp.php",
1466 crossDomain: crossDomain,
1467 success: function(data){
1468 ok( data.data, "JSON results returned (POST, data obj callback)" );
1471 error: function(data){
1472 ok( false, "Ajax error JSON (POST, data obj callback)" );
1479 url: "data/jsonp.php",
1481 crossDomain: crossDomain,
1482 beforeSend: function(){
1483 strictEqual( this.cache, false, "cache must be false on JSON request" );
1490 url: "data/jsonp.php?callback=XXX",
1493 jsonpCallback: "XXX",
1494 crossDomain: crossDomain,
1495 beforeSend: function() {
1496 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1497 "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1500 success: function(data){
1501 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1504 error: function(data){
1505 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1513 test("jQuery.ajax() - script, Remote", function() {
1516 var base = window.location.href.replace(/[^\/]*$/, "");
1521 url: base + "data/test.js",
1523 success: function(data){
1524 ok( foobar, "Script results returned (GET, no callback)" );
1530 test("jQuery.ajax() - script, Remote with POST", function() {
1533 var base = window.location.href.replace(/[^\/]*$/, "");
1538 url: base + "data/test.js",
1541 success: function(data, status){
1542 ok( foobar, "Script results returned (POST, no callback)" );
1543 equals( status, "success", "Script results returned (POST, no callback)" );
1546 error: function(xhr) {
1547 ok( false, "ajax error, status code: " + xhr.status );
1553 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1556 var base = window.location.href.replace(/[^\/]*$/, "");
1557 base = base.replace(/^.*?\/\//, "//");
1562 url: base + "data/test.js",
1564 success: function(data){
1565 ok( foobar, "Script results returned (GET, no callback)" );
1571 test("jQuery.ajax() - malformed JSON", function() {
1577 url: "data/badjson.js",
1579 success: function(){
1580 ok( false, "Success." );
1583 error: function(xhr, msg, detailedMsg) {
1584 equals( "parsererror", msg, "A parse error occurred." );
1585 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1591 test("jQuery.ajax() - script by content-type", function() {
1599 url: "data/script.php",
1600 data: { header: "script" }
1604 url: "data/script.php",
1605 data: { header: "ecma" }
1608 ).then( start, start );
1611 test("jQuery.ajax() - json by content-type", function() {
1617 url: "data/json.php",
1618 data: { header: "json", json: "array" },
1619 success: function( json ) {
1620 ok( json.length >= 2, "Check length");
1621 equals( json[0].name, 'John', 'Check JSON: first, name' );
1622 equals( json[0].age, 21, 'Check JSON: first, age' );
1623 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1624 equals( json[1].age, 25, 'Check JSON: second, age' );
1630 test("jQuery.ajax() - json by content-type disabled with options", function() {
1636 url: url("data/json.php"),
1637 data: { header: "json", json: "array" },
1641 success: function( text ) {
1642 equals( typeof text , "string" , "json wasn't auto-determined" );
1643 var json = jQuery.parseJSON( text );
1644 ok( json.length >= 2, "Check length");
1645 equals( json[0].name, 'John', 'Check JSON: first, name' );
1646 equals( json[0].age, 21, 'Check JSON: first, age' );
1647 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1648 equals( json[1].age, 25, 'Check JSON: second, age' );
1654 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1657 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1658 ok( json.length >= 2, "Check length");
1659 equals( json[0].name, 'John', 'Check JSON: first, name' );
1660 equals( json[0].age, 21, 'Check JSON: first, age' );
1661 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1662 equals( json[1].age, 25, 'Check JSON: second, age' );
1667 test("jQuery.getJSON(String, Function) - JSON object", function() {
1670 jQuery.getJSON(url("data/json.php"), function(json) {
1671 if (json && json.data) {
1672 equals( json.data.lang, 'en', 'Check JSON: lang' );
1673 equals( json.data.length, 25, 'Check JSON: length' );
1679 test("jQuery.getJSON - Using Native JSON", function() {
1682 var old = window.JSON;
1684 parse: function(str){
1685 ok( true, "Verifying that parse method was run" );
1691 jQuery.getJSON(url("data/json.php"), function(json) {
1693 equals( json, true, "Verifying return value" );
1698 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1701 var base = window.location.href.replace(/[^\/]*$/, "");
1704 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1705 equals( json.data.lang, 'en', 'Check JSON: lang' );
1706 equals( json.data.length, 25, 'Check JSON: length' );
1711 test("jQuery.post - data", 3, function() {
1715 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1716 jQuery( 'math', xml ).each( function() {
1717 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1718 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1723 url: url('data/echoData.php'),
1731 success: function( data ) {
1732 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1735 ).then( start, start );
1739 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1744 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1745 jQuery('math', xml).each(function() {
1746 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1747 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1749 if ( ++done === 2 ) start();
1752 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1753 jQuery('math', xml).each(function() {
1754 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1755 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1757 if ( ++done === 2 ) start();
1761 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1766 jQuery.ajaxSetup({timeout: 1000});
1768 var pass = function() {
1770 if ( passed == 2 ) {
1771 ok( true, 'Check local and global callbacks after timeout' );
1772 jQuery('#main').unbind("ajaxError");
1777 var fail = function(a,b,c) {
1778 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1782 jQuery('#main').ajaxError(pass);
1786 url: url("data/name.php?wait=5"),
1792 jQuery.ajaxSetup({timeout: 0});
1795 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1797 jQuery.ajaxSetup({timeout: 50});
1802 url: url("data/name.php?wait=1"),
1804 ok( false, 'Check for local timeout failed' );
1807 success: function() {
1808 ok( true, 'Check for local timeout' );
1814 jQuery.ajaxSetup({timeout: 0});
1817 test("jQuery.ajax - simple get", function() {
1822 url: url("data/name.php?name=foo"),
1823 success: function(msg){
1824 equals( msg, 'bar', 'Check for GET' );
1830 test("jQuery.ajax - simple post", function() {
1835 url: url("data/name.php"),
1837 success: function(msg){
1838 equals( msg, 'pan', 'Check for POST' );
1844 test("ajaxSetup()", function() {
1848 url: url("data/name.php?name=foo"),
1849 success: function(msg){
1850 equals( msg, 'bar', 'Check for GET' );
1858 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1861 url: "data/name.php?wait=1",
1863 error: function(request, status) {
1864 ok( status != null, "status shouldn't be null in error handler" );
1865 equals( "timeout", status );
1872 test("data option: evaluate function values (#2806)", function() {
1875 url: "data/echoQuery.php",
1881 success: function(result) {
1882 equals( result, "key=value" );
1888 test("data option: empty bodies for non-GET requests", function() {
1891 url: "data/echoData.php",
1894 success: function(result) {
1895 equals( result, "" );
1901 var ifModifiedNow = new Date();
1903 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1905 test("jQuery.ajax - If-Modified-Since support" + label, function() {
1910 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1916 success: function(data, status) {
1917 equals(status, "success" );
1923 success: function(data, status) {
1924 if ( data === "FAIL" ) {
1925 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1926 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1928 equals(status, "notmodified");
1929 ok(data == null, "response body should be empty");
1934 // Do this because opera simply refuses to implement 304 handling :(
1935 // A feature-driven way of detecting this would be appreciated
1936 // See: http://gist.github.com/599419
1937 ok(jQuery.browser.opera, "error");
1938 ok(jQuery.browser.opera, "error");
1944 equals(false, "error");
1945 // Do this because opera simply refuses to implement 304 handling :(
1946 // A feature-driven way of detecting this would be appreciated
1947 // See: http://gist.github.com/599419
1948 ok(jQuery.browser.opera, "error");
1954 test("jQuery.ajax - Etag support" + label, function() {
1959 var url = "data/etag.php?ts=" + ifModifiedNow++;
1965 success: function(data, status) {
1966 equals(status, "success" );
1972 success: function(data, status) {
1973 if ( data === "FAIL" ) {
1974 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1975 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1977 equals(status, "notmodified");
1978 ok(data == null, "response body should be empty");
1983 // Do this because opera simply refuses to implement 304 handling :(
1984 // A feature-driven way of detecting this would be appreciated
1985 // See: http://gist.github.com/599419
1986 ok(jQuery.browser.opera, "error");
1987 ok(jQuery.browser.opera, "error");
1993 // Do this because opera simply refuses to implement 304 handling :(
1994 // A feature-driven way of detecting this would be appreciated
1995 // See: http://gist.github.com/599419
1996 ok(jQuery.browser.opera, "error");
2003 test("jQuery ajax - failing cross-domain", function() {
2012 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
2013 success: function(){ ok( false , "success" ); },
2014 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
2015 complete: function() { if ( ! --i ) start(); }
2019 url: 'http://www.google.com',
2020 success: function(){ ok( false , "success" ); },
2021 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
2022 complete: function() { if ( ! --i ) start(); }
2027 test("jQuery ajax - atom+xml", function() {
2032 url: url( 'data/atom+xml.php' ),
2033 success: function(){ ok( true , "success" ); },
2034 error: function(){ ok( false , "error" ); },
2035 complete: function() { start(); }
2040 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2041 var success = false;
2043 var xhr = jQuery.ajax({ url: window.location });
2048 ok( success, "document.location did not generate exception" );
2051 test( "jQuery.ajax - statusCode" , function() {
2058 function countComplete() {
2064 function createStatusCodes( name , isSuccess ) {
2065 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2068 ok( isSuccess , name );
2071 ok( ! isSuccess , name );
2077 "data/name.html": true,
2078 "data/someFileThatDoesNotExist.html": false
2079 } , function( uri , isSuccess ) {
2081 jQuery.ajax( url( uri ) , {
2082 statusCode: createStatusCodes( "in options" , isSuccess ),
2083 complete: countComplete
2086 jQuery.ajax( url( uri ) , {
2087 complete: countComplete
2088 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2090 jQuery.ajax( url( uri ) , {
2091 complete: function(jqXHR) {
2092 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2097 jQuery.ajax( url( uri ) , {
2098 complete: function(jqXHR) {
2099 setTimeout( function() {
2100 jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2106 jQuery.ajax( url( uri ) , {
2107 statusCode: createStatusCodes( "all (options)" , isSuccess ),
2108 complete: function(jqXHR) {
2109 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2110 setTimeout( function() {
2111 jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2115 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2117 var testString = "";
2119 jQuery.ajax( url( uri ), {
2120 success: function( a , b , jqXHR ) {
2121 ok( isSuccess , "success" );
2122 var statusCode = {};
2123 statusCode[ jqXHR.status ] = function() {
2126 jqXHR.statusCode( statusCode );
2129 error: function( jqXHR ) {
2130 ok( ! isSuccess , "error" );
2131 var statusCode = {};
2132 statusCode[ jqXHR.status ] = function() {
2135 jqXHR.statusCode( statusCode );
2138 complete: function() {
2139 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2140 ( isSuccess ? "success" : "error" ) + " callbacks" );
2148 test("jQuery.ajax - transitive conversions", function() {
2156 jQuery.ajax( url("data/json.php") , {
2158 "json myJson": function( data ) {
2159 ok( true , "converter called" );
2164 success: function() {
2165 ok( true , "Transitive conversion worked" );
2166 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2167 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2171 jQuery.ajax( url("data/json.php") , {
2173 "json myJson": function( data ) {
2174 ok( true , "converter called (*)" );
2178 contents: false, /* headers are wrong so we ignore them */
2179 dataType: "* myJson",
2180 success: function() {
2181 ok( true , "Transitive conversion worked (*)" );
2182 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2183 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2187 ).then( start , start );
2191 test("jQuery.ajax - overrideMimeType", function() {
2199 jQuery.ajax( url("data/json.php") , {
2200 beforeSend: function( xhr ) {
2201 xhr.overrideMimeType( "application/json" );
2203 success: function( json ) {
2204 ok( json.data , "Mimetype overriden using beforeSend" );
2208 jQuery.ajax( url("data/json.php") , {
2209 mimeType: "application/json",
2210 success: function( json ) {
2211 ok( json.data , "Mimetype overriden using mimeType option" );
2215 ).then( start , start );
2219 test("jQuery.ajax - abort in prefilter", function() {
2223 jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
2224 if ( options.abortInPrefilter ) {
2229 strictEqual( jQuery.ajax({
2230 abortInPrefilter: true,
2232 ok( false, "error callback called" );
2234 }), false, "Request was properly aborted early by the prefilter" );
2238 test("jQuery.ajax - active counter", function() {
2239 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );