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() - responseText on error", function() {
250 url: url("data/errorWithText.php"),
251 error: function(xhr) {
252 strictEqual( xhr.responseText , "plain text message" , "Test jXHR.responseText is filled for HTTP errors" );
254 complete: function() {
260 test(".ajax() - headers" , function() {
266 var requestHeaders = {
268 "SometHing-elsE": "other value",
269 OthEr: "something else"
274 for( i in requestHeaders ) {
278 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
279 headers: requestHeaders,
280 success: function( data , _ , xhr ) {
282 for ( i in requestHeaders ) {
283 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
285 tmp = tmp.join( "" );
287 equals( data , tmp , "Headers were sent" );
288 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
291 error: function(){ ok(false, "error"); }
296 test(".ajax() - contentType" , function() {
310 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
312 success: function( data ) {
313 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
315 complete: function() {
320 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
322 success: function( data ) {
323 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
325 complete: function() {
332 test(".ajax() - hash", function() {
336 url: "data/name.html#foo",
337 beforeSend: function( xhr, settings ) {
338 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
344 url: "data/name.html?abc#foo",
345 beforeSend: function( xhr, settings ) {
346 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
352 url: "data/name.html?abc#foo",
353 data: { "test": 123 },
354 beforeSend: function( xhr, settings ) {
355 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
361 test(".ajax() - 304", function() {
366 url: url("data/notmodified.php"),
367 success: function(){ ok(true, "304 ok"); },
368 // Do this because opera simply refuses to implement 304 handling :(
369 // A feature-driven way of detecting this would be appreciated
370 // See: http://gist.github.com/599419
371 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
372 complete: function(xhr){ start(); }
376 test(".load()) - 404 error callbacks", function() {
380 jQuery('#foo').ajaxStart(function(){
381 ok( true, "ajaxStart" );
382 }).ajaxStop(function(){
383 ok( true, "ajaxStop" );
385 }).ajaxSend(function(){
386 ok( true, "ajaxSend" );
387 }).ajaxComplete(function(){
388 ok( true, "ajaxComplete" );
389 }).ajaxError(function(){
390 ok( true, "ajaxError" );
391 }).ajaxSuccess(function(){
392 ok( false, "ajaxSuccess" );
395 jQuery("<div/>").load("data/404.html", function(){
396 ok(true, "complete");
400 test("jQuery.ajax() - abort", function() {
404 jQuery('#foo').ajaxStart(function(){
405 ok( true, "ajaxStart" );
406 }).ajaxStop(function(){
407 ok( true, "ajaxStop" );
409 }).ajaxSend(function(){
410 ok( true, "ajaxSend" );
411 }).ajaxComplete(function(){
412 ok( true, "ajaxComplete" );
415 var xhr = jQuery.ajax({
416 url: url("data/name.php?wait=5"),
417 beforeSend: function(){ ok(true, "beforeSend"); },
418 complete: function(){ ok(true, "complete"); }
421 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
424 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
427 test("Ajax events with context", function() {
431 var context = document.createElement("div");
434 equals( this, context, e.type );
437 function callback(msg){
439 equals( this, context, "context is preserved on callback " + msg );
443 function nocallback(msg){
445 equals( typeof this.url, "string", "context is settings on callback " + msg );
449 jQuery('#foo').add(context)
456 url: url("data/name.html"),
457 beforeSend: callback("beforeSend"),
458 success: callback("success"),
459 error: callback("error"),
461 callback("complete").call(this);
464 url: url("data/404.html"),
466 beforeSend: callback("beforeSend"),
467 error: callback("error"),
468 complete: function(){
469 callback("complete").call(this);
471 jQuery('#foo').add(context).unbind();
474 url: url("data/404.html"),
475 beforeSend: nocallback("beforeSend"),
476 error: nocallback("error"),
477 complete: function(){
478 nocallback("complete").call(this);
489 test("jQuery.ajax context modification", function() {
497 url: url("data/name.html"),
499 beforeSend: function(){
502 complete: function() {
507 equals( obj.test, "foo", "Make sure the original object is maintained." );
510 test("jQuery.ajax() - disabled globals", function() {
514 jQuery('#foo').ajaxStart(function(){
515 ok( false, "ajaxStart" );
516 }).ajaxStop(function(){
517 ok( false, "ajaxStop" );
518 }).ajaxSend(function(){
519 ok( false, "ajaxSend" );
520 }).ajaxComplete(function(){
521 ok( false, "ajaxComplete" );
522 }).ajaxError(function(){
523 ok( false, "ajaxError" );
524 }).ajaxSuccess(function(){
525 ok( false, "ajaxSuccess" );
530 url: url("data/name.html"),
531 beforeSend: function(){ ok(true, "beforeSend"); },
532 success: function(){ ok(true, "success"); },
533 error: function(){ ok(false, "error"); },
534 complete: function(){
535 ok(true, "complete");
536 setTimeout(function(){ start(); }, 13);
541 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
545 url: url("data/with_fries.xml"),
547 success: function(resp) {
548 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
549 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
550 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
556 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
560 url: url("data/with_fries_over_jsonp.php"),
561 dataType: "jsonp xml",
562 success: function(resp) {
563 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
564 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
565 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
568 error: function(_1,_2,error) {
575 test("jQuery.ajax - HEAD requests", function() {
580 url: url("data/name.html"),
582 success: function(data, status, xhr){
583 var h = xhr.getAllResponseHeaders();
584 ok( /Date/i.test(h), 'No Date in HEAD response' );
587 url: url("data/name.html"),
588 data: { whip_it: "good" },
590 success: function(data, status, xhr){
591 var h = xhr.getAllResponseHeaders();
592 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
601 test("jQuery.ajax - beforeSend", function() {
607 jQuery.ajaxSetup({ timeout: 0 });
610 url: url("data/name.html"),
611 beforeSend: function(xml) {
614 success: function(data) {
615 ok( check, "check beforeSend was executed" );
621 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
623 var request = jQuery.ajax({
624 url: url("data/name.html"),
625 beforeSend: function() {
626 ok( true, "beforeSend got called, canceling" );
629 success: function() {
630 ok( false, "request didn't get canceled" );
632 complete: function() {
633 ok( false, "request didn't get canceled" );
636 ok( false, "request didn't get canceled" );
639 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
642 test("jQuery.ajax - beforeSend, cancel request manually", function() {
644 var request = jQuery.ajax({
645 url: url("data/name.html"),
646 beforeSend: function(xhr) {
647 ok( true, "beforeSend got called, canceling" );
650 success: function() {
651 ok( false, "request didn't get canceled" );
653 complete: function() {
654 ok( false, "request didn't get canceled" );
657 ok( false, "request didn't get canceled" );
660 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
663 window.foobar = null;
664 window.testFoo = undefined;
666 test("jQuery.ajax - dataType html", function() {
670 var verifyEvaluation = function() {
671 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
672 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
679 url: url("data/test.html"),
680 success: function(data) {
681 jQuery("#ap").html(data);
682 ok( data.match(/^html text/), 'Check content for datatype html' );
683 setTimeout(verifyEvaluation, 600);
688 test("serialize()", function() {
691 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
692 jQuery("#search").after(
693 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
694 '<input type="number" id="html5number" name="number" value="43" />'
697 equals( jQuery('#form').serialize(),
698 "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",
699 'Check form serialization as query string');
701 equals( jQuery('#form :input').serialize(),
702 "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",
703 'Check input serialization as query string');
705 equals( jQuery('#testForm').serialize(),
706 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
707 'Check form serialization as query string');
709 equals( jQuery('#testForm :input').serialize(),
710 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
711 'Check input serialization as query string');
713 equals( jQuery('#form, #testForm').serialize(),
714 "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=",
715 'Multiple form serialization as query string');
717 /* Temporarily disabled. Opera 10 has problems with form serialization.
718 equals( jQuery('#form, #testForm :input').serialize(),
719 "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=",
720 'Mixed form/input serialization as query string');
722 jQuery("#html5email, #html5number").remove();
725 test("jQuery.param()", function() {
728 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
730 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
731 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
733 params = {someName: [1, 2, 3], regularThing: "blah" };
734 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
736 params = {foo: ['a', 'b', 'c']};
737 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
739 params = {foo: ["baz", 42, "All your base are belong to us"] };
740 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
742 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
743 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" );
745 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?" };
746 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" );
748 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 ] };
749 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" );
751 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?" };
752 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" );
754 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." );
756 // Make sure empty arrays and objects are handled #6481
757 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
758 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
759 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
761 jQuery.ajaxSetup({ traditional: true });
763 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
764 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
766 params = {someName: [1, 2, 3], regularThing: "blah" };
767 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
769 params = {foo: ['a', 'b', 'c']};
770 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
772 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
773 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
775 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
776 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" );
778 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?" };
779 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" );
781 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 ] };
782 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)" );
784 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?" };
785 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" );
787 params = { param1: null };
788 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
791 test("synchronous request", function() {
793 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
796 test("synchronous request with callbacks", function() {
799 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
800 ok( /^{ "data"/.test( result ), "check returned text" );
803 test("pass-through request object", function() {
807 var target = "data/name.html";
808 var successCount = 0;
811 var success = function() {
814 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
816 errorEx += ": " + xml.status;
818 jQuery("#foo").one('ajaxStop', function () {
819 equals(successCount, 5, "Check all ajax calls successful");
820 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
821 jQuery("#foo").unbind('ajaxError');
826 ok( jQuery.get(url(target), success), "get" );
827 ok( jQuery.post(url(target), success), "post" );
828 ok( jQuery.getScript(url("data/test.js"), success), "script" );
829 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
830 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
833 test("ajax cache", function () {
840 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
841 var re = /_=(.*?)(&|$)/g;
843 for (var i = 0; i < 6; i++) {
844 var ret = re.exec(s.url);
850 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
851 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
856 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
857 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
858 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
859 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
860 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
861 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
866 * The assertions expect that the passed-in object will be modified,
867 * which shouldn't be the case. Fixes #5439.
868 test("global ajaxSettings", function() {
871 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
872 var orig = { url: "data/with_fries.xml" };
875 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
877 t = jQuery.extend({}, orig);
880 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
882 t = jQuery.extend({}, orig);
883 t.data = { zoo: 'a', ping: 'b' };
885 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' }" );
887 jQuery.ajaxSettings = tmp;
891 test("load(String)", function() {
893 stop(); // check if load can be called with only url
894 jQuery('#first').load("data/name.html", start);
897 test("load('url selector')", function() {
899 stop(); // check if load can be called with only url
900 jQuery('#first').load("data/test3.html div.user", function(){
901 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
906 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
909 jQuery.ajaxSetup({ dataType: "json" });
910 jQuery("#first").ajaxComplete(function (e, xml, s) {
911 equals( s.dataType, "html", "Verify the load() dataType was html" );
912 jQuery("#first").unbind("ajaxComplete");
913 jQuery.ajaxSetup({ dataType: "" });
916 jQuery('#first').load("data/test3.html");
919 test("load(String, Function) - simple: inject text into DOM", function() {
922 jQuery('#first').load(url("data/name.html"), function() {
923 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
928 test("load(String, Function) - check scripts", function() {
932 var verifyEvaluation = function() {
933 equals( foobar, "bar", 'Check if script src was evaluated after load' );
934 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
938 jQuery('#first').load(url('data/test.html'), function() {
939 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
940 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
941 equals( testFoo, "foo", 'Check if script was evaluated after load' );
942 setTimeout(verifyEvaluation, 600);
946 test("load(String, Function) - check file with only a script tag", function() {
950 jQuery('#first').load(url('data/test2.html'), function() {
951 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
952 equals( testFoo, "foo", 'Check if script was evaluated after load' );
958 test("load(String, Object, Function)", function() {
962 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
963 var $post = jQuery(this).find('#post');
964 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
965 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
970 test("load(String, String, Function)", function() {
974 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
975 var $get = jQuery(this).find('#get');
976 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
977 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
982 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
985 jQuery.get(url('data/dashboard.xml'), function(xml) {
987 jQuery('tab', xml).each(function() {
988 content.push(jQuery(this).text());
990 equals( content[0], 'blabla', 'Check first tab');
991 equals( content[1], 'blublu', 'Check second tab');
996 test("jQuery.getScript(String, Function) - with callback", function() {
999 jQuery.getScript(url("data/test.js"), function() {
1000 equals( foobar, "bar", 'Check if script was evaluated' );
1001 setTimeout(start, 100);
1005 test("jQuery.getScript(String, Function) - no callback", function() {
1008 jQuery.getScript(url("data/test.js"), function(){
1013 test("jQuery.ajax() - JSONP, Local", function() {
1017 function plus(){ if ( ++count == 9 ) start(); }
1022 url: "data/jsonp.php",
1024 success: function(data){
1025 ok( data.data, "JSON results returned (GET, no callback)" );
1028 error: function(data){
1029 ok( false, "Ajax error JSON (GET, no callback)" );
1035 url: "data/jsonp.php?callback=?",
1037 success: function(data){
1038 ok( data.data, "JSON results returned (GET, url callback)" );
1041 error: function(data){
1042 ok( false, "Ajax error JSON (GET, url callback)" );
1048 url: "data/jsonp.php",
1051 success: function(data){
1052 ok( data.data, "JSON results returned (GET, data callback)" );
1055 error: function(data){
1056 ok( false, "Ajax error JSON (GET, data callback)" );
1062 url: "data/jsonp.php",
1065 success: function(data){
1066 ok( data.data, "JSON results returned (GET, data obj callback)" );
1069 error: function(data){
1070 ok( false, "Ajax error JSON (GET, data obj callback)" );
1076 url: "data/jsonp.php",
1078 jsonpCallback: "jsonpResults",
1079 success: function(data){
1080 ok( data.data, "JSON results returned (GET, custom callback name)" );
1083 error: function(data){
1084 ok( false, "Ajax error JSON (GET, custom callback name)" );
1091 url: "data/jsonp.php",
1093 success: function(data){
1094 ok( data.data, "JSON results returned (POST, no callback)" );
1097 error: function(data){
1098 ok( false, "Ajax error JSON (GET, data obj callback)" );
1105 url: "data/jsonp.php",
1108 success: function(data){
1109 ok( data.data, "JSON results returned (POST, data callback)" );
1112 error: function(data){
1113 ok( false, "Ajax error JSON (POST, data callback)" );
1120 url: "data/jsonp.php",
1123 success: function(data){
1124 ok( data.data, "JSON results returned (POST, data obj callback)" );
1127 error: function(data){
1128 ok( false, "Ajax error JSON (POST, data obj callback)" );
1135 url: "data/jsonp.php",
1137 beforeSend: function(){
1138 strictEqual( this.cache, false, "cache must be false on JSON request" );
1145 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1149 window.jsonpResults = function(data) {
1150 ok( data.data, "JSON results returned (GET, custom callback function)" );
1151 window.jsonpResults = undefined;
1156 url: "data/jsonp.php",
1158 jsonpCallback: "jsonpResults"
1162 test("jQuery.ajax() - JSONP, Remote", function() {
1166 function plus(){ if ( ++count == 4 ) start(); }
1168 var base = window.location.href.replace(/[^\/]*$/, "");
1173 url: base + "data/jsonp.php",
1175 success: function(data){
1176 ok( data.data, "JSON results returned (GET, no callback)" );
1179 error: function(data){
1180 ok( false, "Ajax error JSON (GET, no callback)" );
1186 url: base + "data/jsonp.php?callback=?",
1188 success: function(data){
1189 ok( data.data, "JSON results returned (GET, url callback)" );
1192 error: function(data){
1193 ok( false, "Ajax error JSON (GET, url callback)" );
1199 url: base + "data/jsonp.php",
1202 success: function(data){
1203 ok( data.data, "JSON results returned (GET, data callback)" );
1206 error: function(data){
1207 ok( false, "Ajax error JSON (GET, data callback)" );
1213 url: base + "data/jsonp.php",
1216 success: function(data){
1217 ok( data.data, "JSON results returned (GET, data obj callback)" );
1220 error: function(data){
1221 ok( false, "Ajax error JSON (GET, data obj callback)" );
1227 test("jQuery.ajax() - script, Remote", function() {
1230 var base = window.location.href.replace(/[^\/]*$/, "");
1235 url: base + "data/test.js",
1237 success: function(data){
1238 ok( foobar, "Script results returned (GET, no callback)" );
1244 test("jQuery.ajax() - script, Remote with POST", function() {
1247 var base = window.location.href.replace(/[^\/]*$/, "");
1252 url: base + "data/test.js",
1255 success: function(data, status){
1256 ok( foobar, "Script results returned (POST, no callback)" );
1257 equals( status, "success", "Script results returned (POST, no callback)" );
1260 error: function(xhr) {
1261 ok( false, "ajax error, status code: " + xhr.status );
1267 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1270 var base = window.location.href.replace(/[^\/]*$/, "");
1271 base = base.replace(/^.*?\/\//, "//");
1276 url: base + "data/test.js",
1278 success: function(data){
1279 ok( foobar, "Script results returned (GET, no callback)" );
1285 test("jQuery.ajax() - malformed JSON", function() {
1291 url: "data/badjson.js",
1293 success: function(){
1294 ok( false, "Success." );
1297 error: function(xhr, msg, detailedMsg) {
1298 equals( "parsererror", msg, "A parse error occurred." );
1299 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1305 test("jQuery.ajax() - script by content-type", function() {
1311 url: "data/script.php",
1312 data: { header: "script" },
1313 success: function() {
1319 test("jQuery.ajax() - json by content-type", function() {
1325 url: "data/json.php",
1326 data: { header: "json", json: "array" },
1327 success: function( json ) {
1328 ok( json.length >= 2, "Check length");
1329 equals( json[0].name, 'John', 'Check JSON: first, name' );
1330 equals( json[0].age, 21, 'Check JSON: first, age' );
1331 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1332 equals( json[1].age, 25, 'Check JSON: second, age' );
1338 test("jQuery.ajax() - json by content-type disabled with options", function() {
1344 url: url("data/json.php"),
1345 data: { header: "json", json: "array" },
1349 success: function( text ) {
1350 equals( typeof text , "string" , "json wasn't auto-determined" );
1351 var json = jQuery.parseJSON( text );
1352 ok( json.length >= 2, "Check length");
1353 equals( json[0].name, 'John', 'Check JSON: first, name' );
1354 equals( json[0].age, 21, 'Check JSON: first, age' );
1355 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1356 equals( json[1].age, 25, 'Check JSON: second, age' );
1362 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1365 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1366 ok( json.length >= 2, "Check length");
1367 equals( json[0].name, 'John', 'Check JSON: first, name' );
1368 equals( json[0].age, 21, 'Check JSON: first, age' );
1369 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1370 equals( json[1].age, 25, 'Check JSON: second, age' );
1375 test("jQuery.getJSON(String, Function) - JSON object", function() {
1378 jQuery.getJSON(url("data/json.php"), function(json) {
1379 if (json && json.data) {
1380 equals( json.data.lang, 'en', 'Check JSON: lang' );
1381 equals( json.data.length, 25, 'Check JSON: length' );
1387 test("jQuery.getJSON - Using Native JSON", function() {
1390 var old = window.JSON;
1392 parse: function(str){
1393 ok( true, "Verifying that parse method was run" );
1399 jQuery.getJSON(url("data/json.php"), function(json) {
1401 equals( json, true, "Verifying return value" );
1406 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1409 var base = window.location.href.replace(/[^\/]*$/, "");
1412 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1413 equals( json.data.lang, 'en', 'Check JSON: lang' );
1414 equals( json.data.length, 25, 'Check JSON: length' );
1419 test("jQuery.post - data", function() {
1423 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1424 jQuery('math', xml).each(function() {
1425 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1426 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1432 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1437 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1438 jQuery('math', xml).each(function() {
1439 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1440 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1442 if ( ++done === 2 ) start();
1445 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1446 jQuery('math', xml).each(function() {
1447 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1448 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1450 if ( ++done === 2 ) start();
1454 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1459 jQuery.ajaxSetup({timeout: 1000});
1461 var pass = function() {
1463 if ( passed == 2 ) {
1464 ok( true, 'Check local and global callbacks after timeout' );
1465 jQuery('#main').unbind("ajaxError");
1470 var fail = function(a,b,c) {
1471 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1475 jQuery('#main').ajaxError(pass);
1479 url: url("data/name.php?wait=5"),
1485 jQuery.ajaxSetup({timeout: 0});
1488 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1490 jQuery.ajaxSetup({timeout: 50});
1495 url: url("data/name.php?wait=1"),
1497 ok( false, 'Check for local timeout failed' );
1500 success: function() {
1501 ok( true, 'Check for local timeout' );
1507 jQuery.ajaxSetup({timeout: 0});
1510 test("jQuery.ajax - simple get", function() {
1515 url: url("data/name.php?name=foo"),
1516 success: function(msg){
1517 equals( msg, 'bar', 'Check for GET' );
1523 test("jQuery.ajax - simple post", function() {
1528 url: url("data/name.php"),
1530 success: function(msg){
1531 equals( msg, 'pan', 'Check for POST' );
1537 test("ajaxSetup()", function() {
1541 url: url("data/name.php?name=foo"),
1542 success: function(msg){
1543 equals( msg, 'bar', 'Check for GET' );
1551 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1554 url: "data/name.php?wait=1",
1556 error: function(request, status) {
1557 ok( status != null, "status shouldn't be null in error handler" );
1558 equals( "timeout", status );
1565 test("data option: evaluate function values (#2806)", function() {
1568 url: "data/echoQuery.php",
1574 success: function(result) {
1575 equals( result, "key=value" );
1581 test("data option: empty bodies for non-GET requests", function() {
1584 url: "data/echoData.php",
1587 success: function(result) {
1588 equals( result, "" );
1594 test("jQuery.ajax - If-Modified-Since support", function() {
1599 var url = "data/if_modified_since.php?ts=" + new Date();
1604 success: function(data, status) {
1605 equals(status, "success");
1610 success: function(data, status) {
1611 if ( data === "FAIL" ) {
1612 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1613 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1615 equals(status, "notmodified");
1616 ok(data == null, "response body should be empty");
1621 // Do this because opera simply refuses to implement 304 handling :(
1622 // A feature-driven way of detecting this would be appreciated
1623 // See: http://gist.github.com/599419
1624 ok(jQuery.browser.opera, "error");
1625 ok(jQuery.browser.opera, "error");
1631 equals(false, "error");
1632 // Do this because opera simply refuses to implement 304 handling :(
1633 // A feature-driven way of detecting this would be appreciated
1634 // See: http://gist.github.com/599419
1635 ok(jQuery.browser.opera, "error");
1641 test("jQuery.ajax - Etag support", function() {
1646 var url = "data/etag.php?ts=" + new Date();
1651 success: function(data, status) {
1652 equals(status, "success");
1657 success: function(data, status) {
1658 if ( data === "FAIL" ) {
1659 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1660 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1662 equals(status, "notmodified");
1663 ok(data == null, "response body should be empty");
1668 // Do this because opera simply refuses to implement 304 handling :(
1669 // A feature-driven way of detecting this would be appreciated
1670 // See: http://gist.github.com/599419
1671 ok(jQuery.browser.opera, "error");
1672 ok(jQuery.browser.opera, "error");
1678 // Do this because opera simply refuses to implement 304 handling :(
1679 // A feature-driven way of detecting this would be appreciated
1680 // See: http://gist.github.com/599419
1681 ok(jQuery.browser.opera, "error");
1687 test("jQuery ajax - failing cross-domain", function() {
1696 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1697 success: function(){ ok( false , "success" ); },
1698 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1699 complete: function() { if ( ! --i ) start(); }
1701 ok( true , "no transport" );
1702 if ( ! --i ) start();
1706 url: 'http://www.google.com',
1707 success: function(){ ok( false , "success" ); },
1708 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1709 complete: function() { if ( ! --i ) start(); }
1711 ok( true , "no transport" );
1712 if ( ! --i ) start();
1717 test("jQuery ajax - atom+xml", function() {
1722 url: url( 'data/atom+xml.php' ),
1723 success: function(){ ok( true , "success" ); },
1724 error: function(){ ok( false , "error" ); },
1725 complete: function() { start(); }
1730 test("jQuery.ajax - active counter", function() {
1731 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1734 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1735 var success = false;
1737 var xhr = jQuery.ajax({ url: window.location });
1742 ok( success, "document.location did not generate exception" );