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" );
513 beforeSend: function( _ , s ) {
514 ok( s.crossDomain , "Adobe AIR app:/ URL detected as cross-domain" );
521 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
522 beforeSend: function( _ , s ) {
523 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
530 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
531 beforeSend: function( _ , s ) {
532 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
540 beforeSend: function( _ , s ) {
541 ok( s.crossDomain , "Test about:blank is detected as cross-domain" );
548 url: loc.protocol + "//" + loc.host,
550 beforeSend: function( _ , s ) {
551 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
558 test(".load() - 404 error callbacks", function() {
562 jQuery('#foo').ajaxStart(function(){
563 ok( true, "ajaxStart" );
564 }).ajaxStop(function(){
565 ok( true, "ajaxStop" );
567 }).ajaxSend(function(){
568 ok( true, "ajaxSend" );
569 }).ajaxComplete(function(){
570 ok( true, "ajaxComplete" );
571 }).ajaxError(function(){
572 ok( true, "ajaxError" );
573 }).ajaxSuccess(function(){
574 ok( false, "ajaxSuccess" );
577 jQuery("<div/>").load("data/404.html", function(){
578 ok(true, "complete");
582 test("jQuery.ajax() - abort", function() {
586 jQuery('#foo').ajaxStart(function(){
587 ok( true, "ajaxStart" );
588 }).ajaxStop(function(){
589 ok( true, "ajaxStop" );
591 }).ajaxSend(function(){
592 ok( true, "ajaxSend" );
593 }).ajaxComplete(function(){
594 ok( true, "ajaxComplete" );
597 var xhr = jQuery.ajax({
598 url: url("data/name.php?wait=5"),
599 beforeSend: function(){ ok(true, "beforeSend"); },
600 complete: function(){ ok(true, "complete"); }
603 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
606 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
609 test("Ajax events with context", function() {
613 var context = document.createElement("div");
616 equals( this, context, e.type );
619 function callback(msg){
621 equals( this, context, "context is preserved on callback " + msg );
625 function nocallback(msg){
627 equals( typeof this.url, "string", "context is settings on callback " + msg );
631 jQuery('#foo').add(context)
638 url: url("data/name.html"),
639 beforeSend: callback("beforeSend"),
640 success: callback("success"),
641 error: callback("error"),
643 callback("complete").call(this);
646 url: url("data/404.html"),
648 beforeSend: callback("beforeSend"),
649 error: callback("error"),
650 complete: function(){
651 callback("complete").call(this);
653 jQuery('#foo').add(context).unbind();
656 url: url("data/404.html"),
657 beforeSend: nocallback("beforeSend"),
658 error: nocallback("error"),
659 complete: function(){
660 nocallback("complete").call(this);
671 test("jQuery.ajax context modification", function() {
679 url: url("data/name.html"),
681 beforeSend: function(){
684 complete: function() {
689 equals( obj.test, "foo", "Make sure the original object is maintained." );
692 test("jQuery.ajax context modification through ajaxSetup", function() {
703 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
706 url: url("data/name.html"),
707 complete: function() {
708 strictEqual( this, obj, "Make sure the original object is maintained." );
710 url: url("data/name.html"),
712 complete: function() {
713 ok( this !== obj, "Make sure overidding context is possible." );
718 url: url("data/name.html"),
719 beforeSend: function(){
722 complete: function() {
723 ok( this !== obj, "Make sure unsetting context is possible." );
733 test("jQuery.ajax() - disabled globals", function() {
737 jQuery('#foo').ajaxStart(function(){
738 ok( false, "ajaxStart" );
739 }).ajaxStop(function(){
740 ok( false, "ajaxStop" );
741 }).ajaxSend(function(){
742 ok( false, "ajaxSend" );
743 }).ajaxComplete(function(){
744 ok( false, "ajaxComplete" );
745 }).ajaxError(function(){
746 ok( false, "ajaxError" );
747 }).ajaxSuccess(function(){
748 ok( false, "ajaxSuccess" );
753 url: url("data/name.html"),
754 beforeSend: function(){ ok(true, "beforeSend"); },
755 success: function(){ ok(true, "success"); },
756 error: function(){ ok(false, "error"); },
757 complete: function(){
758 ok(true, "complete");
759 setTimeout(function(){ start(); }, 13);
764 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
768 url: url("data/with_fries.xml"),
770 success: function(resp) {
771 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
772 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
773 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
779 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
783 url: url("data/with_fries_over_jsonp.php"),
784 dataType: "jsonp xml",
785 success: function(resp) {
786 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
787 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
788 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
791 error: function(_1,_2,error) {
798 test("jQuery.ajax - HEAD requests", function() {
803 url: url("data/name.html"),
805 success: function(data, status, xhr){
806 var h = xhr.getAllResponseHeaders();
807 ok( /Date/i.test(h), 'No Date in HEAD response' );
810 url: url("data/name.html"),
811 data: { whip_it: "good" },
813 success: function(data, status, xhr){
814 var h = xhr.getAllResponseHeaders();
815 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
824 test("jQuery.ajax - beforeSend", function() {
830 jQuery.ajaxSetup({ timeout: 0 });
833 url: url("data/name.html"),
834 beforeSend: function(xml) {
837 success: function(data) {
838 ok( check, "check beforeSend was executed" );
844 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
846 var request = jQuery.ajax({
847 url: url("data/name.html"),
848 beforeSend: function() {
849 ok( true, "beforeSend got called, canceling" );
852 success: function() {
853 ok( false, "request didn't get canceled" );
855 complete: function() {
856 ok( false, "request didn't get canceled" );
859 ok( false, "request didn't get canceled" );
862 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
865 test("jQuery.ajax - beforeSend, cancel request manually", function() {
867 var request = jQuery.ajax({
868 url: url("data/name.html"),
869 beforeSend: function(xhr) {
870 ok( true, "beforeSend got called, canceling" );
873 success: function() {
874 ok( false, "request didn't get canceled" );
876 complete: function() {
877 ok( false, "request didn't get canceled" );
880 ok( false, "request didn't get canceled" );
883 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
886 window.foobar = null;
887 window.testFoo = undefined;
889 test("jQuery.ajax - dataType html", function() {
893 var verifyEvaluation = function() {
894 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
895 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
902 url: url("data/test.html"),
903 success: function(data) {
904 jQuery("#ap").html(data);
905 ok( data.match(/^html text/), 'Check content for datatype html' );
906 setTimeout(verifyEvaluation, 600);
911 test("serialize()", function() {
914 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
915 jQuery("#search").after(
916 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
917 '<input type="number" id="html5number" name="number" value="43" />'
920 equals( jQuery('#form').serialize(),
921 "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",
922 'Check form serialization as query string');
924 equals( jQuery('#form :input').serialize(),
925 "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",
926 'Check input serialization as query string');
928 equals( jQuery('#testForm').serialize(),
929 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
930 'Check form serialization as query string');
932 equals( jQuery('#testForm :input').serialize(),
933 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
934 'Check input serialization as query string');
936 equals( jQuery('#form, #testForm').serialize(),
937 "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=",
938 'Multiple form serialization as query string');
940 /* Temporarily disabled. Opera 10 has problems with form serialization.
941 equals( jQuery('#form, #testForm :input').serialize(),
942 "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=",
943 'Mixed form/input serialization as query string');
945 jQuery("#html5email, #html5number").remove();
948 test("jQuery.param()", function() {
951 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
953 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
954 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
956 params = {someName: [1, 2, 3], regularThing: "blah" };
957 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
959 params = {foo: ['a', 'b', 'c']};
960 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
962 params = {foo: ["baz", 42, "All your base are belong to us"] };
963 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
965 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
966 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" );
968 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?" };
969 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" );
971 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 ] };
972 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" );
974 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?" };
975 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" );
977 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." );
979 // Make sure empty arrays and objects are handled #6481
980 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
981 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
982 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
985 equals( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
987 jQuery.ajaxSetup({ traditional: true });
989 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
990 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
992 params = {someName: [1, 2, 3], regularThing: "blah" };
993 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
995 params = {foo: ['a', 'b', 'c']};
996 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
998 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
999 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
1001 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
1002 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" );
1004 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?" };
1005 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" );
1007 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 ] };
1008 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)" );
1010 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?" };
1011 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" );
1013 params = { param1: null };
1014 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
1016 params = {'test': {'length': 3, 'foo': 'bar'} };
1017 equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
1020 test("synchronous request", function() {
1022 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
1025 test("synchronous request with callbacks", function() {
1028 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1029 ok( /^{ "data"/.test( result ), "check returned text" );
1032 test("pass-through request object", function() {
1036 var target = "data/name.html";
1037 var successCount = 0;
1040 var success = function() {
1043 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1045 errorEx += ": " + xml.status;
1047 jQuery("#foo").one('ajaxStop', function () {
1048 equals(successCount, 5, "Check all ajax calls successful");
1049 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1050 jQuery("#foo").unbind('ajaxError');
1055 ok( jQuery.get(url(target), success), "get" );
1056 ok( jQuery.post(url(target), success), "post" );
1057 ok( jQuery.getScript(url("data/test.js"), success), "script" );
1058 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1059 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1062 test("ajax cache", function () {
1069 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1070 var re = /_=(.*?)(&|$)/g;
1072 for (var i = 0; i < 6; i++) {
1073 var ret = re.exec(s.url);
1079 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1080 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1085 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1086 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1087 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1088 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1089 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1090 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1095 * The assertions expect that the passed-in object will be modified,
1096 * which shouldn't be the case. Fixes #5439.
1097 test("global ajaxSettings", function() {
1100 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1101 var orig = { url: "data/with_fries.xml" };
1104 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1106 t = jQuery.extend({}, orig);
1109 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1111 t = jQuery.extend({}, orig);
1112 t.data = { zoo: 'a', ping: 'b' };
1114 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' }" );
1116 jQuery.ajaxSettings = tmp;
1120 test("load(String)", function() {
1122 stop(); // check if load can be called with only url
1123 jQuery('#first').load("data/name.html", start);
1126 test("load('url selector')", function() {
1128 stop(); // check if load can be called with only url
1129 jQuery('#first').load("data/test3.html div.user", function(){
1130 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1135 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1138 jQuery.ajaxSetup({ dataType: "json" });
1139 jQuery("#first").ajaxComplete(function (e, xml, s) {
1140 equals( s.dataType, "html", "Verify the load() dataType was html" );
1141 jQuery("#first").unbind("ajaxComplete");
1142 jQuery.ajaxSetup({ dataType: "" });
1145 jQuery('#first').load("data/test3.html");
1148 test("load(String, Function) - simple: inject text into DOM", function() {
1151 jQuery('#first').load(url("data/name.html"), function() {
1152 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1157 test("load(String, Function) - check scripts", function() {
1161 var verifyEvaluation = function() {
1162 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1163 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1167 jQuery('#first').load(url('data/test.html'), function() {
1168 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1169 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1170 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1171 setTimeout(verifyEvaluation, 600);
1175 test("load(String, Function) - check file with only a script tag", function() {
1179 jQuery('#first').load(url('data/test2.html'), function() {
1180 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1181 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1187 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1190 jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1191 var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1192 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1193 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1194 jQuery.ajaxSetup({ dataFilter: 0 });
1199 test("load(String, Object, Function)", function() {
1203 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1204 var $post = jQuery(this).find('#post');
1205 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1206 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1211 test("load(String, String, Function)", function() {
1215 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1216 var $get = jQuery(this).find('#get');
1217 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1218 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1223 test("jQuery.get(String, Function) - data in ajaxSettings (#8277)", function() {
1229 jQuery.get(url('data/echoQuery.php'), function(data) {
1230 ok( /helloworld$/.test( data ), 'Data from ajaxSettings was used');
1238 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1241 jQuery.get(url('data/dashboard.xml'), function(xml) {
1243 jQuery('tab', xml).each(function() {
1244 content.push(jQuery(this).text());
1246 equals( content[0], 'blabla', 'Check first tab');
1247 equals( content[1], 'blublu', 'Check second tab');
1252 test("jQuery.getScript(String, Function) - with callback", function() {
1255 jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1256 equals( foobar, "bar", 'Check if script was evaluated' );
1257 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1258 setTimeout(start, 100);
1262 test("jQuery.getScript(String, Function) - no callback", function() {
1265 jQuery.getScript(url("data/test.js"), function(){
1270 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1272 test("jQuery.ajax() - JSONP, " + label, function() {
1276 function plus(){ if ( ++count == 18 ) start(); }
1281 url: "data/jsonp.php",
1283 crossDomain: crossDomain,
1284 success: function(data){
1285 ok( data.data, "JSON results returned (GET, no callback)" );
1288 error: function(data){
1289 ok( false, "Ajax error JSON (GET, no callback)" );
1295 url: "data/jsonp.php?callback=?",
1297 crossDomain: crossDomain,
1298 success: function(data){
1299 ok( data.data, "JSON results returned (GET, url callback)" );
1302 error: function(data){
1303 ok( false, "Ajax error JSON (GET, url callback)" );
1309 url: "data/jsonp.php",
1311 crossDomain: crossDomain,
1313 success: function(data){
1314 ok( data.data, "JSON results returned (GET, data callback)" );
1317 error: function(data){
1318 ok( false, "Ajax error JSON (GET, data callback)" );
1324 url: "data/jsonp.php?callback=??",
1326 crossDomain: crossDomain,
1327 success: function(data){
1328 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1331 error: function(data){
1332 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1338 url: "data/jsonp.php",
1340 crossDomain: crossDomain,
1341 data: "callback=??",
1342 success: function(data){
1343 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1346 error: function(data){
1347 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1353 url: "data/jsonp.php/??",
1355 crossDomain: crossDomain,
1356 success: function(data){
1357 ok( data.data, "JSON results returned (GET, REST-like)" );
1360 error: function(data){
1361 ok( false, "Ajax error JSON (GET, REST-like)" );
1367 url: "data/jsonp.php/???json=1",
1369 crossDomain: crossDomain,
1370 success: function(data){
1371 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1374 error: function(data){
1375 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1381 url: "data/jsonp.php",
1383 crossDomain: crossDomain,
1385 success: function(data){
1386 ok( data.data, "JSON results returned (GET, data obj callback)" );
1389 error: function(data){
1390 ok( false, "Ajax error JSON (GET, data obj callback)" );
1395 window.jsonpResults = function(data) {
1396 ok( data.data, "JSON results returned (GET, custom callback function)" );
1397 window.jsonpResults = undefined;
1402 url: "data/jsonp.php",
1404 crossDomain: crossDomain,
1405 jsonpCallback: "jsonpResults",
1406 success: function(data){
1407 ok( data.data, "JSON results returned (GET, custom callback name)" );
1410 error: function(data){
1411 ok( false, "Ajax error JSON (GET, custom callback name)" );
1417 url: "data/jsonp.php",
1419 crossDomain: crossDomain,
1420 jsonpCallback: "functionToCleanUp",
1421 success: function(data){
1422 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1423 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1427 url: "data/jsonp.php",
1429 crossDomain: crossDomain,
1430 jsonpCallback: "functionToCleanUp",
1431 beforeSend: function( jqXHR ) {
1436 xhr.error(function() {
1437 ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1438 strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1442 error: function(data){
1443 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1450 url: "data/jsonp.php",
1452 crossDomain: crossDomain,
1453 success: function(data){
1454 ok( data.data, "JSON results returned (POST, no callback)" );
1457 error: function(data){
1458 ok( false, "Ajax error JSON (GET, data obj callback)" );
1465 url: "data/jsonp.php",
1468 crossDomain: crossDomain,
1469 success: function(data){
1470 ok( data.data, "JSON results returned (POST, data callback)" );
1473 error: function(data){
1474 ok( false, "Ajax error JSON (POST, data callback)" );
1481 url: "data/jsonp.php",
1484 crossDomain: crossDomain,
1485 success: function(data){
1486 ok( data.data, "JSON results returned (POST, data obj callback)" );
1489 error: function(data){
1490 ok( false, "Ajax error JSON (POST, data obj callback)" );
1497 url: "data/jsonp.php",
1499 crossDomain: crossDomain,
1500 beforeSend: function(){
1501 strictEqual( this.cache, false, "cache must be false on JSON request" );
1508 url: "data/jsonp.php?callback=XXX",
1511 jsonpCallback: "XXX",
1512 crossDomain: crossDomain,
1513 beforeSend: function() {
1514 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1515 "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1518 success: function(data){
1519 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1522 error: function(data){
1523 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1531 test("jQuery.ajax() - script, Remote", function() {
1534 var base = window.location.href.replace(/[^\/]*$/, "");
1539 url: base + "data/test.js",
1541 success: function(data){
1542 ok( foobar, "Script results returned (GET, no callback)" );
1548 test("jQuery.ajax() - script, Remote with POST", function() {
1551 var base = window.location.href.replace(/[^\/]*$/, "");
1556 url: base + "data/test.js",
1559 success: function(data, status){
1560 ok( foobar, "Script results returned (POST, no callback)" );
1561 equals( status, "success", "Script results returned (POST, no callback)" );
1564 error: function(xhr) {
1565 ok( false, "ajax error, status code: " + xhr.status );
1571 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1574 var base = window.location.href.replace(/[^\/]*$/, "");
1575 base = base.replace(/^.*?\/\//, "//");
1580 url: base + "data/test.js",
1582 success: function(data){
1583 ok( foobar, "Script results returned (GET, no callback)" );
1589 test("jQuery.ajax() - malformed JSON", function() {
1595 url: "data/badjson.js",
1597 success: function(){
1598 ok( false, "Success." );
1601 error: function(xhr, msg, detailedMsg) {
1602 equals( "parsererror", msg, "A parse error occurred." );
1603 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1609 test("jQuery.ajax() - script by content-type", function() {
1617 url: "data/script.php",
1618 data: { header: "script" }
1622 url: "data/script.php",
1623 data: { header: "ecma" }
1626 ).then( start, start );
1629 test("jQuery.ajax() - json by content-type", function() {
1635 url: "data/json.php",
1636 data: { header: "json", json: "array" },
1637 success: function( json ) {
1638 ok( json.length >= 2, "Check length");
1639 equals( json[0].name, 'John', 'Check JSON: first, name' );
1640 equals( json[0].age, 21, 'Check JSON: first, age' );
1641 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1642 equals( json[1].age, 25, 'Check JSON: second, age' );
1648 test("jQuery.ajax() - json by content-type disabled with options", function() {
1654 url: url("data/json.php"),
1655 data: { header: "json", json: "array" },
1659 success: function( text ) {
1660 equals( typeof text , "string" , "json wasn't auto-determined" );
1661 var json = jQuery.parseJSON( text );
1662 ok( json.length >= 2, "Check length");
1663 equals( json[0].name, 'John', 'Check JSON: first, name' );
1664 equals( json[0].age, 21, 'Check JSON: first, age' );
1665 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1666 equals( json[1].age, 25, 'Check JSON: second, age' );
1672 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1675 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1676 ok( json.length >= 2, "Check length");
1677 equals( json[0].name, 'John', 'Check JSON: first, name' );
1678 equals( json[0].age, 21, 'Check JSON: first, age' );
1679 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1680 equals( json[1].age, 25, 'Check JSON: second, age' );
1685 test("jQuery.getJSON(String, Function) - JSON object", function() {
1688 jQuery.getJSON(url("data/json.php"), function(json) {
1689 if (json && json.data) {
1690 equals( json.data.lang, 'en', 'Check JSON: lang' );
1691 equals( json.data.length, 25, 'Check JSON: length' );
1697 test("jQuery.getJSON - Using Native JSON", function() {
1700 var old = window.JSON;
1702 parse: function(str){
1703 ok( true, "Verifying that parse method was run" );
1709 jQuery.getJSON(url("data/json.php"), function(json) {
1711 equals( json, true, "Verifying return value" );
1716 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1719 var base = window.location.href.replace(/[^\/]*$/, "");
1722 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1723 equals( json.data.lang, 'en', 'Check JSON: lang' );
1724 equals( json.data.length, 25, 'Check JSON: length' );
1729 test("jQuery.post - data", 3, function() {
1733 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1734 jQuery( 'math', xml ).each( function() {
1735 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1736 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1741 url: url('data/echoData.php'),
1749 success: function( data ) {
1750 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1753 ).then( start, start );
1757 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1762 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1763 jQuery('math', xml).each(function() {
1764 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1765 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1767 if ( ++done === 2 ) start();
1770 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1771 jQuery('math', xml).each(function() {
1772 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1773 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1775 if ( ++done === 2 ) start();
1779 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1784 jQuery.ajaxSetup({timeout: 1000});
1786 var pass = function() {
1788 if ( passed == 2 ) {
1789 ok( true, 'Check local and global callbacks after timeout' );
1790 jQuery('#main').unbind("ajaxError");
1795 var fail = function(a,b,c) {
1796 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1800 jQuery('#main').ajaxError(pass);
1804 url: url("data/name.php?wait=5"),
1810 jQuery.ajaxSetup({timeout: 0});
1813 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1815 jQuery.ajaxSetup({timeout: 50});
1820 url: url("data/name.php?wait=1"),
1822 ok( false, 'Check for local timeout failed' );
1825 success: function() {
1826 ok( true, 'Check for local timeout' );
1832 jQuery.ajaxSetup({timeout: 0});
1835 test("jQuery.ajax - simple get", function() {
1840 url: url("data/name.php?name=foo"),
1841 success: function(msg){
1842 equals( msg, 'bar', 'Check for GET' );
1848 test("jQuery.ajax - simple post", function() {
1853 url: url("data/name.php"),
1855 success: function(msg){
1856 equals( msg, 'pan', 'Check for POST' );
1862 test("ajaxSetup()", function() {
1866 url: url("data/name.php?name=foo"),
1867 success: function(msg){
1868 equals( msg, 'bar', 'Check for GET' );
1876 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1879 url: "data/name.php?wait=1",
1881 error: function(request, status) {
1882 ok( status != null, "status shouldn't be null in error handler" );
1883 equals( "timeout", status );
1890 test("data option: evaluate function values (#2806)", function() {
1893 url: "data/echoQuery.php",
1899 success: function(result) {
1900 equals( result, "key=value" );
1906 test("data option: empty bodies for non-GET requests", function() {
1909 url: "data/echoData.php",
1912 success: function(result) {
1913 equals( result, "" );
1919 var ifModifiedNow = new Date();
1921 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1923 test("jQuery.ajax - If-Modified-Since support" + label, function() {
1928 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1934 success: function(data, status) {
1935 equals(status, "success" );
1941 success: function(data, status) {
1942 if ( data === "FAIL" ) {
1943 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1944 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1946 equals(status, "notmodified");
1947 ok(data == null, "response body should be empty");
1952 // Do this because opera simply refuses to implement 304 handling :(
1953 // A feature-driven way of detecting this would be appreciated
1954 // See: http://gist.github.com/599419
1955 ok(jQuery.browser.opera, "error");
1956 ok(jQuery.browser.opera, "error");
1962 equals(false, "error");
1963 // Do this because opera simply refuses to implement 304 handling :(
1964 // A feature-driven way of detecting this would be appreciated
1965 // See: http://gist.github.com/599419
1966 ok(jQuery.browser.opera, "error");
1972 test("jQuery.ajax - Etag support" + label, function() {
1977 var url = "data/etag.php?ts=" + ifModifiedNow++;
1983 success: function(data, status) {
1984 equals(status, "success" );
1990 success: function(data, status) {
1991 if ( data === "FAIL" ) {
1992 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1993 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1995 equals(status, "notmodified");
1996 ok(data == null, "response body should be empty");
2001 // Do this because opera simply refuses to implement 304 handling :(
2002 // A feature-driven way of detecting this would be appreciated
2003 // See: http://gist.github.com/599419
2004 ok(jQuery.browser.opera, "error");
2005 ok(jQuery.browser.opera, "error");
2011 // Do this because opera simply refuses to implement 304 handling :(
2012 // A feature-driven way of detecting this would be appreciated
2013 // See: http://gist.github.com/599419
2014 ok(jQuery.browser.opera, "error");
2021 test("jQuery ajax - failing cross-domain", function() {
2030 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
2031 success: function(){ ok( false , "success" ); },
2032 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
2033 complete: function() { if ( ! --i ) start(); }
2037 url: 'http://www.google.com',
2038 success: function(){ ok( false , "success" ); },
2039 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
2040 complete: function() { if ( ! --i ) start(); }
2045 test("jQuery ajax - atom+xml", function() {
2050 url: url( 'data/atom+xml.php' ),
2051 success: function(){ ok( true , "success" ); },
2052 error: function(){ ok( false , "error" ); },
2053 complete: function() { start(); }
2058 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2059 var success = false;
2061 var xhr = jQuery.ajax({ url: window.location });
2066 ok( success, "document.location did not generate exception" );
2069 test( "jQuery.ajax - statusCode" , function() {
2076 function countComplete() {
2082 function createStatusCodes( name , isSuccess ) {
2083 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2086 ok( isSuccess , name );
2089 ok( ! isSuccess , name );
2095 "data/name.html": true,
2096 "data/someFileThatDoesNotExist.html": false
2097 } , function( uri , isSuccess ) {
2099 jQuery.ajax( url( uri ) , {
2100 statusCode: createStatusCodes( "in options" , isSuccess ),
2101 complete: countComplete
2104 jQuery.ajax( url( uri ) , {
2105 complete: countComplete
2106 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2108 jQuery.ajax( url( uri ) , {
2109 complete: function(jqXHR) {
2110 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2115 jQuery.ajax( url( uri ) , {
2116 complete: function(jqXHR) {
2117 setTimeout( function() {
2118 jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2124 jQuery.ajax( url( uri ) , {
2125 statusCode: createStatusCodes( "all (options)" , isSuccess ),
2126 complete: function(jqXHR) {
2127 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2128 setTimeout( function() {
2129 jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2133 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2135 var testString = "";
2137 jQuery.ajax( url( uri ), {
2138 success: function( a , b , jqXHR ) {
2139 ok( isSuccess , "success" );
2140 var statusCode = {};
2141 statusCode[ jqXHR.status ] = function() {
2144 jqXHR.statusCode( statusCode );
2147 error: function( jqXHR ) {
2148 ok( ! isSuccess , "error" );
2149 var statusCode = {};
2150 statusCode[ jqXHR.status ] = function() {
2153 jqXHR.statusCode( statusCode );
2156 complete: function() {
2157 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2158 ( isSuccess ? "success" : "error" ) + " callbacks" );
2166 test("jQuery.ajax - transitive conversions", function() {
2174 jQuery.ajax( url("data/json.php") , {
2176 "json myJson": function( data ) {
2177 ok( true , "converter called" );
2182 success: function() {
2183 ok( true , "Transitive conversion worked" );
2184 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2185 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2189 jQuery.ajax( url("data/json.php") , {
2191 "json myJson": function( data ) {
2192 ok( true , "converter called (*)" );
2196 contents: false, /* headers are wrong so we ignore them */
2197 dataType: "* myJson",
2198 success: function() {
2199 ok( true , "Transitive conversion worked (*)" );
2200 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2201 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2205 ).then( start , start );
2209 test("jQuery.ajax - overrideMimeType", function() {
2217 jQuery.ajax( url("data/json.php") , {
2218 beforeSend: function( xhr ) {
2219 xhr.overrideMimeType( "application/json" );
2221 success: function( json ) {
2222 ok( json.data , "Mimetype overriden using beforeSend" );
2226 jQuery.ajax( url("data/json.php") , {
2227 mimeType: "application/json",
2228 success: function( json ) {
2229 ok( json.data , "Mimetype overriden using mimeType option" );
2233 ).then( start , start );
2237 test("jQuery.ajax - abort in prefilter", function() {
2241 jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
2242 if ( options.abortInPrefilter ) {
2247 strictEqual( jQuery.ajax({
2248 abortInPrefilter: true,
2250 ok( false, "error callback called" );
2252 }), false, "Request was properly aborted early by the prefilter" );
2256 test("jQuery.ajax - active counter", function() {
2257 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );