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 equals( data , tmp , "Headers were sent" );
380 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
382 error: function(){ ok(false, "error"); }
384 }).then( start, start );
388 test(".ajax() - Accept header" , function() {
394 jQuery.ajax(url("data/headers.php?keys=accept"), {
396 Accept: "very wrong accept value"
398 beforeSend: function( xhr ) {
399 xhr.setRequestHeader( "Accept", "*/*" );
401 success: function( data ) {
402 strictEqual( data , "accept: */*\n" , "Test Accept header is set to last value provided" );
405 error: function(){ ok(false, "error"); }
410 test(".ajax() - contentType" , function() {
424 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
426 success: function( data ) {
427 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
429 complete: function() {
434 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
436 success: function( data ) {
437 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
439 complete: function() {
446 test(".ajax() - protocol-less urls", function() {
450 url: "//somedomain.com",
451 beforeSend: function( xhr, settings ) {
452 equals(settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added.");
458 test(".ajax() - hash", function() {
462 url: "data/name.html#foo",
463 beforeSend: function( xhr, settings ) {
464 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
470 url: "data/name.html?abc#foo",
471 beforeSend: function( xhr, settings ) {
472 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
478 url: "data/name.html?abc#foo",
479 data: { "test": 123 },
480 beforeSend: function( xhr, settings ) {
481 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
487 test("jQuery ajax - cross-domain detection", function() {
491 var loc = document.location,
492 otherPort = loc.port === 666 ? 667 : 666,
493 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
497 url: otherProtocol + "//" + loc.host,
498 beforeSend: function( _ , s ) {
499 ok( s.crossDomain , "Test different protocols are detected as cross-domain" );
506 url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ),
507 beforeSend: function( _ , s ) {
508 ok( s.crossDomain , "Test different hostnames are detected as cross-domain" );
515 url: loc.protocol + "//" + loc.hostname + ":" + otherPort,
516 beforeSend: function( _ , s ) {
517 ok( s.crossDomain , "Test different ports are detected as cross-domain" );
524 url: loc.protocol + "//" + loc.host,
526 beforeSend: function( _ , s ) {
527 ok( s.crossDomain , "Test forced crossDomain is detected as cross-domain" );
534 test(".load() - 404 error callbacks", function() {
538 jQuery('#foo').ajaxStart(function(){
539 ok( true, "ajaxStart" );
540 }).ajaxStop(function(){
541 ok( true, "ajaxStop" );
543 }).ajaxSend(function(){
544 ok( true, "ajaxSend" );
545 }).ajaxComplete(function(){
546 ok( true, "ajaxComplete" );
547 }).ajaxError(function(){
548 ok( true, "ajaxError" );
549 }).ajaxSuccess(function(){
550 ok( false, "ajaxSuccess" );
553 jQuery("<div/>").load("data/404.html", function(){
554 ok(true, "complete");
558 test("jQuery.ajax() - abort", 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" );
573 var xhr = jQuery.ajax({
574 url: url("data/name.php?wait=5"),
575 beforeSend: function(){ ok(true, "beforeSend"); },
576 complete: function(){ ok(true, "complete"); }
579 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
582 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
585 test("Ajax events with context", function() {
589 var context = document.createElement("div");
592 equals( this, context, e.type );
595 function callback(msg){
597 equals( this, context, "context is preserved on callback " + msg );
601 function nocallback(msg){
603 equals( typeof this.url, "string", "context is settings on callback " + msg );
607 jQuery('#foo').add(context)
614 url: url("data/name.html"),
615 beforeSend: callback("beforeSend"),
616 success: callback("success"),
617 error: callback("error"),
619 callback("complete").call(this);
622 url: url("data/404.html"),
624 beforeSend: callback("beforeSend"),
625 error: callback("error"),
626 complete: function(){
627 callback("complete").call(this);
629 jQuery('#foo').add(context).unbind();
632 url: url("data/404.html"),
633 beforeSend: nocallback("beforeSend"),
634 error: nocallback("error"),
635 complete: function(){
636 nocallback("complete").call(this);
647 test("jQuery.ajax context modification", function() {
655 url: url("data/name.html"),
657 beforeSend: function(){
660 complete: function() {
665 equals( obj.test, "foo", "Make sure the original object is maintained." );
668 test("jQuery.ajax context modification through ajaxSetup", function() {
679 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
682 url: url("data/name.html"),
683 complete: function() {
684 strictEqual( this, obj, "Make sure the original object is maintained." );
686 url: url("data/name.html"),
688 complete: function() {
689 ok( this !== obj, "Make sure overidding context is possible." );
694 url: url("data/name.html"),
695 beforeSend: function(){
698 complete: function() {
699 ok( this !== obj, "Make sure unsetting context is possible." );
709 test("jQuery.ajax() - disabled globals", function() {
713 jQuery('#foo').ajaxStart(function(){
714 ok( false, "ajaxStart" );
715 }).ajaxStop(function(){
716 ok( false, "ajaxStop" );
717 }).ajaxSend(function(){
718 ok( false, "ajaxSend" );
719 }).ajaxComplete(function(){
720 ok( false, "ajaxComplete" );
721 }).ajaxError(function(){
722 ok( false, "ajaxError" );
723 }).ajaxSuccess(function(){
724 ok( false, "ajaxSuccess" );
729 url: url("data/name.html"),
730 beforeSend: function(){ ok(true, "beforeSend"); },
731 success: function(){ ok(true, "success"); },
732 error: function(){ ok(false, "error"); },
733 complete: function(){
734 ok(true, "complete");
735 setTimeout(function(){ start(); }, 13);
740 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
744 url: url("data/with_fries.xml"),
746 success: function(resp) {
747 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
748 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
749 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
755 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
759 url: url("data/with_fries_over_jsonp.php"),
760 dataType: "jsonp xml",
761 success: function(resp) {
762 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
763 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
764 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
767 error: function(_1,_2,error) {
774 test("jQuery.ajax - HEAD requests", function() {
779 url: url("data/name.html"),
781 success: function(data, status, xhr){
782 var h = xhr.getAllResponseHeaders();
783 ok( /Date/i.test(h), 'No Date in HEAD response' );
786 url: url("data/name.html"),
787 data: { whip_it: "good" },
789 success: function(data, status, xhr){
790 var h = xhr.getAllResponseHeaders();
791 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
800 test("jQuery.ajax - beforeSend", function() {
806 jQuery.ajaxSetup({ timeout: 0 });
809 url: url("data/name.html"),
810 beforeSend: function(xml) {
813 success: function(data) {
814 ok( check, "check beforeSend was executed" );
820 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
822 var request = jQuery.ajax({
823 url: url("data/name.html"),
824 beforeSend: function() {
825 ok( true, "beforeSend got called, canceling" );
828 success: function() {
829 ok( false, "request didn't get canceled" );
831 complete: function() {
832 ok( false, "request didn't get canceled" );
835 ok( false, "request didn't get canceled" );
838 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
841 test("jQuery.ajax - beforeSend, cancel request manually", function() {
843 var request = jQuery.ajax({
844 url: url("data/name.html"),
845 beforeSend: function(xhr) {
846 ok( true, "beforeSend got called, canceling" );
849 success: function() {
850 ok( false, "request didn't get canceled" );
852 complete: function() {
853 ok( false, "request didn't get canceled" );
856 ok( false, "request didn't get canceled" );
859 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
862 window.foobar = null;
863 window.testFoo = undefined;
865 test("jQuery.ajax - dataType html", function() {
869 var verifyEvaluation = function() {
870 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
871 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
878 url: url("data/test.html"),
879 success: function(data) {
880 jQuery("#ap").html(data);
881 ok( data.match(/^html text/), 'Check content for datatype html' );
882 setTimeout(verifyEvaluation, 600);
887 test("serialize()", function() {
890 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
891 jQuery("#search").after(
892 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
893 '<input type="number" id="html5number" name="number" value="43" />'
896 equals( jQuery('#form').serialize(),
897 "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",
898 'Check form serialization as query string');
900 equals( jQuery('#form :input').serialize(),
901 "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",
902 'Check input serialization as query string');
904 equals( jQuery('#testForm').serialize(),
905 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
906 'Check form serialization as query string');
908 equals( jQuery('#testForm :input').serialize(),
909 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
910 'Check input serialization as query string');
912 equals( jQuery('#form, #testForm').serialize(),
913 "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=",
914 'Multiple form serialization as query string');
916 /* Temporarily disabled. Opera 10 has problems with form serialization.
917 equals( jQuery('#form, #testForm :input').serialize(),
918 "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=",
919 'Mixed form/input serialization as query string');
921 jQuery("#html5email, #html5number").remove();
924 test("jQuery.param()", function() {
927 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
929 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
930 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
932 params = {someName: [1, 2, 3], regularThing: "blah" };
933 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
935 params = {foo: ['a', 'b', 'c']};
936 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
938 params = {foo: ["baz", 42, "All your base are belong to us"] };
939 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
941 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
942 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" );
944 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?" };
945 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" );
947 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 ] };
948 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" );
950 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
951 equals( 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" );
953 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." );
955 // Make sure empty arrays and objects are handled #6481
956 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
957 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
958 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
961 equals( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
963 jQuery.ajaxSetup({ traditional: true });
965 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
966 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
968 params = {someName: [1, 2, 3], regularThing: "blah" };
969 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
971 params = {foo: ['a', 'b', 'c']};
972 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
974 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
975 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
977 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
978 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" );
980 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?" };
981 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" );
983 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 ] };
984 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)" );
986 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
987 equals( 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" );
989 params = { param1: null };
990 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
992 params = {'test': {'length': 3, 'foo': 'bar'} };
993 equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
996 test("synchronous request", function() {
998 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
1001 test("synchronous request with callbacks", function() {
1004 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1005 ok( /^{ "data"/.test( result ), "check returned text" );
1008 test("pass-through request object", function() {
1012 var target = "data/name.html";
1013 var successCount = 0;
1016 var success = function() {
1019 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1021 errorEx += ": " + xml.status;
1023 jQuery("#foo").one('ajaxStop', function () {
1024 equals(successCount, 5, "Check all ajax calls successful");
1025 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1026 jQuery("#foo").unbind('ajaxError');
1031 ok( jQuery.get(url(target), success), "get" );
1032 ok( jQuery.post(url(target), success), "post" );
1033 ok( jQuery.getScript(url("data/test.js"), success), "script" );
1034 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1035 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1038 test("ajax cache", function () {
1045 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1046 var re = /_=(.*?)(&|$)/g;
1048 for (var i = 0; i < 6; i++) {
1049 var ret = re.exec(s.url);
1055 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1056 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1061 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1062 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1063 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1064 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1065 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1066 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1071 * The assertions expect that the passed-in object will be modified,
1072 * which shouldn't be the case. Fixes #5439.
1073 test("global ajaxSettings", function() {
1076 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1077 var orig = { url: "data/with_fries.xml" };
1080 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1082 t = jQuery.extend({}, orig);
1085 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1087 t = jQuery.extend({}, orig);
1088 t.data = { zoo: 'a', ping: 'b' };
1090 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' }" );
1092 jQuery.ajaxSettings = tmp;
1096 test("load(String)", function() {
1098 stop(); // check if load can be called with only url
1099 jQuery('#first').load("data/name.html", start);
1102 test("load('url selector')", function() {
1104 stop(); // check if load can be called with only url
1105 jQuery('#first').load("data/test3.html div.user", function(){
1106 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1111 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1114 jQuery.ajaxSetup({ dataType: "json" });
1115 jQuery("#first").ajaxComplete(function (e, xml, s) {
1116 equals( s.dataType, "html", "Verify the load() dataType was html" );
1117 jQuery("#first").unbind("ajaxComplete");
1118 jQuery.ajaxSetup({ dataType: "" });
1121 jQuery('#first').load("data/test3.html");
1124 test("load(String, Function) - simple: inject text into DOM", function() {
1127 jQuery('#first').load(url("data/name.html"), function() {
1128 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1133 test("load(String, Function) - check scripts", function() {
1137 var verifyEvaluation = function() {
1138 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1139 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1143 jQuery('#first').load(url('data/test.html'), function() {
1144 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1145 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1146 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1147 setTimeout(verifyEvaluation, 600);
1151 test("load(String, Function) - check file with only a script tag", function() {
1155 jQuery('#first').load(url('data/test2.html'), function() {
1156 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1157 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1163 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1166 jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1167 var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1168 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1169 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1170 jQuery.ajaxSetup({ dataFilter: 0 });
1175 test("load(String, Object, Function)", function() {
1179 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1180 var $post = jQuery(this).find('#post');
1181 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1182 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1187 test("load(String, String, Function)", function() {
1191 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1192 var $get = jQuery(this).find('#get');
1193 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1194 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1199 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1202 jQuery.get(url('data/dashboard.xml'), function(xml) {
1204 jQuery('tab', xml).each(function() {
1205 content.push(jQuery(this).text());
1207 equals( content[0], 'blabla', 'Check first tab');
1208 equals( content[1], 'blublu', 'Check second tab');
1213 test("jQuery.getScript(String, Function) - with callback", function() {
1216 jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1217 equals( foobar, "bar", 'Check if script was evaluated' );
1218 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1219 setTimeout(start, 100);
1223 test("jQuery.getScript(String, Function) - no callback", function() {
1226 jQuery.getScript(url("data/test.js"), function(){
1231 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1233 test("jQuery.ajax() - JSONP, " + label, function() {
1237 function plus(){ if ( ++count == 18 ) start(); }
1242 url: "data/jsonp.php",
1244 crossDomain: crossDomain,
1245 success: function(data){
1246 ok( data.data, "JSON results returned (GET, no callback)" );
1249 error: function(data){
1250 ok( false, "Ajax error JSON (GET, no callback)" );
1256 url: "data/jsonp.php?callback=?",
1258 crossDomain: crossDomain,
1259 success: function(data){
1260 ok( data.data, "JSON results returned (GET, url callback)" );
1263 error: function(data){
1264 ok( false, "Ajax error JSON (GET, url callback)" );
1270 url: "data/jsonp.php",
1272 crossDomain: crossDomain,
1274 success: function(data){
1275 ok( data.data, "JSON results returned (GET, data callback)" );
1278 error: function(data){
1279 ok( false, "Ajax error JSON (GET, data callback)" );
1285 url: "data/jsonp.php?callback=??",
1287 crossDomain: crossDomain,
1288 success: function(data){
1289 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1292 error: function(data){
1293 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1299 url: "data/jsonp.php",
1301 crossDomain: crossDomain,
1302 data: "callback=??",
1303 success: function(data){
1304 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1307 error: function(data){
1308 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1314 url: "data/jsonp.php/??",
1316 crossDomain: crossDomain,
1317 success: function(data){
1318 ok( data.data, "JSON results returned (GET, REST-like)" );
1321 error: function(data){
1322 ok( false, "Ajax error JSON (GET, REST-like)" );
1328 url: "data/jsonp.php/???json=1",
1330 crossDomain: crossDomain,
1331 success: function(data){
1332 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1335 error: function(data){
1336 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1342 url: "data/jsonp.php",
1344 crossDomain: crossDomain,
1346 success: function(data){
1347 ok( data.data, "JSON results returned (GET, data obj callback)" );
1350 error: function(data){
1351 ok( false, "Ajax error JSON (GET, data obj callback)" );
1356 window.jsonpResults = function(data) {
1357 ok( data.data, "JSON results returned (GET, custom callback function)" );
1358 window.jsonpResults = undefined;
1363 url: "data/jsonp.php",
1365 crossDomain: crossDomain,
1366 jsonpCallback: "jsonpResults",
1367 success: function(data){
1368 ok( data.data, "JSON results returned (GET, custom callback name)" );
1371 error: function(data){
1372 ok( false, "Ajax error JSON (GET, custom callback name)" );
1378 url: "data/jsonp.php",
1380 crossDomain: crossDomain,
1381 jsonpCallback: "functionToCleanUp",
1382 success: function(data){
1383 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1384 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1388 url: "data/jsonp.php",
1390 crossDomain: crossDomain,
1391 jsonpCallback: "functionToCleanUp",
1392 beforeSend: function( jqXHR ) {
1397 xhr.error(function() {
1398 ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1399 strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1403 error: function(data){
1404 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1411 url: "data/jsonp.php",
1413 crossDomain: crossDomain,
1414 success: function(data){
1415 ok( data.data, "JSON results returned (POST, no callback)" );
1418 error: function(data){
1419 ok( false, "Ajax error JSON (GET, data obj callback)" );
1426 url: "data/jsonp.php",
1429 crossDomain: crossDomain,
1430 success: function(data){
1431 ok( data.data, "JSON results returned (POST, data callback)" );
1434 error: function(data){
1435 ok( false, "Ajax error JSON (POST, data callback)" );
1442 url: "data/jsonp.php",
1445 crossDomain: crossDomain,
1446 success: function(data){
1447 ok( data.data, "JSON results returned (POST, data obj callback)" );
1450 error: function(data){
1451 ok( false, "Ajax error JSON (POST, data obj callback)" );
1458 url: "data/jsonp.php",
1460 crossDomain: crossDomain,
1461 beforeSend: function(){
1462 strictEqual( this.cache, false, "cache must be false on JSON request" );
1469 url: "data/jsonp.php?callback=XXX",
1472 jsonpCallback: "XXX",
1473 crossDomain: crossDomain,
1474 beforeSend: function() {
1475 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1476 "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1479 success: function(data){
1480 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1483 error: function(data){
1484 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1492 test("jQuery.ajax() - script, Remote", function() {
1495 var base = window.location.href.replace(/[^\/]*$/, "");
1500 url: base + "data/test.js",
1502 success: function(data){
1503 ok( foobar, "Script results returned (GET, no callback)" );
1509 test("jQuery.ajax() - script, Remote with POST", function() {
1512 var base = window.location.href.replace(/[^\/]*$/, "");
1517 url: base + "data/test.js",
1520 success: function(data, status){
1521 ok( foobar, "Script results returned (POST, no callback)" );
1522 equals( status, "success", "Script results returned (POST, no callback)" );
1525 error: function(xhr) {
1526 ok( false, "ajax error, status code: " + xhr.status );
1532 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1535 var base = window.location.href.replace(/[^\/]*$/, "");
1536 base = base.replace(/^.*?\/\//, "//");
1541 url: base + "data/test.js",
1543 success: function(data){
1544 ok( foobar, "Script results returned (GET, no callback)" );
1550 test("jQuery.ajax() - malformed JSON", function() {
1556 url: "data/badjson.js",
1558 success: function(){
1559 ok( false, "Success." );
1562 error: function(xhr, msg, detailedMsg) {
1563 equals( "parsererror", msg, "A parse error occurred." );
1564 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1570 test("jQuery.ajax() - script by content-type", function() {
1578 url: "data/script.php",
1579 data: { header: "script" }
1583 url: "data/script.php",
1584 data: { header: "ecma" }
1587 ).then( start, start );
1590 test("jQuery.ajax() - json by content-type", function() {
1596 url: "data/json.php",
1597 data: { header: "json", json: "array" },
1598 success: function( json ) {
1599 ok( json.length >= 2, "Check length");
1600 equals( json[0].name, 'John', 'Check JSON: first, name' );
1601 equals( json[0].age, 21, 'Check JSON: first, age' );
1602 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1603 equals( json[1].age, 25, 'Check JSON: second, age' );
1609 test("jQuery.ajax() - json by content-type disabled with options", function() {
1615 url: url("data/json.php"),
1616 data: { header: "json", json: "array" },
1620 success: function( text ) {
1621 equals( typeof text , "string" , "json wasn't auto-determined" );
1622 var json = jQuery.parseJSON( text );
1623 ok( json.length >= 2, "Check length");
1624 equals( json[0].name, 'John', 'Check JSON: first, name' );
1625 equals( json[0].age, 21, 'Check JSON: first, age' );
1626 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1627 equals( json[1].age, 25, 'Check JSON: second, age' );
1633 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1636 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1637 ok( json.length >= 2, "Check length");
1638 equals( json[0].name, 'John', 'Check JSON: first, name' );
1639 equals( json[0].age, 21, 'Check JSON: first, age' );
1640 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1641 equals( json[1].age, 25, 'Check JSON: second, age' );
1646 test("jQuery.getJSON(String, Function) - JSON object", function() {
1649 jQuery.getJSON(url("data/json.php"), function(json) {
1650 if (json && json.data) {
1651 equals( json.data.lang, 'en', 'Check JSON: lang' );
1652 equals( json.data.length, 25, 'Check JSON: length' );
1658 test("jQuery.getJSON - Using Native JSON", function() {
1661 var old = window.JSON;
1663 parse: function(str){
1664 ok( true, "Verifying that parse method was run" );
1670 jQuery.getJSON(url("data/json.php"), function(json) {
1672 equals( json, true, "Verifying return value" );
1677 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1680 var base = window.location.href.replace(/[^\/]*$/, "");
1683 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1684 equals( json.data.lang, 'en', 'Check JSON: lang' );
1685 equals( json.data.length, 25, 'Check JSON: length' );
1690 test("jQuery.post - data", 3, function() {
1694 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1695 jQuery( 'math', xml ).each( function() {
1696 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1697 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1702 url: url('data/echoData.php'),
1710 success: function( data ) {
1711 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1714 ).then( start, start );
1718 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1723 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1724 jQuery('math', xml).each(function() {
1725 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1726 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1728 if ( ++done === 2 ) start();
1731 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1732 jQuery('math', xml).each(function() {
1733 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1734 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1736 if ( ++done === 2 ) start();
1740 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1745 jQuery.ajaxSetup({timeout: 1000});
1747 var pass = function() {
1749 if ( passed == 2 ) {
1750 ok( true, 'Check local and global callbacks after timeout' );
1751 jQuery('#main').unbind("ajaxError");
1756 var fail = function(a,b,c) {
1757 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1761 jQuery('#main').ajaxError(pass);
1765 url: url("data/name.php?wait=5"),
1771 jQuery.ajaxSetup({timeout: 0});
1774 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1776 jQuery.ajaxSetup({timeout: 50});
1781 url: url("data/name.php?wait=1"),
1783 ok( false, 'Check for local timeout failed' );
1786 success: function() {
1787 ok( true, 'Check for local timeout' );
1793 jQuery.ajaxSetup({timeout: 0});
1796 test("jQuery.ajax - simple get", function() {
1801 url: url("data/name.php?name=foo"),
1802 success: function(msg){
1803 equals( msg, 'bar', 'Check for GET' );
1809 test("jQuery.ajax - simple post", function() {
1814 url: url("data/name.php"),
1816 success: function(msg){
1817 equals( msg, 'pan', 'Check for POST' );
1823 test("ajaxSetup()", function() {
1827 url: url("data/name.php?name=foo"),
1828 success: function(msg){
1829 equals( msg, 'bar', 'Check for GET' );
1837 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1840 url: "data/name.php?wait=1",
1842 error: function(request, status) {
1843 ok( status != null, "status shouldn't be null in error handler" );
1844 equals( "timeout", status );
1851 test("data option: evaluate function values (#2806)", function() {
1854 url: "data/echoQuery.php",
1860 success: function(result) {
1861 equals( result, "key=value" );
1867 test("data option: empty bodies for non-GET requests", function() {
1870 url: "data/echoData.php",
1873 success: function(result) {
1874 equals( result, "" );
1880 var ifModifiedNow = new Date();
1882 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1884 test("jQuery.ajax - If-Modified-Since support" + label, function() {
1889 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1895 success: function(data, status) {
1896 equals(status, "success" );
1902 success: function(data, status) {
1903 if ( data === "FAIL" ) {
1904 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1905 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1907 equals(status, "notmodified");
1908 ok(data == null, "response body should be empty");
1913 // Do this because opera simply refuses to implement 304 handling :(
1914 // A feature-driven way of detecting this would be appreciated
1915 // See: http://gist.github.com/599419
1916 ok(jQuery.browser.opera, "error");
1917 ok(jQuery.browser.opera, "error");
1923 equals(false, "error");
1924 // Do this because opera simply refuses to implement 304 handling :(
1925 // A feature-driven way of detecting this would be appreciated
1926 // See: http://gist.github.com/599419
1927 ok(jQuery.browser.opera, "error");
1933 test("jQuery.ajax - Etag support" + label, function() {
1938 var url = "data/etag.php?ts=" + ifModifiedNow++;
1944 success: function(data, status) {
1945 equals(status, "success" );
1951 success: function(data, status) {
1952 if ( data === "FAIL" ) {
1953 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1954 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1956 equals(status, "notmodified");
1957 ok(data == null, "response body should be empty");
1962 // Do this because opera simply refuses to implement 304 handling :(
1963 // A feature-driven way of detecting this would be appreciated
1964 // See: http://gist.github.com/599419
1965 ok(jQuery.browser.opera, "error");
1966 ok(jQuery.browser.opera, "error");
1972 // Do this because opera simply refuses to implement 304 handling :(
1973 // A feature-driven way of detecting this would be appreciated
1974 // See: http://gist.github.com/599419
1975 ok(jQuery.browser.opera, "error");
1982 test("jQuery ajax - failing cross-domain", function() {
1991 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1992 success: function(){ ok( false , "success" ); },
1993 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1994 complete: function() { if ( ! --i ) start(); }
1998 url: 'http://www.google.com',
1999 success: function(){ ok( false , "success" ); },
2000 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
2001 complete: function() { if ( ! --i ) start(); }
2006 test("jQuery ajax - atom+xml", function() {
2011 url: url( 'data/atom+xml.php' ),
2012 success: function(){ ok( true , "success" ); },
2013 error: function(){ ok( false , "error" ); },
2014 complete: function() { start(); }
2019 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2020 var success = false;
2022 var xhr = jQuery.ajax({ url: window.location });
2027 ok( success, "document.location did not generate exception" );
2030 test( "jQuery.ajax - statusCode" , function() {
2037 function countComplete() {
2043 function createStatusCodes( name , isSuccess ) {
2044 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2047 ok( isSuccess , name );
2050 ok( ! isSuccess , name );
2056 "data/name.html": true,
2057 "data/someFileThatDoesNotExist.html": false
2058 } , function( uri , isSuccess ) {
2060 jQuery.ajax( url( uri ) , {
2061 statusCode: createStatusCodes( "in options" , isSuccess ),
2062 complete: countComplete
2065 jQuery.ajax( url( uri ) , {
2066 complete: countComplete
2067 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2069 jQuery.ajax( url( uri ) , {
2070 complete: function(jqXHR) {
2071 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2076 jQuery.ajax( url( uri ) , {
2077 complete: function(jqXHR) {
2078 setTimeout( function() {
2079 jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2085 jQuery.ajax( url( uri ) , {
2086 statusCode: createStatusCodes( "all (options)" , isSuccess ),
2087 complete: function(jqXHR) {
2088 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2089 setTimeout( function() {
2090 jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2094 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2096 var testString = "";
2098 jQuery.ajax( url( uri ), {
2099 success: function( a , b , jqXHR ) {
2100 ok( isSuccess , "success" );
2101 var statusCode = {};
2102 statusCode[ jqXHR.status ] = function() {
2105 jqXHR.statusCode( statusCode );
2108 error: function( jqXHR ) {
2109 ok( ! isSuccess , "error" );
2110 var statusCode = {};
2111 statusCode[ jqXHR.status ] = function() {
2114 jqXHR.statusCode( statusCode );
2117 complete: function() {
2118 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2119 ( isSuccess ? "success" : "error" ) + " callbacks" );
2127 test("jQuery.ajax - transitive conversions", function() {
2135 jQuery.ajax( url("data/json.php") , {
2137 "json myJson": function( data ) {
2138 ok( true , "converter called" );
2143 success: function() {
2144 ok( true , "Transitive conversion worked" );
2145 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2146 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2150 jQuery.ajax( url("data/json.php") , {
2152 "json myJson": function( data ) {
2153 ok( true , "converter called (*)" );
2157 contents: false, /* headers are wrong so we ignore them */
2158 dataType: "* myJson",
2159 success: function() {
2160 ok( true , "Transitive conversion worked (*)" );
2161 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2162 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2166 ).then( start , start );
2170 test("jQuery.ajax - overrideMimeType", function() {
2178 jQuery.ajax( url("data/json.php") , {
2179 beforeSend: function( xhr ) {
2180 xhr.overrideMimeType( "application/json" );
2182 success: function( json ) {
2183 ok( json.data , "Mimetype overriden using beforeSend" );
2187 jQuery.ajax( url("data/json.php") , {
2188 mimeType: "application/json",
2189 success: function( json ) {
2190 ok( json.data , "Mimetype overriden using mimeType option" );
2194 ).then( start , start );
2198 test("jQuery.ajax - abort in prefilter", function() {
2202 jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
2203 if ( options.abortInPrefilter ) {
2208 strictEqual( jQuery.ajax({
2209 abortInPrefilter: true,
2211 ok( false, "error callback called" );
2213 }), false, "Request was properly aborted early by the prefilter" );
2217 test("jQuery.ajax - active counter", function() {
2218 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );