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(".ajax() - 304", function() {
532 url: url("data/notmodified.php"),
533 success: function(){ ok(true, "304 ok"); },
534 // Do this because opera simply refuses to implement 304 handling :(
535 // A feature-driven way of detecting this would be appreciated
536 // See: http://gist.github.com/599419
537 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
538 complete: function(xhr){ start(); }
542 test(".load()) - 404 error callbacks", function() {
546 jQuery('#foo').ajaxStart(function(){
547 ok( true, "ajaxStart" );
548 }).ajaxStop(function(){
549 ok( true, "ajaxStop" );
551 }).ajaxSend(function(){
552 ok( true, "ajaxSend" );
553 }).ajaxComplete(function(){
554 ok( true, "ajaxComplete" );
555 }).ajaxError(function(){
556 ok( true, "ajaxError" );
557 }).ajaxSuccess(function(){
558 ok( false, "ajaxSuccess" );
561 jQuery("<div/>").load("data/404.html", function(){
562 ok(true, "complete");
566 test("jQuery.ajax() - abort", function() {
570 jQuery('#foo').ajaxStart(function(){
571 ok( true, "ajaxStart" );
572 }).ajaxStop(function(){
573 ok( true, "ajaxStop" );
575 }).ajaxSend(function(){
576 ok( true, "ajaxSend" );
577 }).ajaxComplete(function(){
578 ok( true, "ajaxComplete" );
581 var xhr = jQuery.ajax({
582 url: url("data/name.php?wait=5"),
583 beforeSend: function(){ ok(true, "beforeSend"); },
584 complete: function(){ ok(true, "complete"); }
587 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
590 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
593 test("Ajax events with context", function() {
597 var context = document.createElement("div");
600 equals( this, context, e.type );
603 function callback(msg){
605 equals( this, context, "context is preserved on callback " + msg );
609 function nocallback(msg){
611 equals( typeof this.url, "string", "context is settings on callback " + msg );
615 jQuery('#foo').add(context)
622 url: url("data/name.html"),
623 beforeSend: callback("beforeSend"),
624 success: callback("success"),
625 error: callback("error"),
627 callback("complete").call(this);
630 url: url("data/404.html"),
632 beforeSend: callback("beforeSend"),
633 error: callback("error"),
634 complete: function(){
635 callback("complete").call(this);
637 jQuery('#foo').add(context).unbind();
640 url: url("data/404.html"),
641 beforeSend: nocallback("beforeSend"),
642 error: nocallback("error"),
643 complete: function(){
644 nocallback("complete").call(this);
655 test("jQuery.ajax context modification", function() {
663 url: url("data/name.html"),
665 beforeSend: function(){
668 complete: function() {
673 equals( obj.test, "foo", "Make sure the original object is maintained." );
676 test("jQuery.ajax context modification through ajaxSetup", function() {
687 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
690 url: url("data/name.html"),
691 complete: function() {
692 strictEqual( this, obj, "Make sure the original object is maintained." );
694 url: url("data/name.html"),
696 complete: function() {
697 ok( this !== obj, "Make sure overidding context is possible." );
702 url: url("data/name.html"),
703 beforeSend: function(){
706 complete: function() {
707 ok( this !== obj, "Make sure unsetting context is possible." );
717 test("jQuery.ajax() - disabled globals", function() {
721 jQuery('#foo').ajaxStart(function(){
722 ok( false, "ajaxStart" );
723 }).ajaxStop(function(){
724 ok( false, "ajaxStop" );
725 }).ajaxSend(function(){
726 ok( false, "ajaxSend" );
727 }).ajaxComplete(function(){
728 ok( false, "ajaxComplete" );
729 }).ajaxError(function(){
730 ok( false, "ajaxError" );
731 }).ajaxSuccess(function(){
732 ok( false, "ajaxSuccess" );
737 url: url("data/name.html"),
738 beforeSend: function(){ ok(true, "beforeSend"); },
739 success: function(){ ok(true, "success"); },
740 error: function(){ ok(false, "error"); },
741 complete: function(){
742 ok(true, "complete");
743 setTimeout(function(){ start(); }, 13);
748 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
752 url: url("data/with_fries.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' );
763 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
767 url: url("data/with_fries_over_jsonp.php"),
768 dataType: "jsonp xml",
769 success: function(resp) {
770 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
771 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
772 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
775 error: function(_1,_2,error) {
782 test("jQuery.ajax - HEAD requests", function() {
787 url: url("data/name.html"),
789 success: function(data, status, xhr){
790 var h = xhr.getAllResponseHeaders();
791 ok( /Date/i.test(h), 'No Date in HEAD response' );
794 url: url("data/name.html"),
795 data: { whip_it: "good" },
797 success: function(data, status, xhr){
798 var h = xhr.getAllResponseHeaders();
799 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
808 test("jQuery.ajax - beforeSend", function() {
814 jQuery.ajaxSetup({ timeout: 0 });
817 url: url("data/name.html"),
818 beforeSend: function(xml) {
821 success: function(data) {
822 ok( check, "check beforeSend was executed" );
828 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
830 var request = jQuery.ajax({
831 url: url("data/name.html"),
832 beforeSend: function() {
833 ok( true, "beforeSend got called, canceling" );
836 success: function() {
837 ok( false, "request didn't get canceled" );
839 complete: function() {
840 ok( false, "request didn't get canceled" );
843 ok( false, "request didn't get canceled" );
846 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
849 test("jQuery.ajax - beforeSend, cancel request manually", function() {
851 var request = jQuery.ajax({
852 url: url("data/name.html"),
853 beforeSend: function(xhr) {
854 ok( true, "beforeSend got called, canceling" );
857 success: function() {
858 ok( false, "request didn't get canceled" );
860 complete: function() {
861 ok( false, "request didn't get canceled" );
864 ok( false, "request didn't get canceled" );
867 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
870 window.foobar = null;
871 window.testFoo = undefined;
873 test("jQuery.ajax - dataType html", function() {
877 var verifyEvaluation = function() {
878 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
879 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
886 url: url("data/test.html"),
887 success: function(data) {
888 jQuery("#ap").html(data);
889 ok( data.match(/^html text/), 'Check content for datatype html' );
890 setTimeout(verifyEvaluation, 600);
895 test("serialize()", function() {
898 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
899 jQuery("#search").after(
900 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
901 '<input type="number" id="html5number" name="number" value="43" />'
904 equals( jQuery('#form').serialize(),
905 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
906 'Check form serialization as query string');
908 equals( jQuery('#form :input').serialize(),
909 "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",
910 'Check input serialization as query string');
912 equals( jQuery('#testForm').serialize(),
913 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
914 'Check form serialization as query string');
916 equals( jQuery('#testForm :input').serialize(),
917 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
918 'Check input serialization as query string');
920 equals( jQuery('#form, #testForm').serialize(),
921 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
922 'Multiple form serialization as query string');
924 /* Temporarily disabled. Opera 10 has problems with form serialization.
925 equals( jQuery('#form, #testForm :input').serialize(),
926 "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=",
927 'Mixed form/input serialization as query string');
929 jQuery("#html5email, #html5number").remove();
932 test("jQuery.param()", function() {
935 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
937 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
938 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
940 params = {someName: [1, 2, 3], regularThing: "blah" };
941 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
943 params = {foo: ['a', 'b', 'c']};
944 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
946 params = {foo: ["baz", 42, "All your base are belong to us"] };
947 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
949 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
950 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" );
952 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?" };
953 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" );
955 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 ] };
956 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" );
958 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?" };
959 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" );
961 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." );
963 // Make sure empty arrays and objects are handled #6481
964 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
965 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
966 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
969 equals( jQuery.param({"jquery": "1.4.2"}), "jquery=1.4.2", "Check that object with a jQuery property get serialized correctly" );
971 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");
973 jQuery.ajaxSetup({ traditional: true });
975 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
976 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
978 params = {someName: [1, 2, 3], regularThing: "blah" };
979 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
981 params = {foo: ['a', 'b', 'c']};
982 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
984 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
985 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
987 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
988 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" );
990 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?" };
991 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" );
993 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 ] };
994 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)" );
996 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?" };
997 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" );
999 params = { param1: null };
1000 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
1002 params = {'test': {'length': 3, 'foo': 'bar'} };
1003 equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
1006 test("synchronous request", function() {
1008 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
1011 test("synchronous request with callbacks", function() {
1014 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
1015 ok( /^{ "data"/.test( result ), "check returned text" );
1018 test("pass-through request object", function() {
1022 var target = "data/name.html";
1023 var successCount = 0;
1026 var success = function() {
1029 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
1031 errorEx += ": " + xml.status;
1033 jQuery("#foo").one('ajaxStop', function () {
1034 equals(successCount, 5, "Check all ajax calls successful");
1035 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
1036 jQuery("#foo").unbind('ajaxError');
1041 ok( jQuery.get(url(target), success), "get" );
1042 ok( jQuery.post(url(target), success), "post" );
1043 ok( jQuery.getScript(url("data/test.js"), success), "script" );
1044 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
1045 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
1048 test("ajax cache", function () {
1055 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
1056 var re = /_=(.*?)(&|$)/g;
1058 for (var i = 0; i < 6; i++) {
1059 var ret = re.exec(s.url);
1065 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
1066 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
1071 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
1072 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
1073 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
1074 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
1075 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
1076 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
1081 * The assertions expect that the passed-in object will be modified,
1082 * which shouldn't be the case. Fixes #5439.
1083 test("global ajaxSettings", function() {
1086 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
1087 var orig = { url: "data/with_fries.xml" };
1090 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
1092 t = jQuery.extend({}, orig);
1095 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
1097 t = jQuery.extend({}, orig);
1098 t.data = { zoo: 'a', ping: 'b' };
1100 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' }" );
1102 jQuery.ajaxSettings = tmp;
1106 test("load(String)", function() {
1108 stop(); // check if load can be called with only url
1109 jQuery('#first').load("data/name.html", start);
1112 test("load('url selector')", function() {
1114 stop(); // check if load can be called with only url
1115 jQuery('#first').load("data/test3.html div.user", function(){
1116 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
1121 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
1124 jQuery.ajaxSetup({ dataType: "json" });
1125 jQuery("#first").ajaxComplete(function (e, xml, s) {
1126 equals( s.dataType, "html", "Verify the load() dataType was html" );
1127 jQuery("#first").unbind("ajaxComplete");
1128 jQuery.ajaxSetup({ dataType: "" });
1131 jQuery('#first').load("data/test3.html");
1134 test("load(String, Function) - simple: inject text into DOM", function() {
1137 jQuery('#first').load(url("data/name.html"), function() {
1138 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
1143 test("load(String, Function) - check scripts", function() {
1147 var verifyEvaluation = function() {
1148 equals( foobar, "bar", 'Check if script src was evaluated after load' );
1149 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
1153 jQuery('#first').load(url('data/test.html'), function() {
1154 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
1155 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1156 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1157 setTimeout(verifyEvaluation, 600);
1161 test("load(String, Function) - check file with only a script tag", function() {
1165 jQuery('#first').load(url('data/test2.html'), function() {
1166 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
1167 equals( testFoo, "foo", 'Check if script was evaluated after load' );
1173 test("load(String, Function) - dataFilter in ajaxSettings", function() {
1176 jQuery.ajaxSetup({ dataFilter: function() { return "Hello World"; } });
1177 var div = jQuery("<div/>").load(url("data/name.html"), function(responseText) {
1178 strictEqual( div.html(), "Hello World" , "Test div was filled with filtered data" );
1179 strictEqual( responseText, "Hello World" , "Test callback receives filtered data" );
1180 jQuery.ajaxSetup({ dataFilter: 0 });
1185 test("load(String, Object, Function)", function() {
1189 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
1190 var $post = jQuery(this).find('#post');
1191 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
1192 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
1197 test("load(String, String, Function)", function() {
1201 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
1202 var $get = jQuery(this).find('#get');
1203 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
1204 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
1209 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
1212 jQuery.get(url('data/dashboard.xml'), function(xml) {
1214 jQuery('tab', xml).each(function() {
1215 content.push(jQuery(this).text());
1217 equals( content[0], 'blabla', 'Check first tab');
1218 equals( content[1], 'blublu', 'Check second tab');
1223 test("jQuery.getScript(String, Function) - with callback", function() {
1226 jQuery.getScript(url("data/test.js"), function( data, _, jqXHR ) {
1227 equals( foobar, "bar", 'Check if script was evaluated' );
1228 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" );
1229 setTimeout(start, 100);
1233 test("jQuery.getScript(String, Function) - no callback", function() {
1236 jQuery.getScript(url("data/test.js"), function(){
1241 jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) {
1243 test("jQuery.ajax() - JSONP, " + label, function() {
1247 function plus(){ if ( ++count == 18 ) start(); }
1252 url: "data/jsonp.php",
1254 crossDomain: crossDomain,
1255 success: function(data){
1256 ok( data.data, "JSON results returned (GET, no callback)" );
1259 error: function(data){
1260 ok( false, "Ajax error JSON (GET, no callback)" );
1266 url: "data/jsonp.php?callback=?",
1268 crossDomain: crossDomain,
1269 success: function(data){
1270 ok( data.data, "JSON results returned (GET, url callback)" );
1273 error: function(data){
1274 ok( false, "Ajax error JSON (GET, url callback)" );
1280 url: "data/jsonp.php",
1282 crossDomain: crossDomain,
1284 success: function(data){
1285 ok( data.data, "JSON results returned (GET, data callback)" );
1288 error: function(data){
1289 ok( false, "Ajax error JSON (GET, data callback)" );
1295 url: "data/jsonp.php?callback=??",
1297 crossDomain: crossDomain,
1298 success: function(data){
1299 ok( data.data, "JSON results returned (GET, url context-free callback)" );
1302 error: function(data){
1303 ok( false, "Ajax error JSON (GET, url context-free callback)" );
1309 url: "data/jsonp.php",
1311 crossDomain: crossDomain,
1312 data: "callback=??",
1313 success: function(data){
1314 ok( data.data, "JSON results returned (GET, data context-free callback)" );
1317 error: function(data){
1318 ok( false, "Ajax error JSON (GET, data context-free callback)" );
1324 url: "data/jsonp.php/??",
1326 crossDomain: crossDomain,
1327 success: function(data){
1328 ok( data.data, "JSON results returned (GET, REST-like)" );
1331 error: function(data){
1332 ok( false, "Ajax error JSON (GET, REST-like)" );
1338 url: "data/jsonp.php/???json=1",
1340 crossDomain: crossDomain,
1341 success: function(data){
1342 strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
1345 error: function(data){
1346 ok( false, "Ajax error JSON (GET, REST-like with param)" );
1352 url: "data/jsonp.php",
1354 crossDomain: crossDomain,
1356 success: function(data){
1357 ok( data.data, "JSON results returned (GET, data obj callback)" );
1360 error: function(data){
1361 ok( false, "Ajax error JSON (GET, data obj callback)" );
1366 window.jsonpResults = function(data) {
1367 ok( data.data, "JSON results returned (GET, custom callback function)" );
1368 window.jsonpResults = undefined;
1373 url: "data/jsonp.php",
1375 crossDomain: crossDomain,
1376 jsonpCallback: "jsonpResults",
1377 success: function(data){
1378 ok( data.data, "JSON results returned (GET, custom callback name)" );
1381 error: function(data){
1382 ok( false, "Ajax error JSON (GET, custom callback name)" );
1388 url: "data/jsonp.php",
1390 crossDomain: crossDomain,
1391 jsonpCallback: "functionToCleanUp",
1392 success: function(data){
1393 ok( data.data, "JSON results returned (GET, custom callback name to be cleaned up)" );
1394 strictEqual( window.functionToCleanUp, undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
1398 url: "data/jsonp.php",
1400 crossDomain: crossDomain,
1401 jsonpCallback: "functionToCleanUp",
1402 beforeSend: function( jqXHR ) {
1407 xhr.error(function() {
1408 ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1409 strictEqual( window.functionToCleanUp, undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
1413 error: function(data){
1414 ok( false, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
1421 url: "data/jsonp.php",
1423 crossDomain: crossDomain,
1424 success: function(data){
1425 ok( data.data, "JSON results returned (POST, no callback)" );
1428 error: function(data){
1429 ok( false, "Ajax error JSON (GET, data obj callback)" );
1436 url: "data/jsonp.php",
1439 crossDomain: crossDomain,
1440 success: function(data){
1441 ok( data.data, "JSON results returned (POST, data callback)" );
1444 error: function(data){
1445 ok( false, "Ajax error JSON (POST, data callback)" );
1452 url: "data/jsonp.php",
1455 crossDomain: crossDomain,
1456 success: function(data){
1457 ok( data.data, "JSON results returned (POST, data obj callback)" );
1460 error: function(data){
1461 ok( false, "Ajax error JSON (POST, data obj callback)" );
1468 url: "data/jsonp.php",
1470 crossDomain: crossDomain,
1471 beforeSend: function(){
1472 strictEqual( this.cache, false, "cache must be false on JSON request" );
1479 url: "data/jsonp.php?callback=XXX",
1482 jsonpCallback: "XXX",
1483 crossDomain: crossDomain,
1484 beforeSend: function() {
1485 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) ,
1486 "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
1489 success: function(data){
1490 ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" );
1493 error: function(data){
1494 ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" );
1502 test("jQuery.ajax() - script, Remote", function() {
1505 var base = window.location.href.replace(/[^\/]*$/, "");
1510 url: base + "data/test.js",
1512 success: function(data){
1513 ok( foobar, "Script results returned (GET, no callback)" );
1519 test("jQuery.ajax() - script, Remote with POST", function() {
1522 var base = window.location.href.replace(/[^\/]*$/, "");
1527 url: base + "data/test.js",
1530 success: function(data, status){
1531 ok( foobar, "Script results returned (POST, no callback)" );
1532 equals( status, "success", "Script results returned (POST, no callback)" );
1535 error: function(xhr) {
1536 ok( false, "ajax error, status code: " + xhr.status );
1542 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1545 var base = window.location.href.replace(/[^\/]*$/, "");
1546 base = base.replace(/^.*?\/\//, "//");
1551 url: base + "data/test.js",
1553 success: function(data){
1554 ok( foobar, "Script results returned (GET, no callback)" );
1560 test("jQuery.ajax() - malformed JSON", function() {
1566 url: "data/badjson.js",
1568 success: function(){
1569 ok( false, "Success." );
1572 error: function(xhr, msg, detailedMsg) {
1573 equals( "parsererror", msg, "A parse error occurred." );
1574 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1580 test("jQuery.ajax() - script by content-type", function() {
1588 url: "data/script.php",
1589 data: { header: "script" }
1593 url: "data/script.php",
1594 data: { header: "ecma" }
1597 ).then( start, start );
1600 test("jQuery.ajax() - json by content-type", function() {
1606 url: "data/json.php",
1607 data: { header: "json", json: "array" },
1608 success: function( json ) {
1609 ok( json.length >= 2, "Check length");
1610 equals( json[0].name, 'John', 'Check JSON: first, name' );
1611 equals( json[0].age, 21, 'Check JSON: first, age' );
1612 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1613 equals( json[1].age, 25, 'Check JSON: second, age' );
1619 test("jQuery.ajax() - json by content-type disabled with options", function() {
1625 url: url("data/json.php"),
1626 data: { header: "json", json: "array" },
1630 success: function( text ) {
1631 equals( typeof text , "string" , "json wasn't auto-determined" );
1632 var json = jQuery.parseJSON( text );
1633 ok( json.length >= 2, "Check length");
1634 equals( json[0].name, 'John', 'Check JSON: first, name' );
1635 equals( json[0].age, 21, 'Check JSON: first, age' );
1636 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1637 equals( json[1].age, 25, 'Check JSON: second, age' );
1643 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1646 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1647 ok( json.length >= 2, "Check length");
1648 equals( json[0].name, 'John', 'Check JSON: first, name' );
1649 equals( json[0].age, 21, 'Check JSON: first, age' );
1650 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1651 equals( json[1].age, 25, 'Check JSON: second, age' );
1656 test("jQuery.getJSON(String, Function) - JSON object", function() {
1659 jQuery.getJSON(url("data/json.php"), function(json) {
1660 if (json && json.data) {
1661 equals( json.data.lang, 'en', 'Check JSON: lang' );
1662 equals( json.data.length, 25, 'Check JSON: length' );
1668 test("jQuery.getJSON - Using Native JSON", function() {
1671 var old = window.JSON;
1673 parse: function(str){
1674 ok( true, "Verifying that parse method was run" );
1680 jQuery.getJSON(url("data/json.php"), function(json) {
1682 equals( json, true, "Verifying return value" );
1687 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1690 var base = window.location.href.replace(/[^\/]*$/, "");
1693 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1694 equals( json.data.lang, 'en', 'Check JSON: lang' );
1695 equals( json.data.length, 25, 'Check JSON: length' );
1700 test("jQuery.post - data", 3, function() {
1704 jQuery.post( url( "data/name.php" ), { xml: "5-2", length: 3 }, function( xml ) {
1705 jQuery( 'math', xml ).each( function() {
1706 equals( jQuery( 'calculation', this ).text(), '5-2', 'Check for XML' );
1707 equals( jQuery( 'result', this ).text(), '3', 'Check for XML' );
1712 url: url('data/echoData.php'),
1720 success: function( data ) {
1721 strictEqual( data, 'test%5Blength%5D=7&test%5Bfoo%5D=bar', 'Check if a sub-object with a length param is serialized correctly');
1724 ).then( start, start );
1728 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1733 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1734 jQuery('math', xml).each(function() {
1735 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1736 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1738 if ( ++done === 2 ) start();
1741 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1742 jQuery('math', xml).each(function() {
1743 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1744 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1746 if ( ++done === 2 ) start();
1750 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1755 jQuery.ajaxSetup({timeout: 1000});
1757 var pass = function() {
1759 if ( passed == 2 ) {
1760 ok( true, 'Check local and global callbacks after timeout' );
1761 jQuery('#main').unbind("ajaxError");
1766 var fail = function(a,b,c) {
1767 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1771 jQuery('#main').ajaxError(pass);
1775 url: url("data/name.php?wait=5"),
1781 jQuery.ajaxSetup({timeout: 0});
1784 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1786 jQuery.ajaxSetup({timeout: 50});
1791 url: url("data/name.php?wait=1"),
1793 ok( false, 'Check for local timeout failed' );
1796 success: function() {
1797 ok( true, 'Check for local timeout' );
1803 jQuery.ajaxSetup({timeout: 0});
1806 test("jQuery.ajax - simple get", function() {
1811 url: url("data/name.php?name=foo"),
1812 success: function(msg){
1813 equals( msg, 'bar', 'Check for GET' );
1819 test("jQuery.ajax - simple post", function() {
1824 url: url("data/name.php"),
1826 success: function(msg){
1827 equals( msg, 'pan', 'Check for POST' );
1833 test("ajaxSetup()", function() {
1837 url: url("data/name.php?name=foo"),
1838 success: function(msg){
1839 equals( msg, 'bar', 'Check for GET' );
1847 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1850 url: "data/name.php?wait=1",
1852 error: function(request, status) {
1853 ok( status != null, "status shouldn't be null in error handler" );
1854 equals( "timeout", status );
1861 test("data option: evaluate function values (#2806)", function() {
1864 url: "data/echoQuery.php",
1870 success: function(result) {
1871 equals( result, "key=value" );
1877 test("data option: empty bodies for non-GET requests", function() {
1880 url: "data/echoData.php",
1883 success: function(result) {
1884 equals( result, "" );
1890 var ifModifiedNow = new Date();
1892 jQuery.each( { " (cache)": true, " (no cache)": false }, function( label, cache ) {
1894 test("jQuery.ajax - If-Modified-Since support" + label, function() {
1899 var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
1905 success: function(data, status) {
1906 equals(status, "success" );
1912 success: function(data, status) {
1913 if ( data === "FAIL" ) {
1914 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1915 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1917 equals(status, "notmodified");
1918 ok(data == null, "response body should be empty");
1923 // Do this because opera simply refuses to implement 304 handling :(
1924 // A feature-driven way of detecting this would be appreciated
1925 // See: http://gist.github.com/599419
1926 ok(jQuery.browser.opera, "error");
1927 ok(jQuery.browser.opera, "error");
1933 equals(false, "error");
1934 // Do this because opera simply refuses to implement 304 handling :(
1935 // A feature-driven way of detecting this would be appreciated
1936 // See: http://gist.github.com/599419
1937 ok(jQuery.browser.opera, "error");
1943 test("jQuery.ajax - Etag support" + label, function() {
1948 var url = "data/etag.php?ts=" + ifModifiedNow++;
1954 success: function(data, status) {
1955 equals(status, "success" );
1961 success: function(data, status) {
1962 if ( data === "FAIL" ) {
1963 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1964 ok(jQuery.browser.opera, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1966 equals(status, "notmodified");
1967 ok(data == null, "response body should be empty");
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");
1976 ok(jQuery.browser.opera, "error");
1982 // Do this because opera simply refuses to implement 304 handling :(
1983 // A feature-driven way of detecting this would be appreciated
1984 // See: http://gist.github.com/599419
1985 ok(jQuery.browser.opera, "error");
1992 test("jQuery ajax - failing cross-domain", function() {
2001 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
2002 success: function(){ ok( false , "success" ); },
2003 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
2004 complete: function() { if ( ! --i ) start(); }
2008 url: 'http://www.google.com',
2009 success: function(){ ok( false , "success" ); },
2010 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
2011 complete: function() { if ( ! --i ) start(); }
2016 test("jQuery ajax - atom+xml", function() {
2021 url: url( 'data/atom+xml.php' ),
2022 success: function(){ ok( true , "success" ); },
2023 error: function(){ ok( false , "error" ); },
2024 complete: function() { start(); }
2029 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
2030 var success = false;
2032 var xhr = jQuery.ajax({ url: window.location });
2037 ok( success, "document.location did not generate exception" );
2040 test( "jQuery.ajax - statusCode" , function() {
2047 function countComplete() {
2053 function createStatusCodes( name , isSuccess ) {
2054 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
2057 ok( isSuccess , name );
2060 ok( ! isSuccess , name );
2066 "data/name.html": true,
2067 "data/someFileThatDoesNotExist.html": false
2068 } , function( uri , isSuccess ) {
2070 jQuery.ajax( url( uri ) , {
2071 statusCode: createStatusCodes( "in options" , isSuccess ),
2072 complete: countComplete
2075 jQuery.ajax( url( uri ) , {
2076 complete: countComplete
2077 }).statusCode( createStatusCodes( "immediately with method" , isSuccess ) );
2079 jQuery.ajax( url( uri ) , {
2080 complete: function(jqXHR) {
2081 jqXHR.statusCode( createStatusCodes( "on complete" , isSuccess ) );
2086 jQuery.ajax( url( uri ) , {
2087 complete: function(jqXHR) {
2088 setTimeout( function() {
2089 jqXHR.statusCode( createStatusCodes( "very late binding" , isSuccess ) );
2095 jQuery.ajax( url( uri ) , {
2096 statusCode: createStatusCodes( "all (options)" , isSuccess ),
2097 complete: function(jqXHR) {
2098 jqXHR.statusCode( createStatusCodes( "all (on complete)" , isSuccess ) );
2099 setTimeout( function() {
2100 jqXHR.statusCode( createStatusCodes( "all (very late binding)" , isSuccess ) );
2104 }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) );
2106 var testString = "";
2108 jQuery.ajax( url( uri ), {
2109 success: function( a , b , jqXHR ) {
2110 ok( isSuccess , "success" );
2111 var statusCode = {};
2112 statusCode[ jqXHR.status ] = function() {
2115 jqXHR.statusCode( statusCode );
2118 error: function( jqXHR ) {
2119 ok( ! isSuccess , "error" );
2120 var statusCode = {};
2121 statusCode[ jqXHR.status ] = function() {
2124 jqXHR.statusCode( statusCode );
2127 complete: function() {
2128 strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " +
2129 ( isSuccess ? "success" : "error" ) + " callbacks" );
2137 test("jQuery.ajax - transitive conversions", function() {
2145 jQuery.ajax( url("data/json.php") , {
2147 "json myJson": function( data ) {
2148 ok( true , "converter called" );
2153 success: function() {
2154 ok( true , "Transitive conversion worked" );
2155 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" );
2156 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" );
2160 jQuery.ajax( url("data/json.php") , {
2162 "json myJson": function( data ) {
2163 ok( true , "converter called (*)" );
2167 contents: false, /* headers are wrong so we ignore them */
2168 dataType: "* myJson",
2169 success: function() {
2170 ok( true , "Transitive conversion worked (*)" );
2171 strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" );
2172 strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" );
2176 ).then( start , start );
2180 test("jQuery.ajax - active counter", function() {
2181 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );