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() - hash", function() {
283 url: "data/name.html#foo",
284 beforeSend: function( xhr, settings ) {
285 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
291 url: "data/name.html?abc#foo",
292 beforeSend: function( xhr, settings ) {
293 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
299 url: "data/name.html?abc#foo",
300 data: { "test": 123 },
301 beforeSend: function( xhr, settings ) {
302 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
308 test(".ajax() - 304", function() {
313 url: url("data/notmodified.php"),
314 success: function(){ ok(true, "304 ok"); },
315 // Do this because opera simply refuses to implement 304 handling :(
316 // A feature-driven way of detecting this would be appreciated
317 // See: http://gist.github.com/599419
318 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
319 complete: function(xhr){ start(); }
323 test(".load()) - 404 error callbacks", function() {
327 jQuery('#foo').ajaxStart(function(){
328 ok( true, "ajaxStart" );
329 }).ajaxStop(function(){
330 ok( true, "ajaxStop" );
332 }).ajaxSend(function(){
333 ok( true, "ajaxSend" );
334 }).ajaxComplete(function(){
335 ok( true, "ajaxComplete" );
336 }).ajaxError(function(){
337 ok( true, "ajaxError" );
338 }).ajaxSuccess(function(){
339 ok( false, "ajaxSuccess" );
342 jQuery("<div/>").load("data/404.html", function(){
343 ok(true, "complete");
347 test("jQuery.ajax() - abort", function() {
351 jQuery('#foo').ajaxStart(function(){
352 ok( true, "ajaxStart" );
353 }).ajaxStop(function(){
354 ok( true, "ajaxStop" );
356 }).ajaxSend(function(){
357 ok( true, "ajaxSend" );
358 }).ajaxComplete(function(){
359 ok( true, "ajaxComplete" );
362 var xhr = jQuery.ajax({
363 url: url("data/name.php?wait=5"),
364 beforeSend: function(){ ok(true, "beforeSend"); },
365 complete: function(){ ok(true, "complete"); }
368 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
371 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
374 test("Ajax events with context", function() {
378 var context = document.createElement("div");
381 equals( this, context, e.type );
384 function callback(msg){
386 equals( this, context, "context is preserved on callback " + msg );
390 function nocallback(msg){
392 equals( typeof this.url, "string", "context is settings on callback " + msg );
396 jQuery('#foo').add(context)
403 url: url("data/name.html"),
404 beforeSend: callback("beforeSend"),
405 success: callback("success"),
406 error: callback("error"),
408 callback("complete").call(this);
411 url: url("data/404.html"),
413 beforeSend: callback("beforeSend"),
414 error: callback("error"),
415 complete: function(){
416 callback("complete").call(this);
418 jQuery('#foo').add(context).unbind();
421 url: url("data/404.html"),
422 beforeSend: nocallback("beforeSend"),
423 error: nocallback("error"),
424 complete: function(){
425 nocallback("complete").call(this);
436 test("jQuery.ajax context modification", function() {
444 url: url("data/name.html"),
446 beforeSend: function(){
449 complete: function() {
454 equals( obj.test, "foo", "Make sure the original object is maintained." );
457 test("jQuery.ajax() - disabled globals", function() {
461 jQuery('#foo').ajaxStart(function(){
462 ok( false, "ajaxStart" );
463 }).ajaxStop(function(){
464 ok( false, "ajaxStop" );
465 }).ajaxSend(function(){
466 ok( false, "ajaxSend" );
467 }).ajaxComplete(function(){
468 ok( false, "ajaxComplete" );
469 }).ajaxError(function(){
470 ok( false, "ajaxError" );
471 }).ajaxSuccess(function(){
472 ok( false, "ajaxSuccess" );
477 url: url("data/name.html"),
478 beforeSend: function(){ ok(true, "beforeSend"); },
479 success: function(){ ok(true, "success"); },
480 error: function(){ ok(false, "error"); },
481 complete: function(){
482 ok(true, "complete");
483 setTimeout(function(){ start(); }, 13);
488 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
492 url: url("data/with_fries.xml"),
494 success: function(resp) {
495 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
496 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
497 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
503 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements (over JSONP)", function() {
507 url: url("data/with_fries_over_jsonp.php"),
508 dataType: "jsonp xml",
509 success: function(resp) {
510 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
511 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
512 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
515 error: function(_1,_2,error) {
522 test("jQuery.ajax - HEAD requests", function() {
527 url: url("data/name.html"),
529 success: function(data, status, xhr){
530 var h = xhr.getAllResponseHeaders();
531 ok( /Date/i.test(h), 'No Date in HEAD response' );
534 url: url("data/name.html"),
535 data: { whip_it: "good" },
537 success: function(data, status, xhr){
538 var h = xhr.getAllResponseHeaders();
539 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
548 test("jQuery.ajax - beforeSend", function() {
554 jQuery.ajaxSetup({ timeout: 0 });
557 url: url("data/name.html"),
558 beforeSend: function(xml) {
561 success: function(data) {
562 ok( check, "check beforeSend was executed" );
568 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
570 var request = jQuery.ajax({
571 url: url("data/name.html"),
572 beforeSend: function() {
573 ok( true, "beforeSend got called, canceling" );
576 success: function() {
577 ok( false, "request didn't get canceled" );
579 complete: function() {
580 ok( false, "request didn't get canceled" );
583 ok( false, "request didn't get canceled" );
586 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
589 test("jQuery.ajax - beforeSend, cancel request manually", function() {
591 var request = jQuery.ajax({
592 url: url("data/name.html"),
593 beforeSend: function(xhr) {
594 ok( true, "beforeSend got called, canceling" );
597 success: function() {
598 ok( false, "request didn't get canceled" );
600 complete: function() {
601 ok( false, "request didn't get canceled" );
604 ok( false, "request didn't get canceled" );
607 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
610 window.foobar = null;
611 window.testFoo = undefined;
613 test("jQuery.ajax - dataType html", function() {
617 var verifyEvaluation = function() {
618 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
619 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
626 url: url("data/test.html"),
627 success: function(data) {
628 jQuery("#ap").html(data);
629 ok( data.match(/^html text/), 'Check content for datatype html' );
630 setTimeout(verifyEvaluation, 600);
635 test("serialize()", function() {
638 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
639 jQuery("#search").after(
640 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
641 '<input type="number" id="html5number" name="number" value="43" />'
644 equals( jQuery('#form').serialize(),
645 "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",
646 'Check form serialization as query string');
648 equals( jQuery('#form :input').serialize(),
649 "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",
650 'Check input serialization as query string');
652 equals( jQuery('#testForm').serialize(),
653 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
654 'Check form serialization as query string');
656 equals( jQuery('#testForm :input').serialize(),
657 'T3=%3F%0D%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
658 'Check input serialization as query string');
660 equals( jQuery('#form, #testForm').serialize(),
661 "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=",
662 'Multiple form serialization as query string');
664 /* Temporarily disabled. Opera 10 has problems with form serialization.
665 equals( jQuery('#form, #testForm :input').serialize(),
666 "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=",
667 'Mixed form/input serialization as query string');
669 jQuery("#html5email, #html5number").remove();
672 test("jQuery.param()", function() {
675 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
677 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
678 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
680 params = {someName: [1, 2, 3], regularThing: "blah" };
681 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
683 params = {foo: ['a', 'b', 'c']};
684 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
686 params = {foo: ["baz", 42, "All your base are belong to us"] };
687 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
689 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
690 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" );
692 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?" };
693 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" );
695 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 ] };
696 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" );
698 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?" };
699 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" );
701 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." );
703 // Make sure empty arrays and objects are handled #6481
704 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
705 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
706 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
708 jQuery.ajaxSetup({ traditional: true });
710 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
711 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
713 params = {someName: [1, 2, 3], regularThing: "blah" };
714 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
716 params = {foo: ['a', 'b', 'c']};
717 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
719 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
720 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
722 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
723 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" );
725 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?" };
726 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" );
728 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 ] };
729 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)" );
731 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?" };
732 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" );
734 params = { param1: null };
735 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
738 test("synchronous request", function() {
740 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
743 test("synchronous request with callbacks", function() {
746 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
747 ok( /^{ "data"/.test( result ), "check returned text" );
750 test("pass-through request object", function() {
754 var target = "data/name.html";
755 var successCount = 0;
758 var success = function() {
761 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
763 errorEx += ": " + xml.status;
765 jQuery("#foo").one('ajaxStop', function () {
766 equals(successCount, 5, "Check all ajax calls successful");
767 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
768 jQuery("#foo").unbind('ajaxError');
773 ok( jQuery.get(url(target), success), "get" );
774 ok( jQuery.post(url(target), success), "post" );
775 ok( jQuery.getScript(url("data/test.js"), success), "script" );
776 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
777 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
780 test("ajax cache", function () {
787 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
788 var re = /_=(.*?)(&|$)/g;
790 for (var i = 0; i < 6; i++) {
791 var ret = re.exec(s.url);
797 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
798 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
803 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
804 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
805 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
806 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
807 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
808 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
813 * The assertions expect that the passed-in object will be modified,
814 * which shouldn't be the case. Fixes #5439.
815 test("global ajaxSettings", function() {
818 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
819 var orig = { url: "data/with_fries.xml" };
822 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
824 t = jQuery.extend({}, orig);
827 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
829 t = jQuery.extend({}, orig);
830 t.data = { zoo: 'a', ping: 'b' };
832 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' }" );
834 jQuery.ajaxSettings = tmp;
838 test("load(String)", function() {
840 stop(); // check if load can be called with only url
841 jQuery('#first').load("data/name.html", start);
844 test("load('url selector')", function() {
846 stop(); // check if load can be called with only url
847 jQuery('#first').load("data/test3.html div.user", function(){
848 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
853 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
856 jQuery.ajaxSetup({ dataType: "json" });
857 jQuery("#first").ajaxComplete(function (e, xml, s) {
858 equals( s.dataType, "html", "Verify the load() dataType was html" );
859 jQuery("#first").unbind("ajaxComplete");
860 jQuery.ajaxSetup({ dataType: "" });
863 jQuery('#first').load("data/test3.html");
866 test("load(String, Function) - simple: inject text into DOM", function() {
869 jQuery('#first').load(url("data/name.html"), function() {
870 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
875 test("load(String, Function) - check scripts", function() {
879 var verifyEvaluation = function() {
880 equals( foobar, "bar", 'Check if script src was evaluated after load' );
881 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
885 jQuery('#first').load(url('data/test.html'), function() {
886 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
887 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
888 equals( testFoo, "foo", 'Check if script was evaluated after load' );
889 setTimeout(verifyEvaluation, 600);
893 test("load(String, Function) - check file with only a script tag", function() {
897 jQuery('#first').load(url('data/test2.html'), function() {
898 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
899 equals( testFoo, "foo", 'Check if script was evaluated after load' );
905 test("load(String, Object, Function)", function() {
909 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
910 var $post = jQuery(this).find('#post');
911 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
912 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
917 test("load(String, String, Function)", function() {
921 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
922 var $get = jQuery(this).find('#get');
923 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
924 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
929 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
932 jQuery.get(url('data/dashboard.xml'), function(xml) {
934 jQuery('tab', xml).each(function() {
935 content.push(jQuery(this).text());
937 equals( content[0], 'blabla', 'Check first tab');
938 equals( content[1], 'blublu', 'Check second tab');
943 test("jQuery.getScript(String, Function) - with callback", function() {
946 jQuery.getScript(url("data/test.js"), function() {
947 equals( foobar, "bar", 'Check if script was evaluated' );
948 setTimeout(start, 100);
952 test("jQuery.getScript(String, Function) - no callback", function() {
955 jQuery.getScript(url("data/test.js"), function(){
960 test("jQuery.ajax() - JSONP, Local", function() {
964 function plus(){ if ( ++count == 9 ) start(); }
969 url: "data/jsonp.php",
971 success: function(data){
972 ok( data.data, "JSON results returned (GET, no callback)" );
975 error: function(data){
976 ok( false, "Ajax error JSON (GET, no callback)" );
982 url: "data/jsonp.php?callback=?",
984 success: function(data){
985 ok( data.data, "JSON results returned (GET, url callback)" );
988 error: function(data){
989 ok( false, "Ajax error JSON (GET, url callback)" );
995 url: "data/jsonp.php",
998 success: function(data){
999 ok( data.data, "JSON results returned (GET, data callback)" );
1002 error: function(data){
1003 ok( false, "Ajax error JSON (GET, data callback)" );
1009 url: "data/jsonp.php",
1012 success: function(data){
1013 ok( data.data, "JSON results returned (GET, data obj callback)" );
1016 error: function(data){
1017 ok( false, "Ajax error JSON (GET, data obj callback)" );
1023 url: "data/jsonp.php",
1025 jsonpCallback: "jsonpResults",
1026 success: function(data){
1027 ok( data.data, "JSON results returned (GET, custom callback name)" );
1030 error: function(data){
1031 ok( false, "Ajax error JSON (GET, custom callback name)" );
1038 url: "data/jsonp.php",
1040 success: function(data){
1041 ok( data.data, "JSON results returned (POST, no callback)" );
1044 error: function(data){
1045 ok( false, "Ajax error JSON (GET, data obj callback)" );
1052 url: "data/jsonp.php",
1055 success: function(data){
1056 ok( data.data, "JSON results returned (POST, data callback)" );
1059 error: function(data){
1060 ok( false, "Ajax error JSON (POST, data callback)" );
1067 url: "data/jsonp.php",
1070 success: function(data){
1071 ok( data.data, "JSON results returned (POST, data obj callback)" );
1074 error: function(data){
1075 ok( false, "Ajax error JSON (POST, data obj callback)" );
1082 url: "data/jsonp.php",
1084 beforeSend: function(){
1085 strictEqual( this.cache, false, "cache must be false on JSON request" );
1092 test("jQuery.ajax() - JSONP - Custom JSONP Callback", function() {
1096 window.jsonpResults = function(data) {
1097 ok( data.data, "JSON results returned (GET, custom callback function)" );
1098 window.jsonpResults = undefined;
1103 url: "data/jsonp.php",
1105 jsonpCallback: "jsonpResults"
1109 test("jQuery.ajax() - JSONP, Remote", function() {
1113 function plus(){ if ( ++count == 4 ) start(); }
1115 var base = window.location.href.replace(/[^\/]*$/, "");
1120 url: base + "data/jsonp.php",
1122 success: function(data){
1123 ok( data.data, "JSON results returned (GET, no callback)" );
1126 error: function(data){
1127 ok( false, "Ajax error JSON (GET, no callback)" );
1133 url: base + "data/jsonp.php?callback=?",
1135 success: function(data){
1136 ok( data.data, "JSON results returned (GET, url callback)" );
1139 error: function(data){
1140 ok( false, "Ajax error JSON (GET, url callback)" );
1146 url: base + "data/jsonp.php",
1149 success: function(data){
1150 ok( data.data, "JSON results returned (GET, data callback)" );
1153 error: function(data){
1154 ok( false, "Ajax error JSON (GET, data callback)" );
1160 url: base + "data/jsonp.php",
1163 success: function(data){
1164 ok( data.data, "JSON results returned (GET, data obj callback)" );
1167 error: function(data){
1168 ok( false, "Ajax error JSON (GET, data obj callback)" );
1174 test("jQuery.ajax() - script, Remote", function() {
1177 var base = window.location.href.replace(/[^\/]*$/, "");
1182 url: base + "data/test.js",
1184 success: function(data){
1185 ok( foobar, "Script results returned (GET, no callback)" );
1191 test("jQuery.ajax() - script, Remote with POST", function() {
1194 var base = window.location.href.replace(/[^\/]*$/, "");
1199 url: base + "data/test.js",
1202 success: function(data, status){
1203 ok( foobar, "Script results returned (POST, no callback)" );
1204 equals( status, "success", "Script results returned (POST, no callback)" );
1207 error: function(xhr) {
1208 ok( false, "ajax error, status code: " + xhr.status );
1214 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
1217 var base = window.location.href.replace(/[^\/]*$/, "");
1218 base = base.replace(/^.*?\/\//, "//");
1223 url: base + "data/test.js",
1225 success: function(data){
1226 ok( foobar, "Script results returned (GET, no callback)" );
1232 test("jQuery.ajax() - malformed JSON", function() {
1238 url: "data/badjson.js",
1240 success: function(){
1241 ok( false, "Success." );
1244 error: function(xhr, msg, detailedMsg) {
1245 equals( "parsererror", msg, "A parse error occurred." );
1246 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1252 test("jQuery.ajax() - script by content-type", function() {
1258 url: "data/script.php",
1259 data: { header: "script" },
1260 success: function() {
1266 test("jQuery.ajax() - json by content-type", function() {
1272 url: "data/json.php",
1273 data: { header: "json", json: "array" },
1274 success: function( json ) {
1275 ok( json.length >= 2, "Check length");
1276 equals( json[0].name, 'John', 'Check JSON: first, name' );
1277 equals( json[0].age, 21, 'Check JSON: first, age' );
1278 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1279 equals( json[1].age, 25, 'Check JSON: second, age' );
1285 test("jQuery.ajax() - json by content-type disabled with options", function() {
1291 url: url("data/json.php"),
1292 data: { header: "json", json: "array" },
1296 success: function( text ) {
1297 equals( typeof text , "string" , "json wasn't auto-determined" );
1298 var json = jQuery.parseJSON( text );
1299 ok( json.length >= 2, "Check length");
1300 equals( json[0].name, 'John', 'Check JSON: first, name' );
1301 equals( json[0].age, 21, 'Check JSON: first, age' );
1302 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1303 equals( json[1].age, 25, 'Check JSON: second, age' );
1309 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1312 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1313 ok( json.length >= 2, "Check length");
1314 equals( json[0].name, 'John', 'Check JSON: first, name' );
1315 equals( json[0].age, 21, 'Check JSON: first, age' );
1316 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1317 equals( json[1].age, 25, 'Check JSON: second, age' );
1322 test("jQuery.getJSON(String, Function) - JSON object", function() {
1325 jQuery.getJSON(url("data/json.php"), function(json) {
1326 if (json && json.data) {
1327 equals( json.data.lang, 'en', 'Check JSON: lang' );
1328 equals( json.data.length, 25, 'Check JSON: length' );
1334 test("jQuery.getJSON - Using Native JSON", function() {
1337 var old = window.JSON;
1339 parse: function(str){
1340 ok( true, "Verifying that parse method was run" );
1346 jQuery.getJSON(url("data/json.php"), function(json) {
1348 equals( json, true, "Verifying return value" );
1353 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1356 var base = window.location.href.replace(/[^\/]*$/, "");
1359 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1360 equals( json.data.lang, 'en', 'Check JSON: lang' );
1361 equals( json.data.length, 25, 'Check JSON: length' );
1366 test("jQuery.post - data", function() {
1370 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1371 jQuery('math', xml).each(function() {
1372 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1373 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1379 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1384 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1385 jQuery('math', xml).each(function() {
1386 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1387 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1389 if ( ++done === 2 ) start();
1392 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1393 jQuery('math', xml).each(function() {
1394 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1395 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1397 if ( ++done === 2 ) start();
1401 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1406 jQuery.ajaxSetup({timeout: 1000});
1408 var pass = function() {
1410 if ( passed == 2 ) {
1411 ok( true, 'Check local and global callbacks after timeout' );
1412 jQuery('#main').unbind("ajaxError");
1417 var fail = function(a,b,c) {
1418 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1422 jQuery('#main').ajaxError(pass);
1426 url: url("data/name.php?wait=5"),
1432 jQuery.ajaxSetup({timeout: 0});
1435 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1437 jQuery.ajaxSetup({timeout: 50});
1442 url: url("data/name.php?wait=1"),
1444 ok( false, 'Check for local timeout failed' );
1447 success: function() {
1448 ok( true, 'Check for local timeout' );
1454 jQuery.ajaxSetup({timeout: 0});
1457 test("jQuery.ajax - simple get", function() {
1462 url: url("data/name.php?name=foo"),
1463 success: function(msg){
1464 equals( msg, 'bar', 'Check for GET' );
1470 test("jQuery.ajax - simple post", function() {
1475 url: url("data/name.php"),
1477 success: function(msg){
1478 equals( msg, 'pan', 'Check for POST' );
1484 test("ajaxSetup()", function() {
1488 url: url("data/name.php?name=foo"),
1489 success: function(msg){
1490 equals( msg, 'bar', 'Check for GET' );
1498 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1501 url: "data/name.php?wait=1",
1503 error: function(request, status) {
1504 ok( status != null, "status shouldn't be null in error handler" );
1505 equals( "timeout", status );
1512 test("data option: evaluate function values (#2806)", function() {
1515 url: "data/echoQuery.php",
1521 success: function(result) {
1522 equals( result, "key=value" );
1528 test("data option: empty bodies for non-GET requests", function() {
1531 url: "data/echoData.php",
1534 success: function(result) {
1535 equals( result, "" );
1541 test("jQuery.ajax - If-Modified-Since support", function() {
1546 var url = "data/if_modified_since.php?ts=" + new Date();
1551 success: function(data, status) {
1552 equals(status, "success");
1557 success: function(data, status) {
1558 if ( data === "FAIL" ) {
1559 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1560 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1562 equals(status, "notmodified");
1563 ok(data == null, "response body should be empty")
1568 // Do this because opera simply refuses to implement 304 handling :(
1569 // A feature-driven way of detecting this would be appreciated
1570 // See: http://gist.github.com/599419
1571 ok(jQuery.browser.opera, "error");
1572 ok(jQuery.browser.opera, "error");
1578 equals(false, "error");
1579 // Do this because opera simply refuses to implement 304 handling :(
1580 // A feature-driven way of detecting this would be appreciated
1581 // See: http://gist.github.com/599419
1582 ok(jQuery.browser.opera, "error");
1588 test("jQuery.ajax - Etag support", function() {
1593 var url = "data/etag.php?ts=" + new Date();
1598 success: function(data, status) {
1599 equals(status, "success");
1604 success: function(data, status) {
1605 if ( data === "FAIL" ) {
1606 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1607 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1609 equals(status, "notmodified");
1610 ok(data == null, "response body should be empty")
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");
1619 ok(jQuery.browser.opera, "error");
1625 // Do this because opera simply refuses to implement 304 handling :(
1626 // A feature-driven way of detecting this would be appreciated
1627 // See: http://gist.github.com/599419
1628 ok(jQuery.browser.opera, "error");
1634 test("jQuery ajax - failing cross-domain", function() {
1643 url: 'http://somewebsitethatdoesnotexist-67864863574657654.com',
1644 success: function(){ ok( false , "success" ); },
1645 error: function(xhr,_,e){ ok( true , "file not found: " + xhr.status + " => " + e ); },
1646 complete: function() { if ( ! --i ) start(); }
1648 ok( true , "no transport" );
1649 if ( ! --i ) start();
1653 url: 'http://www.google.com',
1654 success: function(){ ok( false , "success" ); },
1655 error: function(xhr,_,e){ ok( true , "access denied: " + xhr.status + " => " + e ); },
1656 complete: function() { if ( ! --i ) start(); }
1658 ok( true , "no transport" );
1659 if ( ! --i ) start();
1664 test("jQuery ajax - atom+xml", function() {
1669 url: url( 'data/atom+xml.php' ),
1670 success: function(){ ok( true , "success" ); },
1671 error: function(){ ok( false , "error" ); },
1672 complete: function() { start(); }
1677 test("jQuery.ajax - active counter", function() {
1678 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1681 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1682 var success = false;
1684 var xhr = jQuery.ajax({ url: window.location });
1689 ok( success, "document.location did not generate exception" );