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 var requestHeaders = {
353 "SometHing-elsE": "other value",
354 OthEr: "something else"
359 for( i in requestHeaders ) {
363 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
364 headers: requestHeaders,
365 success: function( data , _ , xhr ) {
367 for ( i in requestHeaders ) {
368 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
370 tmp = tmp.join( "" );
372 equals( data , tmp , "Headers were sent" );
373 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
376 error: function(){ ok(false, "error"); }
381 test(".ajax() - Accept header" , function() {
387 jQuery.ajax(url("data/headers.php?keys=accept"), {
389 Accept: "very wrong accept value"
391 beforeSend: function( xhr ) {
392 xhr.setRequestHeader( "Accept", "*/*" );
394 success: function( data ) {
395 strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
398 error: function(){ ok(false, "error"); }
403 test(".ajax() - contentType" , function() {
417 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
419 success: function( data ) {
420 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
422 complete: function() {
427 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
429 success: function( data ) {
430 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
432 complete: function() {
439 test(".ajax() - protocol-less urls", function() {
443 url: "//somedomain.com",
444 beforeSend: function( xhr, settings ) {
445 equals(settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added.");
451 test(".ajax() - hash", function() {
455 url: "data/name.html#foo",
456 beforeSend: function( xhr, settings ) {
457 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
463 url: "data/name.html?abc#foo",
464 beforeSend: function( xhr, settings ) {
465 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
471 url: "data/name.html?abc#foo",
472 data: { "test": 123 },
473 beforeSend: function( xhr, settings ) {
474 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
480 test("jQuery ajax - cross-domain detection", function() {
484 var loc = document.location,
485 otherPort = loc.port === 666 ? 667 : 666,
486 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
490 url: otherProtocol + "//" + loc.host,
491 beforeSend: function( _ , s ) {
492 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
499 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
500 beforeSend: function( _ , s ) {
501 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
508 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
509 beforeSend: function( _ , s ) {
510 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
517 url: loc.protocol + "//" + loc.host,
519 beforeSend: function( _ , s ) {
520 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
527 test(".load() - 404 error callbacks", function() {
531 jQuery('#foo').ajaxStart(function(){
532 ok( true, "ajaxStart" );
533 }).ajaxStop(function(){
534 ok( true, "ajaxStop" );
536 }).ajaxSend(function(){
537 ok( true, "ajaxSend" );
538 }).ajaxComplete(function(){
539 ok( true, "ajaxComplete" );
540 }).ajaxError(function(){
541 ok( true, "ajaxError" );
542 }).ajaxSuccess(function(){
543 ok( false, "ajaxSuccess" );
546 jQuery("<div/>").load("data/404.html", function(){
547 ok(true, "complete");
551 test("jQuery.ajax() - abort", function() {
555 jQuery('#foo').ajaxStart(function(){
556 ok( true, "ajaxStart" );
557 }).ajaxStop(function(){
558 ok( true, "ajaxStop" );
560 }).ajaxSend(function(){
561 ok( true, "ajaxSend" );
562 }).ajaxComplete(function(){
563 ok( true, "ajaxComplete" );
566 var xhr = jQuery.ajax({
567 url: url("data/name.php?wait=5"),
568 beforeSend: function(){ ok(true, "beforeSend"); },
569 complete: function(){ ok(true, "complete"); }
572 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
575 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
578 test("Ajax events with context", function() {
582 var context = document.createElement("div");
585 equals( this, context, e.type );
588 function callback(msg){
590 equals( this, context, "context is preserved on callback " + msg );
594 function nocallback(msg){
596 equals( typeof this.url, "string", "context is settings on callback " + msg );
600 jQuery('#foo').add(context)
607 url: url("data/name.html"),
608 beforeSend: callback("beforeSend"),
609 success: callback("success"),
610 error: callback("error"),
612 callback("complete").call(this);
615 url: url("data/404.html"),
617 beforeSend: callback("beforeSend"),
618 error: callback("error"),
619 complete: function(){
620 callback("complete").call(this);
622 jQuery('#foo').add(context).unbind();
625 url: url("data/404.html"),
626 beforeSend: nocallback("beforeSend"),
627 error: nocallback("error"),
628 complete: function(){
629 nocallback("complete").call(this);
640 test("jQuery.ajax context modification", function() {
648 url: url("data/name.html"),
650 beforeSend: function(){
653 complete: function() {
658 equals( obj.test, "foo", "Make sure the original object is maintained." );
661 test("jQuery.ajax context modification through ajaxSetup", function() {
672 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
675 url: url("data/name.html"),
676 complete: function() {
677 strictEqual( this, obj, "Make sure the original object is maintained." );
679 url: url("data/name.html"),
681 complete: function() {
682 ok( this !== obj, "Make sure overidding context is possible." );
687 url: url("data/name.html"),
688 beforeSend: function(){
691 complete: function() {
692 ok( this !== obj, "Make sure unsetting context is possible." );
702 test("jQuery.ajax() - disabled globals", function() {
706 jQuery('#foo').ajaxStart(function(){
707 ok( false, "ajaxStart" );
708 }).ajaxStop(function(){
709 ok( false, "ajaxStop" );
710 }).ajaxSend(function(){
711 ok( false, "ajaxSend" );
712 }).ajaxComplete(function(){
713 ok( false, "ajaxComplete" );
714 }).ajaxError(function(){
715 ok( false, "ajaxError" );
716 }).ajaxSuccess(function(){
717 ok( false, "ajaxSuccess" );
722 url: url("data/name.html"),
723 beforeSend: function(){ ok(true, "beforeSend"); },
724 success: function(){ ok(true, "success"); },
725 error: function(){ ok(false, "error"); },
726 complete: function(){
727 ok(true, "complete");
728 setTimeout(function(){ start(); }, 13);
733 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
737 url: url("data/with_fries.xml"),
739 success: function(resp) {
740 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
741 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
742 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
748 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
752 url: url("data/with_fries_over_jsonp.php"),
753 dataType: "jsonp xml",
754 success: function(resp) {
755 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
756 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
757 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
760 error: function(_1,_2,error) {
767 test("jQuery.ajax - HEAD requests", function() {
772 url: url("data/name.html"),
774 success: function(data, status, xhr){
775 var h = xhr.getAllResponseHeaders();
776 ok( /Date/i.test(h), 'No Date in HEAD response' );
779 url: url("data/name.html"),
780 data: { whip_it: "good" },
782 success: function(data, status, xhr){
783 var h = xhr.getAllResponseHeaders();
784 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
793 test("jQuery.ajax - beforeSend", function() {
799 jQuery.ajaxSetup({ timeout: 0 });
802 url: url("data/name.html"),
803 beforeSend: function(xml) {
806 success: function(data) {
807 ok( check, "check beforeSend was executed" );
813 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
815 var request = jQuery.ajax({
816 url: url("data/name.html"),
817 beforeSend: function() {
818 ok( true, "beforeSend got called, canceling" );
821 success: function() {
822 ok( false, "request didn't get canceled" );
824 complete: function() {
825 ok( false, "request didn't get canceled" );
828 ok( false, "request didn't get canceled" );
831 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
834 test("jQuery.ajax - beforeSend, cancel request manually", function() {
836 var request = jQuery.ajax({
837 url: url("data/name.html"),
838 beforeSend: function(xhr) {
839 ok( true, "beforeSend got called, canceling" );
842 success: function() {
843 ok( false, "request didn't get canceled" );
845 complete: function() {
846 ok( false, "request didn't get canceled" );
849 ok( false, "request didn't get canceled" );
852 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
855 window.foobar = null;
856 window.testFoo = undefined;
858 test("jQuery.ajax - dataType html", function() {
862 var verifyEvaluation = function() {
863 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
864 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
871 url: url("data/test.html"),
872 success: function(data) {
873 jQuery("#ap").html(data);
874 ok( data.match(/^html text/), 'Check content for datatype html' );
875 setTimeout(verifyEvaluation, 600);
880 test("serialize()", function() {
883 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
884 jQuery("#search").after(
885 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
886 '<input type="number" id="html5number" name="number" value="43" />'
889 equals( jQuery('#form').serialize(),
890 "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",
891 'Check form serialization as query string');
893 equals( jQuery('#form :input').serialize(),
894 "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",
895 'Check input serialization as query string');
897 equals( jQuery('#testForm').serialize(),
898 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
899 'Check form serialization as query string');
901 equals( jQuery('#testForm :input').serialize(),
902 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
903 'Check input serialization as query string');
905 equals( jQuery('#form, #testForm').serialize(),
906 "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=",
907 'Multiple form serialization as query string');
909 /* Temporarily disabled. Opera 10 has problems with form serialization.
910 equals( jQuery('#form, #testForm :input').serialize(),
911 "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=",
912 'Mixed form/input serialization as query string');
914 jQuery("#html5email, #html5number").remove();
917 test("jQuery.param()", function() {
920 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
922 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
923 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
925 params = {someName: [1, 2, 3], regularThing: "blah" };
926 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
928 params = {foo: ['a', 'b', 'c']};
929 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
931 params = {foo: ["baz", 42, "All your base are belong to us"] };
932 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
934 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
935 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" );
937 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
938 equals( 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" );
940 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 ] };
941 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" );
943 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?" };
944 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" );
946 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." );
948 // Make sure empty arrays and objects are handled #6481
949 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
950 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
951 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
954 equals( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
956 equals( jQuery.param(jQuery("#form :input")), "action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo%5Bbar%5D=&name=name&search=search&button=&=foobar&select1=&select2=3&select3=1&select4=1&select5=3", "Make sure jQuery objects are properly serialized");
958 jQuery.ajaxSetup({ traditional: true });
960 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
961 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
963 params = {someName: [1, 2, 3], regularThing: "blah" };
964 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
966 params = {foo: ['a', 'b', 'c']};
967 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
969 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
970 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
972 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
973 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" );
975 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?" };
976 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" );
978 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 ] };
979 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)" );
981 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?" };
982 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" );
984 params = { param1: null };
985 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
987 params = {'test': {'length': 3, 'foo': 'bar'} };
988 equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
991 test("synchronous request", function() {
993 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
996 test("synchronous request with callbacks", function() {
999 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1000 ok( /^{ "data"/.test( result ), "check returned text" );
1003 test("pass-through request object", function() {
1007 var target = "data/name.html";
1008 var successCount = 0;
1011 var success = function() {
1014 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1016 errorEx += ": " + xml.status;
1018 jQuery("#foo").one('ajaxStop', function () {
1019 equals(successCount, 5, "Check all ajax calls successful");
1020 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1021 jQuery("#foo").unbind('ajaxError');
1026 ok( jQuery.get(url(target), success), "get" );
1027 ok( jQuery.post(url(target), success), "post" );
1028 ok( jQuery.getScript(url("data/test.js"), success), "script" );
1029 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1030 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1033 test("ajax cache", function () {
1040 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1041 var re = /_=(.*?)(&|$)/g;
1043 for (var i = 0; i < 6; i++) {
1044 var ret = re.exec(s.url);
1050 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1051 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1056 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1057 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1058 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1059 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1060 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1061 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1066 * The assertions expect that the passed-in object will be modified,
1067 * which shouldn't be the case. Fixes #5439.
1068 test("global ajaxSettings", function() {
1071 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1072 var orig = { url: "data/with_fries.xml" };
1075 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1077 t = jQuery.extend({}, orig);
1080 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1082 t = jQuery.extend({}, orig);
1083 t.data = { zoo: 'a', ping: 'b' };
1085 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' }" );
1087 jQuery.ajaxSettings = tmp;
1091 test("load(String)", function() {
1093 stop(); // check if load can be called with only url
1094 jQuery('#first').load("data/name.html", start);
1097 test("load('url selector')", function() {
1099 stop(); // check if load can be called with only url
1100 jQuery('#first').load("data/test3.html div.user", function(){
1101 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1106 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1109 jQuery.ajaxSetup({ dataType: "json" });
1110 jQuery("#first").ajaxComplete(function (e, xml, s) {
1111 equals( s.dataType, "html", "Verify the load() dataType was html" );
1112 jQuery("#first").unbind("ajaxComplete");
1113 jQuery.ajaxSetup({ dataType: "" });
1116 jQuery('#first').load("data/test3.html");
1119 test("load(String, Function) - simple: inject text into DOM", function() {
1122 jQuery('#first').load(url("data/name.html"), function() {
1123 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1128 test("load(String, Function) - check scripts", function() {
1132 var verifyEvaluation = function() {
1133 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1134 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1138 jQuery('#first').load(url('data/test.html'), function() {
1139 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1140 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1141 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1142 setTimeout(verifyEvaluation, 600);
1146 test("load(String, Function) - check file with only a script tag", function() {
1150 jQuery('#first').load(url('data/test2.html'), function() {
1151 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1152 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1158 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1161 jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1162 var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1163 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1164 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1165 jQuery.ajaxSetup({ dataFilter: 0 });
1170 test("load(String, Object, Function)", function() {
1174 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1175 var $post = jQuery(this).find('#post');
1176 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1177 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1182 test("load(String, String, Function)", function() {
1186 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1187 var $get = jQuery(this).find('#get');
1188 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1189 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1194 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1197 jQuery.get(url('data/dashboard.xml'), function(xml) {
1199 jQuery('tab', xml).each(function() {
1200 content.push(jQuery(this).text());
1202 equals( content[0], 'blabla', 'Check first tab');
1203 equals( content[1], 'blublu', 'Check second tab');
1208 test("jQuery.getScript(String, Function) - with callback", function() {
1211 jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1212 equals( foobar, "bar", 'Check if script was evaluated' );
1213 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1214 setTimeout(start, 100);
1218 test("jQuery.getScript(String, Function) - no callback", function() {
1221 jQuery.getScript(url("data/test.js"), function(){
1226 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1228 test("jQuery.ajax() - JSONP, " + label, function() {
1232 function plus(){ if ( ++count == 18 ) start(); }
1237 url: "data/jsonp.php",
1239 crossDomain: crossDomain,
1240 success: function(data){
1241 ok( data.data, "JSON results returned (GET, no callback)" );
1244 error: function(data){
1245 ok( false, "Ajax error JSON (GET, no callback)" );
1251 url: "data/jsonp.php?callback=?",
1253 crossDomain: crossDomain,
1254 success: function(data){
1255 ok( data.data, "JSON results returned (GET, url callback)" );
1258 error: function(data){
1259 ok( false, "Ajax error JSON (GET, url callback)" );
1265 url: "data/jsonp.php",
1267 crossDomain: crossDomain,
1269 success: function(data){
1270 ok( data.data, "JSON results returned (GET, data callback)" );
1273 error: function(data){
1274 ok( false, "Ajax error JSON (GET, data callback)" );
1280 url: "data/jsonp.php?callback=??",
1282 crossDomain: crossDomain,
1283 success: function(data){
1284 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1287 error: function(data){
1288 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1294 url: "data/jsonp.php",
1296 crossDomain: crossDomain,
1297 data: "callback=??",
1298 success: function(data){
1299 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1302 error: function(data){
1303 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1309 url: "data/jsonp.php/??",
1311 crossDomain: crossDomain,
1312 success: function(data){
1313 ok( data.data, "JSON results returned (GET, REST-like)" );
1316 error: function(data){
1317 ok( false, "Ajax error JSON (GET, REST-like)" );
1323 url: "data/jsonp.php/???json=1",
1325 crossDomain: crossDomain,
1326 success: function(data){
1327 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1330 error: function(data){
1331 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1337 url: "data/jsonp.php",
1339 crossDomain: crossDomain,
1341 success: function(data){
1342 ok( data.data, "JSON results returned (GET, data obj callback)" );
1345 error: function(data){
1346 ok( false, "Ajax error JSON (GET, data obj callback)" );
1351 window.jsonpResults = function(data) {
1352 ok( data.data, "JSON results returned (GET, custom callback function)" );
1353 window.jsonpResults = undefined;
1358 url: "data/jsonp.php",
1360 crossDomain: crossDomain,
1361 jsonpCallback: "jsonpResults",
1362 success: function(data){
1363 ok( data.data, "JSON results returned (GET, custom callback name)" );
1366 error: function(data){
1367 ok( false, "Ajax error JSON (GET, custom callback name)" );
1373 url: "data/jsonp.php",
1375 crossDomain: crossDomain,
1376 jsonpCallback: "functionToCleanUp",
1377 success: function(data){
1378 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1379 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1383 url: "data/jsonp.php",
1385 crossDomain: crossDomain,
1386 jsonpCallback: "functionToCleanUp",
1387 beforeSend: function( jqXHR ) {
1392 xhr.error(function() {
1393 ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1394 strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1398 error: function(data){
1399 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1406 url: "data/jsonp.php",
1408 crossDomain: crossDomain,
1409 success: function(data){
1410 ok( data.data, "JSON results returned (POST, no callback)" );
1413 error: function(data){
1414 ok( false, "Ajax error JSON (GET, data obj callback)" );
1421 url: "data/jsonp.php",
1424 crossDomain: crossDomain,
1425 success: function(data){
1426 ok( data.data, "JSON results returned (POST, data callback)" );
1429 error: function(data){
1430 ok( false, "Ajax error JSON (POST, data callback)" );
1437 url: "data/jsonp.php",
1440 crossDomain: crossDomain,
1441 success: function(data){
1442 ok( data.data, "JSON results returned (POST, data obj callback)" );
1445 error: function(data){
1446 ok( false, "Ajax error JSON (POST, data obj callback)" );
1453 url: "data/jsonp.php",
1455 crossDomain: crossDomain,
1456 beforeSend: function(){
1457 strictEqual( this.cache, false, "cache must be false on JSON request" );
1464 url: "data/jsonp.php?callback=XXX",
1467 jsonpCallback: "XXX",
1468 crossDomain: crossDomain,
1469 beforeSend: function() {
1470 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1471 "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1474 success: function(data){
1475 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1478 error: function(data){
1479 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1487 test("jQuery.ajax() - script, Remote", function() {
1490 var base = window.location.href.replace(/[^\/]*$/, "");
1495 url: base + "data/test.js",
1497 success: function(data){
1498 ok( foobar, "Script results returned (GET, no callback)" );
1504 test("jQuery.ajax() - script, Remote with POST", function() {
1507 var base = window.location.href.replace(/[^\/]*$/, "");
1512 url: base + "data/test.js",
1515 success: function(data, status){
1516 ok( foobar, "Script results returned (POST, no callback)" );
1517 equals( status, "success", "Script results returned (POST, no callback)" );
1520 error: function(xhr) {
1521 ok( false, "ajax error, status code: " + xhr.status );
1527 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1530 var base = window.location.href.replace(/[^\/]*$/, "");
1531 base = base.replace(/^.*?\/\//, "//");
1536 url: base + "data/test.js",
1538 success: function(data){
1539 ok( foobar, "Script results returned (GET, no callback)" );
1545 test("jQuery.ajax() - malformed JSON", function() {
1551 url: "data/badjson.js",
1553 success: function(){
1554 ok( false, "Success." );
1557 error: function(xhr, msg, detailedMsg) {
1558 equals( "parsererror", msg, "A parse error occurred." );
1559 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1565 test("jQuery.ajax() - script by content-type", function() {
1573 url: "data/script.php",
1574 data: { header: "script" }
1578 url: "data/script.php",
1579 data: { header: "ecma" }
1582 ).then( start, start );
1585 test("jQuery.ajax() - json by content-type", function() {
1591 url: "data/json.php",
1592 data: { header: "json", json: "array" },
1593 success: function( json ) {
1594 ok( json.length >= 2, "Check length");
1595 equals( json[0].name, 'John', 'Check JSON: first, name' );
1596 equals( json[0].age, 21, 'Check JSON: first, age' );
1597 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1598 equals( json[1].age, 25, 'Check JSON: second, age' );
1604 test("jQuery.ajax() - json by content-type disabled with options", function() {
1610 url: url("data/json.php"),
1611 data: { header: "json", json: "array" },
1615 success: function( text ) {
1616 equals( typeof text , "string" , "json wasn't auto-determined" );
1617 var json = jQuery.parseJSON( text );
1618 ok( json.length >= 2, "Check length");
1619 equals( json[0].name, 'John', 'Check JSON: first, name' );
1620 equals( json[0].age, 21, 'Check JSON: first, age' );
1621 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1622 equals( json[1].age, 25, 'Check JSON: second, age' );
1628 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1631 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1632 ok( json.length >= 2, "Check length");
1633 equals( json[0].name, 'John', 'Check JSON: first, name' );
1634 equals( json[0].age, 21, 'Check JSON: first, age' );
1635 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1636 equals( json[1].age, 25, 'Check JSON: second, age' );
1641 test("jQuery.getJSON(String, Function) - JSON object", function() {
1644 jQuery.getJSON(url("data/json.php"), function(json) {
1645 if (json && json.data) {
1646 equals( json.data.lang, 'en', 'Check JSON: lang' );
1647 equals( json.data.length, 25, 'Check JSON: length' );
1653 test("jQuery.getJSON - Using Native JSON", function() {
1656 var old = window.JSON;
1658 parse: function(str){
1659 ok( true, "Verifying that parse method was run" );
1665 jQuery.getJSON(url("data/json.php"), function(json) {
1667 equals( json, true, "Verifying return value" );
1672 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1675 var base = window.location.href.replace(/[^\/]*$/, "");
1678 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1679 equals( json.data.lang, 'en', 'Check JSON: lang' );
1680 equals( json.data.length, 25, 'Check JSON: length' );
1685 test("jQuery.post - data", 3, function() {
1689 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1690 jQuery( 'math', xml ).each( function() {
1691 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1692 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1697 url: url('data/echoData.php'),
1705 success: function( data ) {
1706 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1709 ).then( start, start );
1713 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1718 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1719 jQuery('math', xml).each(function() {
1720 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1721 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1723 if ( ++done === 2 ) start();
1726 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1727 jQuery('math', xml).each(function() {
1728 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1729 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1731 if ( ++done === 2 ) start();
1735 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1740 jQuery.ajaxSetup({timeout: 1000});
1742 var pass = function() {
1744 if ( passed == 2 ) {
1745 ok( true, 'Check local and global callbacks after timeout' );
1746 jQuery('#main').unbind("ajaxError");
1751 var fail = function(a,b,c) {
1752 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1756 jQuery('#main').ajaxError(pass);
1760 url: url("data/name.php?wait=5"),
1766 jQuery.ajaxSetup({timeout: 0});
1769 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1771 jQuery.ajaxSetup({timeout: 50});
1776 url: url("data/name.php?wait=1"),
1778 ok( false, 'Check for local timeout failed' );
1781 success: function() {
1782 ok( true, 'Check for local timeout' );
1788 jQuery.ajaxSetup({timeout: 0});
1791 test("jQuery.ajax - simple get", function() {
1796 url: url("data/name.php?name=foo"),
1797 success: function(msg){
1798 equals( msg, 'bar', 'Check for GET' );
1804 test("jQuery.ajax - simple post", function() {
1809 url: url("data/name.php"),
1811 success: function(msg){
1812 equals( msg, 'pan', 'Check for POST' );
1818 test("ajaxSetup()", function() {
1822 url: url("data/name.php?name=foo"),
1823 success: function(msg){
1824 equals( msg, 'bar', 'Check for GET' );
1832 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1835 url: "data/name.php?wait=1",
1837 error: function(request, status) {
1838 ok( status != null, "status shouldn't be null in error handler" );
1839 equals( "timeout", status );
1846 test("data option: evaluate function values (#2806)", function() {
1849 url: "data/echoQuery.php",
1855 success: function(result) {
1856 equals( result, "key=value" );
1862 test("data option: empty bodies for non-GET requests", function() {
1865 url: "data/echoData.php",
1868 success: function(result) {
1869 equals( result, "" );
1875 var ifModifiedNow = new Date();
1877 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1879 test("jQuery.ajax - If-Modified-Since support" + label, function() {
1884 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1890 success: function(data, status) {
1891 equals(status, "success" );
1897 success: function(data, status) {
1898 if ( data === "FAIL" ) {
1899 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1900 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1902 equals(status, "notmodified");
1903 ok(data == null, "response body should be empty");
1908 // Do this because opera simply refuses to implement 304 handling :(
1909 // A feature-driven way of detecting this would be appreciated
1910 // See: http://gist.github.com/599419
1911 ok(jQuery.browser.opera, "error");
1912 ok(jQuery.browser.opera, "error");
1918 equals(false, "error");
1919 // Do this because opera simply refuses to implement 304 handling :(
1920 // A feature-driven way of detecting this would be appreciated
1921 // See: http://gist.github.com/599419
1922 ok(jQuery.browser.opera, "error");
1928 test("jQuery.ajax - Etag support" + label, function() {
1933 var url = "data/etag.php?ts=" + ifModifiedNow++;
1939 success: function(data, status) {
1940 equals(status, "success" );
1946 success: function(data, status) {
1947 if ( data === "FAIL" ) {
1948 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1949 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1951 equals(status, "notmodified");
1952 ok(data == null, "response body should be empty");
1957 // Do this because opera simply refuses to implement 304 handling :(
1958 // A feature-driven way of detecting this would be appreciated
1959 // See: http://gist.github.com/599419
1960 ok(jQuery.browser.opera, "error");
1961 ok(jQuery.browser.opera, "error");
1967 // Do this because opera simply refuses to implement 304 handling :(
1968 // A feature-driven way of detecting this would be appreciated
1969 // See: http://gist.github.com/599419
1970 ok(jQuery.browser.opera, "error");
1977 test("jQuery ajax - failing cross-domain", function() {
1986 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1987 success: function(){ ok( false , "success" ); },
1988 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1989 complete: function() { if ( ! --i ) start(); }
1993 url: 'http://www.google.com',
1994 success: function(){ ok( false , "success" ); },
1995 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1996 complete: function() { if ( ! --i ) start(); }
2001 test("jQuery ajax - atom+xml", function() {
2006 url: url( 'data/atom+xml.php' ),
2007 success: function(){ ok( true , "success" ); },
2008 error: function(){ ok( false , "error" ); },
2009 complete: function() { start(); }
2014 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2015 var success = false;
2017 var xhr = jQuery.ajax({ url: window.location });
2022 ok( success, "document.location did not generate exception" );
2025 test( "jQuery.ajax - statusCode" , function() {
2032 function countComplete() {
2038 function createStatusCodes( name , isSuccess ) {
2039 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2042 ok( isSuccess , name );
2045 ok( ! isSuccess , name );
2051 "data/name.html": true,
2052 "data/someFileThatDoesNotExist.html": false
2053 } , function( uri , isSuccess ) {
2055 jQuery.ajax( url( uri ) , {
2056 statusCode: createStatusCodes( "in options" , isSuccess ),
2057 complete: countComplete
2060 jQuery.ajax( url( uri ) , {
2061 complete: countComplete
2062 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2064 jQuery.ajax( url( uri ) , {
2065 complete: function(jqXHR) {
2066 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2071 jQuery.ajax( url( uri ) , {
2072 complete: function(jqXHR) {
2073 setTimeout( function() {
2074 jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2080 jQuery.ajax( url( uri ) , {
2081 statusCode: createStatusCodes( "all (options)" , isSuccess ),
2082 complete: function(jqXHR) {
2083 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2084 setTimeout( function() {
2085 jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2089 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2091 var testString = "";
2093 jQuery.ajax( url( uri ), {
2094 success: function( a , b , jqXHR ) {
2095 ok( isSuccess , "success" );
2096 var statusCode = {};
2097 statusCode[ jqXHR.status ] = function() {
2100 jqXHR.statusCode( statusCode );
2103 error: function( jqXHR ) {
2104 ok( ! isSuccess , "error" );
2105 var statusCode = {};
2106 statusCode[ jqXHR.status ] = function() {
2109 jqXHR.statusCode( statusCode );
2112 complete: function() {
2113 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2114 ( isSuccess ? "success" : "error" ) + " callbacks" );
2122 test("jQuery.ajax - transitive conversions", function() {
2130 jQuery.ajax( url("data/json.php") , {
2132 "json myJson": function( data ) {
2133 ok( true , "converter called" );
2138 success: function() {
2139 ok( true , "Transitive conversion worked" );
2140 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2141 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2145 jQuery.ajax( url("data/json.php") , {
2147 "json myJson": function( data ) {
2148 ok( true , "converter called (*)" );
2152 contents: false, /* headers are wrong so we ignore them */
2153 dataType: "* myJson",
2154 success: function() {
2155 ok( true , "Transitive conversion worked (*)" );
2156 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2157 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2161 ).then( start , start );
2165 test("jQuery.ajax - abort in prefilter", function() {
2169 jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
2170 if ( options.abortInPrefilter ) {
2175 strictEqual( jQuery.ajax({
2176 abortInPrefilter: true,
2178 ok( false, "error callback called" );
2180 }), false, "Request was properly aborted early by the prefilter" );
2184 test("jQuery.ajax - active counter", function() {
2185 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );