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(".ajax() - headers" , function() {
249 var requestHeaders = {
251 "SometHing-elsE": "other value",
252 OthEr: "something else"
257 for( i in requestHeaders ) {
261 jQuery.ajax(url("data/headers.php?keys="+list.join( "_" ) ), {
262 headers: requestHeaders,
263 success: function( data , _ , xhr ) {
265 for ( i in requestHeaders ) {
266 tmp.push( i , ": " , requestHeaders[ i ] , "\n" );
268 tmp = tmp.join( "" );
270 equals( data , tmp , "Headers were sent" );
271 equals( xhr.getResponseHeader( "Sample-Header" ) , "Hello World" , "Sample header received" );
274 error: function(){ ok(false, "error"); }
279 test(".ajax() - contentType" , function() {
293 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
295 success: function( data ) {
296 strictEqual( data , "content-type: test\n" , "Test content-type is sent when options.contentType is set" );
298 complete: function() {
303 jQuery.ajax(url("data/headers.php?keys=content-type" ), {
305 success: function( data ) {
306 strictEqual( data , "content-type: \n" , "Test content-type is not sent when options.contentType===false" );
308 complete: function() {
315 test(".ajax() - hash", function() {
319 url: "data/name.html#foo",
320 beforeSend: function( xhr, settings ) {
321 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
327 url: "data/name.html?abc#foo",
328 beforeSend: function( xhr, settings ) {
329 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
335 url: "data/name.html?abc#foo",
336 data: { "test": 123 },
337 beforeSend: function( xhr, settings ) {
338 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
344 test(".ajax() - 304", function() {
349 url: url("data/notmodified.php"),
350 success: function(){ ok(true, "304 ok"); },
351 // Do this because opera simply refuses to implement 304 handling :(
352 // A feature-driven way of detecting this would be appreciated
353 // See: http://gist.github.com/599419
354 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
355 complete: function(xhr){ start(); }
359 test(".load()) - 404 error callbacks", function() {
363 jQuery('#foo').ajaxStart(function(){
364 ok( true, "ajaxStart" );
365 }).ajaxStop(function(){
366 ok( true, "ajaxStop" );
368 }).ajaxSend(function(){
369 ok( true, "ajaxSend" );
370 }).ajaxComplete(function(){
371 ok( true, "ajaxComplete" );
372 }).ajaxError(function(){
373 ok( true, "ajaxError" );
374 }).ajaxSuccess(function(){
375 ok( false, "ajaxSuccess" );
378 jQuery("<div/>").load("data/404.html", function(){
379 ok(true, "complete");
383 test("jQuery.ajax() - abort", function() {
387 jQuery('#foo').ajaxStart(function(){
388 ok( true, "ajaxStart" );
389 }).ajaxStop(function(){
390 ok( true, "ajaxStop" );
392 }).ajaxSend(function(){
393 ok( true, "ajaxSend" );
394 }).ajaxComplete(function(){
395 ok( true, "ajaxComplete" );
398 var xhr = jQuery.ajax({
399 url: url("data/name.php?wait=5"),
400 beforeSend: function(){ ok(true, "beforeSend"); },
401 complete: function(){ ok(true, "complete"); }
404 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
407 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
410 test("Ajax events with context", function() {
414 var context = document.createElement("div");
417 equals( this, context, e.type );
420 function callback(msg){
422 equals( this, context, "context is preserved on callback " + msg );
426 function nocallback(msg){
428 equals( typeof this.url, "string", "context is settings on callback " + msg );
432 jQuery('#foo').add(context)
439 url: url("data/name.html"),
440 beforeSend: callback("beforeSend"),
441 success: callback("success"),
442 error: callback("error"),
444 callback("complete").call(this);
447 url: url("data/404.html"),
449 beforeSend: callback("beforeSend"),
450 error: callback("error"),
451 complete: function(){
452 callback("complete").call(this);
454 jQuery('#foo').add(context).unbind();
457 url: url("data/404.html"),
458 beforeSend: nocallback("beforeSend"),
459 error: nocallback("error"),
460 complete: function(){
461 nocallback("complete").call(this);
472 test("jQuery.ajax context modification", function() {
480 url: url("data/name.html"),
482 beforeSend: function(){
485 complete: function() {
490 equals( obj.test, "foo", "Make sure the original object is maintained." );
493 test("jQuery.ajax() - disabled globals", function() {
497 jQuery('#foo').ajaxStart(function(){
498 ok( false, "ajaxStart" );
499 }).ajaxStop(function(){
500 ok( false, "ajaxStop" );
501 }).ajaxSend(function(){
502 ok( false, "ajaxSend" );
503 }).ajaxComplete(function(){
504 ok( false, "ajaxComplete" );
505 }).ajaxError(function(){
506 ok( false, "ajaxError" );
507 }).ajaxSuccess(function(){
508 ok( false, "ajaxSuccess" );
513 url: url("data/name.html"),
514 beforeSend: function(){ ok(true, "beforeSend"); },
515 success: function(){ ok(true, "success"); },
516 error: function(){ ok(false, "error"); },
517 complete: function(){
518 ok(true, "complete");
519 setTimeout(function(){ start(); }, 13);
524 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
528 url: url("data/with_fries.xml"),
530 success: function(resp) {
531 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
532 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
533 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
539 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
543 url: url("data/with_fries_over_jsonp.php"),
544 dataType: "jsonp xml",
545 success: function(resp) {
546 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
547 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
548 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
551 error: function(_1,_2,error) {
558 test("jQuery.ajax - HEAD requests", function() {
563 url: url("data/name.html"),
565 success: function(data, status, xhr){
566 var h = xhr.getAllResponseHeaders();
567 ok( /Date/i.test(h), 'No Date in HEAD response' );
570 url: url("data/name.html"),
571 data: { whip_it: "good" },
573 success: function(data, status, xhr){
574 var h = xhr.getAllResponseHeaders();
575 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
584 test("jQuery.ajax - beforeSend", function() {
590 jQuery.ajaxSetup({ timeout: 0 });
593 url: url("data/name.html"),
594 beforeSend: function(xml) {
597 success: function(data) {
598 ok( check, "check beforeSend was executed" );
604 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
606 var request = jQuery.ajax({
607 url: url("data/name.html"),
608 beforeSend: function() {
609 ok( true, "beforeSend got called, canceling" );
612 success: function() {
613 ok( false, "request didn't get canceled" );
615 complete: function() {
616 ok( false, "request didn't get canceled" );
619 ok( false, "request didn't get canceled" );
622 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
625 test("jQuery.ajax - beforeSend, cancel request manually", function() {
627 var request = jQuery.ajax({
628 url: url("data/name.html"),
629 beforeSend: function(xhr) {
630 ok( true, "beforeSend got called, canceling" );
633 success: function() {
634 ok( false, "request didn't get canceled" );
636 complete: function() {
637 ok( false, "request didn't get canceled" );
640 ok( false, "request didn't get canceled" );
643 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
646 window.foobar = null;
647 window.testFoo = undefined;
649 test("jQuery.ajax - dataType html", function() {
653 var verifyEvaluation = function() {
654 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
655 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
662 url: url("data/test.html"),
663 success: function(data) {
664 jQuery("#ap").html(data);
665 ok( data.match(/^html text/), 'Check content for datatype html' );
666 setTimeout(verifyEvaluation, 600);
671 test("serialize()", function() {
674 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
675 jQuery("#search").after(
676 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
677 '<input type="number" id="html5number" name="number" value="43" />'
680 equals( jQuery('#form').serialize(),
681 "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",
682 'Check form serialization as query string');
684 equals( jQuery('#form :input').serialize(),
685 "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",
686 'Check input serialization as query string');
688 equals( jQuery('#testForm').serialize(),
689 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
690 'Check form serialization as query string');
692 equals( jQuery('#testForm :input').serialize(),
693 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
694 'Check input serialization as query string');
696 equals( jQuery('#form, #testForm').serialize(),
697 "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=",
698 'Multiple form serialization as query string');
700 /* Temporarily disabled. Opera 10 has problems with form serialization.
701 equals( jQuery('#form, #testForm :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&T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
703 'Mixed form/input serialization as query string');
705 jQuery("#html5email, #html5number").remove();
708 test("jQuery.param()", function() {
711 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
713 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
714 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
716 params = {someName: [1, 2, 3], regularThing: "blah" };
717 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
719 params = {foo: ['a', 'b', 'c']};
720 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
722 params = {foo: ["baz", 42, "All your base are belong to us"] };
723 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
725 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
726 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" );
728 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?" };
729 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" );
731 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 ] };
732 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" );
734 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?" };
735 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" );
737 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." );
739 // Make sure empty arrays and objects are handled #6481
740 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
741 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
742 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
744 jQuery.ajaxSetup({ traditional: true });
746 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
747 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
749 params = {someName: [1, 2, 3], regularThing: "blah" };
750 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
752 params = {foo: ['a', 'b', 'c']};
753 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
755 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
756 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
758 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
759 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" );
761 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?" };
762 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" );
764 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 ] };
765 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)" );
767 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?" };
768 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" );
770 params = { param1: null };
771 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
774 test("synchronous request", function() {
776 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
779 test("synchronous request with callbacks", function() {
782 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
783 ok( /^{ "data"/.test( result ), "check returned text" );
786 test("pass-through request object", function() {
790 var target = "data/name.html";
791 var successCount = 0;
794 var success = function() {
797 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
799 errorEx += ": " + xml.status;
801 jQuery("#foo").one('ajaxStop', function () {
802 equals(successCount, 5, "Check all ajax calls successful");
803 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
804 jQuery("#foo").unbind('ajaxError');
809 ok( jQuery.get(url(target), success), "get" );
810 ok( jQuery.post(url(target), success), "post" );
811 ok( jQuery.getScript(url("data/test.js"), success), "script" );
812 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
813 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
816 test("ajax cache", function () {
823 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
824 var re = /_=(.*?)(&|$)/g;
826 for (var i = 0; i < 6; i++) {
827 var ret = re.exec(s.url);
833 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
834 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
839 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
840 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
841 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
842 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
843 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
844 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
849 * The assertions expect that the passed-in object will be modified,
850 * which shouldn't be the case. Fixes #5439.
851 test("global ajaxSettings", function() {
854 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
855 var orig = { url: "data/with_fries.xml" };
858 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
860 t = jQuery.extend({}, orig);
863 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
865 t = jQuery.extend({}, orig);
866 t.data = { zoo: 'a', ping: 'b' };
868 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' }" );
870 jQuery.ajaxSettings = tmp;
874 test("load(String)", function() {
876 stop(); // check if load can be called with only url
877 jQuery('#first').load("data/name.html", start);
880 test("load('url selector')", function() {
882 stop(); // check if load can be called with only url
883 jQuery('#first').load("data/test3.html div.user", function(){
884 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
889 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
892 jQuery.ajaxSetup({ dataType: "json" });
893 jQuery("#first").ajaxComplete(function (e, xml, s) {
894 equals( s.dataType, "html", "Verify the load() dataType was html" );
895 jQuery("#first").unbind("ajaxComplete");
896 jQuery.ajaxSetup({ dataType: "" });
899 jQuery('#first').load("data/test3.html");
902 test("load(String, Function) - simple: inject text into DOM", function() {
905 jQuery('#first').load(url("data/name.html"), function() {
906 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
911 test("load(String, Function) - check scripts", function() {
915 var verifyEvaluation = function() {
916 equals( foobar, "bar", 'Check if script src was evaluated after load' );
917 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
921 jQuery('#first').load(url('data/test.html'), function() {
922 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
923 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
924 equals( testFoo, "foo", 'Check if script was evaluated after load' );
925 setTimeout(verifyEvaluation, 600);
929 test("load(String, Function) - check file with only a script tag", function() {
933 jQuery('#first').load(url('data/test2.html'), function() {
934 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
935 equals( testFoo, "foo", 'Check if script was evaluated after load' );
941 test("load(String, Object, Function)", function() {
945 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
946 var $post = jQuery(this).find('#post');
947 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
948 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
953 test("load(String, String, Function)", function() {
957 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
958 var $get = jQuery(this).find('#get');
959 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
960 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
965 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
968 jQuery.get(url('data/dashboard.xml'), function(xml) {
970 jQuery('tab', xml).each(function() {
971 content.push(jQuery(this).text());
973 equals( content[0], 'blabla', 'Check first tab');
974 equals( content[1], 'blublu', 'Check second tab');
979 test("jQuery.getScript(String, Function) - with callback", function() {
982 jQuery.getScript(url("data/test.js"), function() {
983 equals( foobar, "bar", 'Check if script was evaluated' );
984 setTimeout(start, 100);
988 test("jQuery.getScript(String, Function) - no callback", function() {
991 jQuery.getScript(url("data/test.js"), function(){
996 test("jQuery.ajax() - JSONP, Local", function() {
1000 function plus(){ if ( ++count == 9 ) start(); }
1005 url: "data/jsonp.php",
1007 success: function(data){
1008 ok( data.data, "JSON results returned (GET, no callback)" );
1011 error: function(data){
1012 ok( false, "Ajax error JSON (GET, no callback)" );
1018 url: "data/jsonp.php?callback=?",
1020 success: function(data){
1021 ok( data.data, "JSON results returned (GET, url callback)" );
1024 error: function(data){
1025 ok( false, "Ajax error JSON (GET, url callback)" );
1031 url: "data/jsonp.php",
1034 success: function(data){
1035 ok( data.data, "JSON results returned (GET, data callback)" );
1038 error: function(data){
1039 ok( false, "Ajax error JSON (GET, data callback)" );
1045 url: "data/jsonp.php",
1048 success: function(data){
1049 ok( data.data, "JSON results returned (GET, data obj callback)" );
1052 error: function(data){
1053 ok( false, "Ajax error JSON (GET, data obj callback)" );
1059 url: "data/jsonp.php",
1061 jsonpCallback: "jsonpResults",
1062 success: function(data){
1063 ok( data.data, "JSON results returned (GET, custom callback name)" );
1066 error: function(data){
1067 ok( false, "Ajax error JSON (GET, custom callback name)" );
1074 url: "data/jsonp.php",
1076 success: function(data){
1077 ok( data.data, "JSON results returned (POST, no callback)" );
1080 error: function(data){
1081 ok( false, "Ajax error JSON (GET, data obj callback)" );
1088 url: "data/jsonp.php",
1091 success: function(data){
1092 ok( data.data, "JSON results returned (POST, data callback)" );
1095 error: function(data){
1096 ok( false, "Ajax error JSON (POST, data callback)" );
1103 url: "data/jsonp.php",
1106 success: function(data){
1107 ok( data.data, "JSON results returned (POST, data obj callback)" );
1110 error: function(data){
1111 ok( false, "Ajax error JSON (POST, data obj callback)" );
1118 url: "data/jsonp.php",
1120 beforeSend: function(){
1121 strictEqual( this.cache, false, "cache must be false on JSON request" );
1128 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1132 window.jsonpResults = function(data) {
1133 ok( data.data, "JSON results returned (GET, custom callback function)" );
1134 window.jsonpResults = undefined;
1139 url: "data/jsonp.php",
1141 jsonpCallback: "jsonpResults"
1145 test("jQuery.ajax() - JSONP, Remote", function() {
1149 function plus(){ if ( ++count == 4 ) start(); }
1151 var base = window.location.href.replace(/[^\/]*$/, "");
1156 url: base + "data/jsonp.php",
1158 success: function(data){
1159 ok( data.data, "JSON results returned (GET, no callback)" );
1162 error: function(data){
1163 ok( false, "Ajax error JSON (GET, no callback)" );
1169 url: base + "data/jsonp.php?callback=?",
1171 success: function(data){
1172 ok( data.data, "JSON results returned (GET, url callback)" );
1175 error: function(data){
1176 ok( false, "Ajax error JSON (GET, url callback)" );
1182 url: base + "data/jsonp.php",
1185 success: function(data){
1186 ok( data.data, "JSON results returned (GET, data callback)" );
1189 error: function(data){
1190 ok( false, "Ajax error JSON (GET, data callback)" );
1196 url: base + "data/jsonp.php",
1199 success: function(data){
1200 ok( data.data, "JSON results returned (GET, data obj callback)" );
1203 error: function(data){
1204 ok( false, "Ajax error JSON (GET, data obj callback)" );
1210 test("jQuery.ajax() - script, Remote", function() {
1213 var base = window.location.href.replace(/[^\/]*$/, "");
1218 url: base + "data/test.js",
1220 success: function(data){
1221 ok( foobar, "Script results returned (GET, no callback)" );
1227 test("jQuery.ajax() - script, Remote with POST", function() {
1230 var base = window.location.href.replace(/[^\/]*$/, "");
1235 url: base + "data/test.js",
1238 success: function(data, status){
1239 ok( foobar, "Script results returned (POST, no callback)" );
1240 equals( status, "success", "Script results returned (POST, no callback)" );
1243 error: function(xhr) {
1244 ok( false, "ajax error, status code: " + xhr.status );
1250 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1253 var base = window.location.href.replace(/[^\/]*$/, "");
1254 base = base.replace(/^.*?\/\//, "//");
1259 url: base + "data/test.js",
1261 success: function(data){
1262 ok( foobar, "Script results returned (GET, no callback)" );
1268 test("jQuery.ajax() - malformed JSON", function() {
1274 url: "data/badjson.js",
1276 success: function(){
1277 ok( false, "Success." );
1280 error: function(xhr, msg, detailedMsg) {
1281 equals( "parsererror", msg, "A parse error occurred." );
1282 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1288 test("jQuery.ajax() - script by content-type", function() {
1294 url: "data/script.php",
1295 data: { header: "script" },
1296 success: function() {
1302 test("jQuery.ajax() - json by content-type", function() {
1308 url: "data/json.php",
1309 data: { header: "json", json: "array" },
1310 success: function( json ) {
1311 ok( json.length >= 2, "Check length");
1312 equals( json[0].name, 'John', 'Check JSON: first, name' );
1313 equals( json[0].age, 21, 'Check JSON: first, age' );
1314 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1315 equals( json[1].age, 25, 'Check JSON: second, age' );
1321 test("jQuery.ajax() - json by content-type disabled with options", function() {
1327 url: url("data/json.php"),
1328 data: { header: "json", json: "array" },
1332 success: function( text ) {
1333 equals( typeof text , "string" , "json wasn't auto-determined" );
1334 var json = jQuery.parseJSON( text );
1335 ok( json.length >= 2, "Check length");
1336 equals( json[0].name, 'John', 'Check JSON: first, name' );
1337 equals( json[0].age, 21, 'Check JSON: first, age' );
1338 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1339 equals( json[1].age, 25, 'Check JSON: second, age' );
1345 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1348 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1349 ok( json.length >= 2, "Check length");
1350 equals( json[0].name, 'John', 'Check JSON: first, name' );
1351 equals( json[0].age, 21, 'Check JSON: first, age' );
1352 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1353 equals( json[1].age, 25, 'Check JSON: second, age' );
1358 test("jQuery.getJSON(String, Function) - JSON object", function() {
1361 jQuery.getJSON(url("data/json.php"), function(json) {
1362 if (json && json.data) {
1363 equals( json.data.lang, 'en', 'Check JSON: lang' );
1364 equals( json.data.length, 25, 'Check JSON: length' );
1370 test("jQuery.getJSON - Using Native JSON", function() {
1373 var old = window.JSON;
1375 parse: function(str){
1376 ok( true, "Verifying that parse method was run" );
1382 jQuery.getJSON(url("data/json.php"), function(json) {
1384 equals( json, true, "Verifying return value" );
1389 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1392 var base = window.location.href.replace(/[^\/]*$/, "");
1395 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1396 equals( json.data.lang, 'en', 'Check JSON: lang' );
1397 equals( json.data.length, 25, 'Check JSON: length' );
1402 test("jQuery.post - data", function() {
1406 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1407 jQuery('math', xml).each(function() {
1408 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1409 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1415 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1420 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1421 jQuery('math', xml).each(function() {
1422 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1423 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1425 if ( ++done === 2 ) start();
1428 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1429 jQuery('math', xml).each(function() {
1430 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1431 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1433 if ( ++done === 2 ) start();
1437 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1442 jQuery.ajaxSetup({timeout: 1000});
1444 var pass = function() {
1446 if ( passed == 2 ) {
1447 ok( true, 'Check local and global callbacks after timeout' );
1448 jQuery('#main').unbind("ajaxError");
1453 var fail = function(a,b,c) {
1454 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1458 jQuery('#main').ajaxError(pass);
1462 url: url("data/name.php?wait=5"),
1468 jQuery.ajaxSetup({timeout: 0});
1471 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1473 jQuery.ajaxSetup({timeout: 50});
1478 url: url("data/name.php?wait=1"),
1480 ok( false, 'Check for local timeout failed' );
1483 success: function() {
1484 ok( true, 'Check for local timeout' );
1490 jQuery.ajaxSetup({timeout: 0});
1493 test("jQuery.ajax - simple get", function() {
1498 url: url("data/name.php?name=foo"),
1499 success: function(msg){
1500 equals( msg, 'bar', 'Check for GET' );
1506 test("jQuery.ajax - simple post", function() {
1511 url: url("data/name.php"),
1513 success: function(msg){
1514 equals( msg, 'pan', 'Check for POST' );
1520 test("ajaxSetup()", function() {
1524 url: url("data/name.php?name=foo"),
1525 success: function(msg){
1526 equals( msg, 'bar', 'Check for GET' );
1534 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1537 url: "data/name.php?wait=1",
1539 error: function(request, status) {
1540 ok( status != null, "status shouldn't be null in error handler" );
1541 equals( "timeout", status );
1548 test("data option: evaluate function values (#2806)", function() {
1551 url: "data/echoQuery.php",
1557 success: function(result) {
1558 equals( result, "key=value" );
1564 test("data option: empty bodies for non-GET requests", function() {
1567 url: "data/echoData.php",
1570 success: function(result) {
1571 equals( result, "" );
1577 test("jQuery.ajax - If-Modified-Since support", function() {
1582 var url = "data/if_modified_since.php?ts=" + new Date();
1587 success: function(data, status) {
1588 equals(status, "success");
1593 success: function(data, status) {
1594 if ( data === "FAIL" ) {
1595 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1596 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1598 equals(status, "notmodified");
1599 ok(data == null, "response body should be empty");
1604 // Do this because opera simply refuses to implement 304 handling :(
1605 // A feature-driven way of detecting this would be appreciated
1606 // See: http://gist.github.com/599419
1607 ok(jQuery.browser.opera, "error");
1608 ok(jQuery.browser.opera, "error");
1614 equals(false, "error");
1615 // Do this because opera simply refuses to implement 304 handling :(
1616 // A feature-driven way of detecting this would be appreciated
1617 // See: http://gist.github.com/599419
1618 ok(jQuery.browser.opera, "error");
1624 test("jQuery.ajax - Etag support", function() {
1629 var url = "data/etag.php?ts=" + new Date();
1634 success: function(data, status) {
1635 equals(status, "success");
1640 success: function(data, status) {
1641 if ( data === "FAIL" ) {
1642 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1643 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1645 equals(status, "notmodified");
1646 ok(data == null, "response body should be empty");
1651 // Do this because opera simply refuses to implement 304 handling :(
1652 // A feature-driven way of detecting this would be appreciated
1653 // See: http://gist.github.com/599419
1654 ok(jQuery.browser.opera, "error");
1655 ok(jQuery.browser.opera, "error");
1661 // Do this because opera simply refuses to implement 304 handling :(
1662 // A feature-driven way of detecting this would be appreciated
1663 // See: http://gist.github.com/599419
1664 ok(jQuery.browser.opera, "error");
1670 test("jQuery ajax - failing cross-domain", function() {
1679 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1680 success: function(){ ok( false , "success" ); },
1681 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1682 complete: function() { if ( ! --i ) start(); }
1684 ok( true , "no transport" );
1685 if ( ! --i ) start();
1689 url: 'http://www.google.com',
1690 success: function(){ ok( false , "success" ); },
1691 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1692 complete: function() { if ( ! --i ) start(); }
1694 ok( true , "no transport" );
1695 if ( ! --i ) start();
1700 test("jQuery ajax - atom+xml", function() {
1705 url: url( 'data/atom+xml.php' ),
1706 success: function(){ ok( true , "success" ); },
1707 error: function(){ ok( false , "error" ); },
1708 complete: function() { start(); }
1713 test("jQuery.ajax - active counter", function() {
1714 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1717 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1718 var success = false;
1720 var xhr = jQuery.ajax({ url: window.location });
1725 ok( success, "document.location did not generate exception" );