3 var jsonpCallback = this.jsonpCallback = jQuery.ajaxSettings.jsonpCallback;
4 jQuery.ajaxSettings.jsonpCallback = function() {
5 var callback = jsonpCallback.apply( this, arguments );
6 Globals.register( callback );
10 teardown: function() {
11 jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
12 moduleTeardown.apply( this, arguments );
17 test("Unit Testing Environment", 2, function () {
18 ok( hasPHP, "Running in an environment with PHP support. The AJAX tests only run if the environment supports PHP!" );
19 ok( !isLocal, "Unit tests are not ran from file:// (especially in Chrome. If you must test from file:// with Chrome, run it with the --allow-file-access-from-files flag!)" );
22 if ( !jQuery.ajax || ( isLocal && !hasPHP ) ) {
26 function addGlobalEvents( expected ) {
28 expected = expected || "";
29 jQuery( document ).on( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess", function( e ) {
30 ok( expected.indexOf(e.type) !== -1, e.type );
35 //----------- jQuery.ajax()
37 testIframeWithCallback( "XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)", "ajax/unreleasedXHR.html", function() {
42 ajaxTest( "jQuery.ajax() - success callbacks", 8, {
43 setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
44 url: url("data/name.html"),
45 beforeSend: function() {
46 ok( true, "beforeSend" );
49 ok( true, "success" );
51 complete: function() {
52 ok( true, "complete");
56 ajaxTest( "jQuery.ajax() - success callbacks - (url, options) syntax", 8, {
57 setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
58 create: function( options ) {
59 return jQuery.ajax( url("data/name.html"), options );
61 beforeSend: function() {
62 ok( true, "beforeSend" );
65 ok( true, "success" );
67 complete: function() {
68 ok( true, "complete" );
72 ajaxTest( "jQuery.ajax() - success callbacks (late binding)", 8, {
73 setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
74 url: url("data/name.html"),
75 beforeSend: function() {
76 ok( true, "beforeSend" );
79 afterSend: function( request ) {
80 request.always(function() {
81 ok( true, "complete" );
83 ok( true, "success" );
90 ajaxTest( "jQuery.ajax() - success callbacks (oncomplete binding)", 8, {
91 setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
92 url: url("data/name.html"),
93 beforeSend: function() {
94 ok( true, "beforeSend" );
97 complete: function( xhr ) {
98 xhr.always(function() {
99 ok( true, "complete" );
101 ok( true, "success" );
103 ok( false, "error" );
108 ajaxTest( "jQuery.ajax() - error callbacks", 8, {
109 setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError"),
110 url: url("data/name.php?wait=5"),
111 beforeSend: function() {
112 ok( true, "beforeSend" );
114 afterSend: function( request ) {
120 complete: function() {
121 ok( true, "complete" );
125 ajaxTest( "jQuery.ajax() - textStatus and errorThrown values", 4, [
127 url: url("data/name.php?wait=5"),
128 error: function( _, textStatus, errorThrown ) {
129 strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
130 strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort" );
132 afterSend: function( request ) {
137 url: url("data/name.php?wait=5"),
138 error: function( _, textStatus, errorThrown ) {
139 strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
140 strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')" );
142 afterSend: function( request ) {
143 request.abort("mystatus");
148 ajaxTest( "jQuery.ajax() - responseText on error", 1, {
149 url: url("data/errorWithText.php"),
150 error: function( xhr ) {
151 strictEqual( xhr.responseText, "plain text message", "Test jqXHR.responseText is filled for HTTP errors" );
155 asyncTest( "jQuery.ajax() - retry with jQuery.ajax( this )", 2, function() {
159 url: url("data/errorWithText.php"),
165 ok ( true, "Test retrying with jQuery.ajax(this) works" );
167 url: url("data/errorWithText.php"),
171 beforeSend: function() {
172 if ( !previousUrl ) {
173 previousUrl = this.url;
175 strictEqual( this.url, previousUrl, "url parameters are not re-appended" );
189 ajaxTest( "jQuery.ajax() - headers", 5, {
191 jQuery( document ).ajaxSend(function( evt, xhr ) {
192 xhr.setRequestHeader( "ajax-send", "test" );
195 url: url("data/headers.php?keys=siMPle_SometHing-elsE_OthEr_Nullable_undefined_Empty_ajax-send"),
198 "SometHing-elsE": "other value",
199 "OthEr": "something else",
201 "undefined": undefined
204 // Not all browsers allow empty-string headers
205 // https://bugzilla.mozilla.org/show_bug.cgi?id=815299
208 success: function( data, _, xhr ) {
210 requestHeaders = jQuery.extend( this.headers, {
214 for ( i in requestHeaders ) {
215 tmp.push( i, ": ", requestHeaders[ i ] + "", "\n" );
219 strictEqual( data, tmp, "Headers were sent" );
220 strictEqual( xhr.getResponseHeader("Sample-Header"), "Hello World", "Sample header received" );
221 ok( data.indexOf( "undefined" ) < 0 , "Undefined header value was not sent" );
223 emptyHeader = xhr.getResponseHeader("Empty-Header");
224 if ( emptyHeader === null ) {
225 ok( true, "Firefox doesn't support empty headers" );
227 strictEqual( emptyHeader, "", "Empty header received" );
229 strictEqual( xhr.getResponseHeader("Sample-Header2"), "Hello World 2", "Second sample header received" );
233 ajaxTest( "jQuery.ajax() - Accept header", 1, {
234 url: url("data/headers.php?keys=accept"),
236 Accept: "very wrong accept value"
238 beforeSend: function( xhr ) {
239 xhr.setRequestHeader("Accept", "*/*");
241 success: function( data ) {
242 strictEqual( data, "accept: */*\n", "Test Accept header is set to last value provided" );
246 ajaxTest( "jQuery.ajax() - contentType", 2, [
248 url: url("data/headers.php?keys=content-type"),
250 success: function( data ) {
251 strictEqual( data, "content-type: test\n", "Test content-type is sent when options.contentType is set" );
255 url: url("data/headers.php?keys=content-type"),
257 success: function( data ) {
258 // Some server/interpreter combinations always supply a Content-Type to scripts
259 data = data || "content-type: \n";
260 strictEqual( data, "content-type: \n", "Test content-type is not set when options.contentType===false" );
265 ajaxTest( "jQuery.ajax() - protocol-less urls", 1, {
266 url: "//somedomain.com",
267 beforeSend: function( xhr, settings ) {
268 equal( settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added." );
274 ajaxTest( "jQuery.ajax() - hash", 3, [
276 url: "data/name.html#foo",
277 beforeSend: function( xhr, settings ) {
278 equal( settings.url, "data/name.html", "Make sure that the URL is trimmed." );
284 url: "data/name.html?abc#foo",
285 beforeSend: function( xhr, settings ) {
286 equal( settings.url, "data/name.html?abc", "Make sure that the URL is trimmed." );
292 url: "data/name.html?abc#foo",
296 beforeSend: function( xhr, settings ) {
297 equal( settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed." );
304 ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function() {
305 function request( url, title, crossDomainOrOptions ) {
306 return jQuery.extend( {
309 beforeSend: function( _, s ) {
310 ok( crossDomainOrOptions === false ? !s.crossDomain : s.crossDomain, title );
314 }, crossDomainOrOptions );
317 var loc = document.location,
318 samePort = loc.port || ( loc.protocol === "http:" ? 80 : 443 ),
319 otherPort = loc.port === 666 ? 667 : 666,
320 otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
324 loc.protocol + "//" + loc.hostname + ":" + samePort,
325 "Test matching ports are not detected as cross-domain",
329 otherProtocol + "//" + loc.host,
330 "Test different protocols are detected as cross-domain"
334 "Adobe AIR app:/ URL detected as cross-domain"
337 loc.protocol + "//example.invalid:" + ( loc.port || 80 ),
338 "Test different hostnames are detected as cross-domain"
341 loc.protocol + "//" + loc.hostname + ":" + otherPort,
342 "Test different ports are detected as cross-domain"
346 "Test about:blank is detected as cross-domain"
349 loc.protocol + "//" + loc.host,
350 "Test forced crossDomain is detected as cross-domain",
356 " http://otherdomain.com",
357 "Cross-domain url with leading space is detected as cross-domain"
362 ajaxTest( "jQuery.ajax() - abort", 9, {
363 setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxError ajaxComplete"),
364 url: url("data/name.php?wait=5"),
365 beforeSend: function() {
366 ok( true, "beforeSend" );
368 afterSend: function( xhr ) {
369 strictEqual( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
371 strictEqual( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
374 complete: function() {
375 ok( true, "complete" );
379 ajaxTest( "jQuery.ajax() - events with context", 12, function() {
381 var context = document.createElement("div");
383 function event( e ) {
384 equal( this, context, e.type );
387 function callback( msg ) {
389 equal( this, context, "context is preserved on callback " + msg );
395 jQuery( context ).appendTo("#foo")
397 .ajaxComplete( event )
399 .ajaxSuccess( event );
402 url: url("data/name.html"),
404 beforeSend: callback("beforeSend"),
405 success: callback("success"),
406 complete: callback("complete")
408 url: url("data/404.html"),
410 beforeSend: callback("beforeSend"),
411 error: callback("error"),
412 complete: callback("complete")
417 ajaxTest( "jQuery.ajax() - events without context", 3, function() {
418 function nocallback( msg ) {
420 equal( typeof this.url, "string", "context is settings on callback " + msg );
424 url: url("data/404.html"),
425 beforeSend: nocallback("beforeSend"),
426 error: nocallback("error"),
427 complete: nocallback("complete")
431 ajaxTest( "#15118 - jQuery.ajax() - function without jQuery.event", 1, function() {
434 url: url( "data/json.php" ),
436 holder = jQuery.event;
439 complete: function() {
440 ok( true, "Call can be made without jQuery.event" );
441 jQuery.event = holder;
447 ajaxTest( "#15160 - jQuery.ajax() - request manually aborted in ajaxSend", 3, {
449 jQuery( document ).on( "ajaxSend", function( e, jqXHR ) {
453 jQuery( document ).on( "ajaxError ajaxComplete", function( e, jqXHR ) {
454 equal( jqXHR.statusText, "abort", "jqXHR.statusText equals abort on global ajaxComplete and ajaxError events" );
457 url: url("data/name.html"),
459 complete: function() {
460 ok( true, "complete" );
464 ajaxTest( "jQuery.ajax() - context modification", 1, {
465 url: url("data/name.html"),
467 beforeSend: function() {
470 afterSend: function() {
471 strictEqual( this.context.test, "foo", "Make sure the original object is maintained." );
476 ajaxTest( "jQuery.ajax() - context modification through ajaxSetup", 3, function() {
483 strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
486 url: url("data/name.html"),
487 success: function() {
488 strictEqual( this, obj, "Make sure the original object is maintained." );
491 url: url("data/name.html"),
493 success: function() {
494 ok( this !== obj, "Make sure overriding context is possible." );
500 ajaxTest( "jQuery.ajax() - disabled globals", 3, {
501 setup: addGlobalEvents(""),
503 url: url("data/name.html"),
504 beforeSend: function() {
505 ok( true, "beforeSend" );
507 success: function() {
508 ok( true, "success" );
510 complete: function() {
511 ok( true, "complete" );
515 ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements", 3, {
516 url: url("data/with_fries.xml"),
518 success: function( resp ) {
519 equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
520 equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
521 equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
525 ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements (over JSONP)", 3, {
526 url: url("data/with_fries_over_jsonp.php"),
527 dataType: "jsonp xml",
528 success: function( resp ) {
529 equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
530 equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
531 equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
535 ajaxTest( "jQuery.ajax() - HEAD requests", 2, [
537 url: url("data/name.html"),
539 success: function( data, status, xhr ) {
540 ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response" );
544 url: url("data/name.html"),
549 success: function( data, status, xhr ) {
550 ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response with data" );
555 ajaxTest( "jQuery.ajax() - beforeSend", 1, {
556 url: url("data/name.html"),
557 beforeSend: function() {
560 success: function() {
561 ok( this.check, "check beforeSend was executed" );
565 ajaxTest( "jQuery.ajax() - beforeSend, cancel request manually", 2, {
568 url: url("data/name.html"),
569 beforeSend: function( xhr ) {
570 ok( true, "beforeSend got called, canceling" );
573 success: function() {
574 ok( false, "request didn't get canceled" );
576 complete: function() {
577 ok( false, "request didn't get canceled" );
580 ok( false, "request didn't get canceled" );
584 fail: function( _, reason ) {
585 strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
589 ajaxTest( "jQuery.ajax() - dataType html", 5, {
591 Globals.register("testFoo");
592 Globals.register("testBar");
595 url: url("data/test.html"),
596 success: function( data ) {
597 ok( data.match( /^html text/ ), "Check content for datatype html" );
598 jQuery("#ap").html( data );
599 strictEqual( window["testFoo"], "foo", "Check if script was evaluated for datatype html" );
600 strictEqual( window["testBar"], "bar", "Check if script src was evaluated for datatype html" );
604 ajaxTest( "jQuery.ajax() - synchronous request", 1, {
605 url: url("data/json_obj.js"),
609 afterSend: function( xhr ) {
610 ok( /^\{ "data"/.test( xhr.responseText ), "check returned text" );
614 ajaxTest( "jQuery.ajax() - synchronous request with callbacks", 2, {
615 url: url("data/json_obj.js"),
619 afterSend: function( xhr ) {
621 xhr.done(function( data ) {
622 ok( true, "success callback executed" );
625 ok( /^\{ "data"/.test( result ), "check returned text" );
629 asyncTest( "jQuery.ajax(), jQuery.get[Script|JSON](), jQuery.post(), pass-through request object", 8, function() {
630 var target = "data/name.html",
634 success = function() {
637 jQuery( document ).on( "ajaxError.passthru", function( e, xml ) {
639 errorEx += ": " + xml.status;
641 jQuery( document ).one( "ajaxStop", function() {
642 equal( successCount, 5, "Check all ajax calls successful" );
643 equal( errorCount, 0, "Check no ajax errors (status" + errorEx + ")" );
644 jQuery( document ).off("ajaxError.passthru");
647 Globals.register("testBar");
649 ok( jQuery.get( url(target), success ), "get" );
650 ok( jQuery.post( url(target), success ), "post" );
651 ok( jQuery.getScript( url("data/testbar.php"), success ), "script" );
652 ok( jQuery.getJSON( url("data/json_obj.js"), success ), "json" );
659 ajaxTest( "jQuery.ajax() - cache", 12, function() {
661 var re = /_=(.*?)(&|$)/g;
663 function request( url, title ) {
667 beforeSend: function() {
669 while(( tmp = re.exec( this.url ) )) {
670 strictEqual( parameter, undefined, title + ": only one 'no-cache' parameter" );
671 parameter = tmp[ 1 ];
672 notStrictEqual( parameter, "tobereplaced555", title + ": parameter (if it was there) was replaced" );
686 "data/text.php?pizza=true",
690 "data/text.php?_=tobereplaced555",
694 "data/text.php?pizza=true&_=tobereplaced555",
698 "data/text.php?_=tobereplaced555&tv=false",
702 "data/text.php?name=David&_=tobereplaced555&washere=true",
703 "2 parameters surrounding _="
708 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
710 ajaxTest( "jQuery.ajax() - JSONP - Query String (?n)" + label, 4, [
712 url: "data/jsonp.php?callback=?",
714 crossDomain: crossDomain,
715 success: function( data ) {
716 ok( data.data, "JSON results returned (GET, url callback)" );
720 url: "data/jsonp.php?callback=??",
722 crossDomain: crossDomain,
723 success: function( data ) {
724 ok( data.data, "JSON results returned (GET, url context-free callback)" );
728 url: "data/jsonp.php/??",
730 crossDomain: crossDomain,
731 success: function( data ) {
732 ok( data.data, "JSON results returned (GET, REST-like)" );
736 url: "data/jsonp.php/???json=1",
738 crossDomain: crossDomain,
739 success: function( data ) {
740 strictEqual( jQuery.type( data ), "array", "JSON results returned (GET, REST-like with param)" );
745 ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 9, {
747 Globals.register("functionToCleanUp");
748 Globals.register("XXX");
749 Globals.register("jsonpResults");
750 window["jsonpResults"] = function( data ) {
751 ok( data["data"], "JSON results returned (GET, custom callback function)" );
755 url: "data/jsonp.php",
757 crossDomain: crossDomain,
759 success: function( data ) {
760 ok( data["data"], "JSON results returned (GET, data obj callback)" );
763 url: "data/jsonp.php",
765 crossDomain: crossDomain,
766 jsonpCallback: "jsonpResults",
767 success: function( data ) {
768 ok( data.data, "JSON results returned (GET, custom callback name)" );
771 url: "data/jsonp.php",
773 crossDomain: crossDomain,
774 jsonpCallback: "functionToCleanUp",
775 success: function( data ) {
776 ok( data["data"], "JSON results returned (GET, custom callback name to be cleaned up)" );
777 strictEqual( window["functionToCleanUp"], true, "Callback was removed (GET, custom callback name to be cleaned up)" );
780 url: "data/jsonp.php",
782 crossDomain: crossDomain,
783 jsonpCallback: "functionToCleanUp",
784 beforeSend: function( jqXHR ) {
789 xhr.fail(function() {
790 ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
791 strictEqual( window["functionToCleanUp"], true, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
795 url: "data/jsonp.php?callback=XXX",
798 jsonpCallback: "XXX",
799 crossDomain: crossDomain,
800 beforeSend: function() {
801 ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ), "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
803 success: function( data ) {
804 ok( data["data"], "JSON results returned (GET, custom callback name with no url manipulation)" );
809 ajaxTest( "jQuery.ajax() - JSONP - Callback in data" + label, 2, [
811 url: "data/jsonp.php",
813 crossDomain: crossDomain,
815 success: function( data ) {
816 ok( data.data, "JSON results returned (GET, data callback)" );
820 url: "data/jsonp.php",
822 crossDomain: crossDomain,
824 success: function( data ) {
825 ok( data.data, "JSON results returned (GET, data context-free callback)" );
831 ajaxTest( "jQuery.ajax() - JSONP - POST" + label, 3, [
834 url: "data/jsonp.php",
836 crossDomain: crossDomain,
837 success: function( data ) {
838 ok( data["data"], "JSON results returned (POST, no callback)" );
843 url: "data/jsonp.php",
846 crossDomain: crossDomain,
847 success: function( data ) {
848 ok( data["data"], "JSON results returned (POST, data callback)" );
853 url: "data/jsonp.php",
856 crossDomain: crossDomain,
857 success: function( data ) {
858 ok( data["data"], "JSON results returned (POST, data obj callback)" );
863 ajaxTest( "jQuery.ajax() - JSONP" + label, 3, [
865 url: "data/jsonp.php",
867 crossDomain: crossDomain,
868 success: function( data ) {
869 ok( data.data, "JSON results returned (GET, no callback)" );
873 create: function( options ) {
874 var request = jQuery.ajax( options ),
875 promise = request.then(function( data ) {
876 ok( data.data, "first request: JSON results returned (GET, no callback)" );
877 request = jQuery.ajax( this ).done(function( data ) {
878 ok( data.data, "this re-used: JSON results returned (GET, no callback)" );
880 promise.abort = request.abort;
883 promise.abort = request.abort;
886 url: "data/jsonp.php",
888 crossDomain: crossDomain,
895 ajaxTest( "jQuery.ajax() - script, Remote", 2, {
897 Globals.register("testBar");
899 url: window.location.href.replace( /[^\/]*$/, "" ) + "data/testbar.php",
901 success: function() {
902 strictEqual( window["testBar"], "bar", "Script results returned (GET, no callback)" );
906 ajaxTest( "jQuery.ajax() - script, Remote with POST", 3, {
908 Globals.register("testBar");
910 url: window.location.href.replace( /[^\/]*$/, "" ) + "data/testbar.php",
913 success: function( data, status ) {
914 strictEqual( window["testBar"], "bar", "Script results returned (POST, no callback)" );
915 strictEqual( status, "success", "Script results returned (POST, no callback)" );
919 ajaxTest( "jQuery.ajax() - script, Remote with scheme-less URL", 2, {
921 Globals.register("testBar");
923 url: window.location.href.replace( /[^\/]*$/, "" ).replace( /^.*?\/\//, "//" ) + "data/testbar.php",
925 success: function() {
926 strictEqual( window["testBar"], "bar", "Script results returned (GET, no callback)" );
930 ajaxTest( "jQuery.ajax() - malformed JSON", 2, {
931 url: "data/badjson.js",
933 error: function( xhr, msg, detailedMsg ) {
934 strictEqual( msg, "parsererror", "A parse error occurred." );
935 ok( /(invalid|error|exception)/i.test( detailedMsg ), "Detailed parsererror message provided" );
939 ajaxTest( "jQuery.ajax() - script by content-type", 2, [
941 url: "data/script.php",
948 url: "data/script.php",
956 ajaxTest( "jQuery.ajax() - JSON by content-type", 5, {
957 url: "data/json.php",
962 success: function( json ) {
963 ok( json.length >= 2, "Check length" );
964 strictEqual( json[ 0 ]["name"], "John", "Check JSON: first, name" );
965 strictEqual( json[ 0 ]["age"], 21, "Check JSON: first, age" );
966 strictEqual( json[ 1 ]["name"], "Peter", "Check JSON: second, name" );
967 strictEqual( json[ 1 ]["age"], 25, "Check JSON: second, age" );
971 ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 6, {
972 url: url("data/json.php"),
980 success: function( text ) {
981 strictEqual( typeof text, "string", "json wasn't auto-determined" );
982 var json = jQuery.parseJSON( text );
983 ok( json.length >= 2, "Check length");
984 strictEqual( json[ 0 ]["name"], "John", "Check JSON: first, name" );
985 strictEqual( json[ 0 ]["age"], 21, "Check JSON: first, age" );
986 strictEqual( json[ 1 ]["name"], "Peter", "Check JSON: second, name" );
987 strictEqual( json[ 1 ]["age"], 25, "Check JSON: second, age" );
991 ajaxTest( "jQuery.ajax() - simple get", 1, {
993 url: url("data/name.php?name=foo"),
994 success: function( msg ) {
995 strictEqual( msg, "bar", "Check for GET" );
999 ajaxTest( "jQuery.ajax() - simple post", 1, {
1001 url: url("data/name.php"),
1003 success: function( msg ) {
1004 strictEqual( msg, "pan", "Check for POST" );
1008 ajaxTest( "jQuery.ajax() - data option - empty bodies for non-GET requests", 1, {
1009 url: "data/echoData.php",
1012 success: function( result ) {
1013 strictEqual( result, "" );
1017 var ifModifiedNow = new Date();
1020 /* jQuery.each arguments start */
1023 " (no cache)": false
1025 function( label, cache ) {
1028 "If-Modified-Since": "if_modified_since.php",
1031 function( type, url ) {
1032 url = "data/" + url + "?ts=" + ifModifiedNow++;
1033 asyncTest( "jQuery.ajax() - " + type + " support" + label, 4, function() {
1038 success: function( _, status ) {
1039 strictEqual( status, "success", "Initial status is 'success'" );
1044 success: function( data, status, jqXHR ) {
1045 strictEqual( status, "notmodified", "Following status is 'notmodified'" );
1046 strictEqual( jqXHR.status, 304, "XHR status is 304" );
1047 equal( data, null, "no response body is given" );
1049 complete: function() {
1059 /* jQuery.each arguments end */
1062 ajaxTest( "jQuery.ajax() - failing cross-domain (non-existing)", 1, {
1064 url: "http://example.invalid",
1065 error: function( xhr, _, e ) {
1066 ok( true, "file not found: " + xhr.status + " => " + e );
1070 ajaxTest( "jQuery.ajax() - failing cross-domain", 1, {
1071 url: "http://" + externalHost,
1072 error: function( xhr, _, e ) {
1073 ok( true, "access denied: " + xhr.status + " => " + e );
1077 ajaxTest( "jQuery.ajax() - atom+xml", 1, {
1078 url: url("data/atom+xml.php"),
1079 success: function() {
1080 ok( true, "success" );
1084 asyncTest( "jQuery.ajax() - statusText", 3, function() {
1085 jQuery.ajax( url("data/statusText.php?status=200&text=Hello") ).done(function( _, statusText, jqXHR ) {
1086 strictEqual( statusText, "success", "callback status text ok for success" );
1087 ok( jqXHR.statusText === "Hello" || jqXHR.statusText === "OK", "jqXHR status text ok for success (" + jqXHR.statusText + ")" );
1088 jQuery.ajax( url("data/statusText.php?status=404&text=World") ).fail(function( jqXHR, statusText ) {
1089 strictEqual( statusText, "error", "callback status text ok for error" );
1095 asyncTest( "jQuery.ajax() - statusCode", 20, function() {
1099 function countComplete() {
1105 function createStatusCodes( name, isSuccess ) {
1106 name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
1109 ok( isSuccess, name );
1112 ok( !isSuccess, name );
1118 /* jQuery.each arguments start */
1120 "data/name.html": true,
1121 "data/someFileThatDoesNotExist.html": false
1123 function( uri, isSuccess ) {
1125 jQuery.ajax( url(uri), {
1126 statusCode: createStatusCodes( "in options", isSuccess ),
1127 complete: countComplete
1130 jQuery.ajax( url(uri), {
1131 complete: countComplete
1132 }).statusCode( createStatusCodes("immediately with method", isSuccess) );
1134 jQuery.ajax( url(uri), {
1135 complete: function( jqXHR ) {
1136 jqXHR.statusCode( createStatusCodes("on complete", isSuccess) );
1141 jQuery.ajax( url(uri), {
1142 complete: function( jqXHR ) {
1143 setTimeout(function() {
1144 jqXHR.statusCode( createStatusCodes("very late binding", isSuccess) );
1150 jQuery.ajax( url(uri), {
1151 statusCode: createStatusCodes( "all (options)", isSuccess ),
1152 complete: function( jqXHR ) {
1153 jqXHR.statusCode( createStatusCodes("all (on complete)", isSuccess) );
1154 setTimeout(function() {
1155 jqXHR.statusCode( createStatusCodes("all (very late binding)", isSuccess) );
1159 }).statusCode( createStatusCodes("all (immediately with method)", isSuccess) );
1161 var testString = "";
1163 jQuery.ajax( url(uri), {
1164 success: function( a, b, jqXHR ) {
1165 ok( isSuccess, "success" );
1166 var statusCode = {};
1167 statusCode[ jqXHR.status ] = function() {
1170 jqXHR.statusCode( statusCode );
1173 error: function( jqXHR ) {
1174 ok( !isSuccess, "error" );
1175 var statusCode = {};
1176 statusCode[ jqXHR.status ] = function() {
1179 jqXHR.statusCode( statusCode );
1182 complete: function() {
1186 "Test statusCode callbacks are ordered like " + ( isSuccess ? "success" : "error" ) + " callbacks"
1193 /* jQuery.each arguments end*/
1197 ajaxTest( "jQuery.ajax() - transitive conversions", 8, [
1199 url: url("data/json.php"),
1201 "json myJson": function( data ) {
1202 ok( true, "converter called" );
1207 success: function() {
1208 ok( true, "Transitive conversion worked" );
1209 strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text" );
1210 strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType" );
1214 url: url("data/json.php"),
1216 "json myJson": function( data ) {
1217 ok( true, "converter called (*)" );
1221 contents: false, /* headers are wrong so we ignore them */
1222 dataType: "* myJson",
1223 success: function() {
1224 ok( true, "Transitive conversion worked (*)" );
1225 strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text (*)" );
1226 strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType (*)" );
1231 ajaxTest( "jQuery.ajax() - overrideMimeType", 2, [
1233 url: url("data/json.php"),
1234 beforeSend: function( xhr ) {
1235 xhr.overrideMimeType( "application/json" );
1237 success: function( json ) {
1238 ok( json.data, "Mimetype overridden using beforeSend" );
1242 url: url("data/json.php"),
1243 mimeType: "application/json",
1244 success: function( json ) {
1245 ok( json.data, "Mimetype overridden using mimeType option" );
1250 ajaxTest( "jQuery.ajax() - empty json gets to error callback instead of success callback.", 1, {
1251 url: url("data/echoData.php"),
1252 error: function( _, __, error ) {
1253 equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
1258 ajaxTest( "#2688 - jQuery.ajax() - beforeSend, cancel request", 2, {
1259 create: function() {
1260 return jQuery.ajax({
1261 url: url("data/name.html"),
1262 beforeSend: function() {
1263 ok( true, "beforeSend got called, canceling" );
1266 success: function() {
1267 ok( false, "request didn't get canceled" );
1269 complete: function() {
1270 ok( false, "request didn't get canceled" );
1273 ok( false, "request didn't get canceled" );
1277 fail: function( _, reason ) {
1278 strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
1282 ajaxTest( "#2806 - jQuery.ajax() - data option - evaluate function values", 1, {
1283 url: "data/echoQuery.php",
1289 success: function( result ) {
1290 strictEqual( result, "key=value" );
1294 test( "#7531 - jQuery.ajax() - Location object as url", 1, function () {
1299 url: window.location
1306 ok( success, "document.location did not generate exception" );
1309 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1310 ajaxTest( "#7578 - jQuery.ajax() - JSONP - default for cache option" + label, 1, {
1311 url: "data/jsonp.php",
1313 crossDomain: crossDomain,
1314 beforeSend: function() {
1315 strictEqual( this.cache, false, "cache must be false on JSON request" );
1322 ajaxTest( "#8107 - jQuery.ajax() - multiple method signatures introduced in 1.5", 4, [
1324 create: function() {
1325 return jQuery.ajax();
1328 ok( true, "With no arguments" );
1332 create: function() {
1333 return jQuery.ajax("data/name.html");
1336 ok( true, "With only string URL argument" );
1340 create: function() {
1341 return jQuery.ajax( "data/name.html", {});
1344 ok( true, "With string URL param and map" );
1348 create: function( options ) {
1349 return jQuery.ajax( options );
1351 url: "data/name.html",
1352 success: function() {
1353 ok( true, "With only map" );
1358 jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1359 ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 2, {
1360 url: "data/jsonp.php",
1362 crossDomain: crossDomain,
1363 beforeSend: function( jqXHR, s ) {
1364 s.callback = s.jsonpCallback;
1366 success: function() {
1367 var previous = this;
1368 strictEqual( previous.jsonpCallback, undefined, "jsonpCallback option is set back to default in callbacks" );
1370 url: "data/jsonp.php",
1372 crossDomain: crossDomain,
1373 beforeSend: function() {
1374 strictEqual( this.jsonpCallback, previous.callback, "JSONP callback name is re-used" );
1382 test( "#9887 - jQuery.ajax() - Context with circular references (#9887)", 2, function () {
1383 var success = false,
1385 context.field = context;
1387 jQuery.ajax( "non-existing", {
1389 beforeSend: function() {
1390 ok( this === context, "context was not deep extended" );
1398 ok( success, "context with circular reference did not generate an exception" );
1401 jQuery.each( [ "as argument", "in settings object" ], function( inSetting, title ) {
1403 function request( url, test ) {
1405 create: function() {
1406 return jQuery.ajax( inSetting ? { url: url } : url );
1409 ok( true, ( test || url ) + " " + title );
1414 ajaxTest( "#10093 - jQuery.ajax() - falsy url " + title, 4, [
1415 request( "", "empty string" ),
1418 request( undefined )
1423 ajaxTest( "#11151 - jQuery.ajax() - parse error body", 2, {
1424 url: url("data/errorWithJSON.php"),
1425 dataFilter: function( string ) {
1426 ok( false, "dataFilter called" );
1429 error: function( jqXHR ) {
1430 strictEqual( jqXHR.responseText, "{ \"code\": 40, \"message\": \"Bad Request\" }", "Error body properly set" );
1431 deepEqual( jqXHR.responseJSON, { code: 40, message: "Bad Request" }, "Error body properly parsed" );
1435 ajaxTest( "#11426 - jQuery.ajax() - loading binary data shouldn't throw an exception in IE", 1, {
1436 url: url("data/1x1.jpg"),
1437 success: function( data ) {
1438 ok( data === undefined || /JFIF/.test( data ), "success callback reached" );
1442 asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
1443 var onerror = window.onerror;
1444 window.onerror = function() {
1445 ok( true, "Exception thrown" );
1446 window.onerror = onerror;
1450 url: "data/badjson.js",
1456 jQuery.each( [ "method", "type" ], function( _, globalOption ) {
1458 function request( option ) {
1460 url: url("data/echoData.php"),
1462 success: function( msg ) {
1463 strictEqual( msg, "hello", "Check for POST (no override)" );
1467 options[ option ] = "GET";
1468 options.success = function( msg ) {
1469 strictEqual( msg, "", "Check for no POST (overriding with " + option + ")" );
1475 ajaxTest( "#12004 - jQuery.ajax() - method is an alias of type - " + globalOption + " set globally", 3, {
1478 options[ globalOption ] = "POST";
1479 jQuery.ajaxSetup( options );
1490 ajaxTest( "#13276 - jQuery.ajax() - compatibility between XML documents from ajax requests and parsed string", 1, {
1491 url: "data/dashboard.xml",
1493 success: function( ajaxXML ) {
1494 var parsedXML = jQuery( jQuery.parseXML("<tab title=\"Added\">blibli</tab>") ).find("tab");
1495 ajaxXML = jQuery( ajaxXML );
1497 // Android 2.3 doesn't automatically adopt nodes from foreign documents.
1498 // (see the comment in test/manipulation.js)
1499 // Support: Android 2.3
1500 if ( /android 2\.3/i.test( navigator.userAgent ) ) {
1501 parsedXML = jQuery( ajaxXML[ 0 ].adoptNode( parsedXML[ 0 ] ) );
1503 ajaxXML.find("infowindowtab").append( parsedXML );
1505 strictEqual( e, undefined, "error" );
1508 strictEqual( ajaxXML.find("tab").length, 3, "Parsed node was added properly" );
1512 ajaxTest( "#13292 - jQuery.ajax() - converter is bypassed for 204 requests", 3, {
1513 url: "data/nocontent.php",
1514 dataType: "testing",
1516 "* testing": function() {
1517 throw "converter was called";
1520 success: function( data, status, jqXHR ) {
1521 strictEqual( jqXHR.status, 204, "status code is 204" );
1522 strictEqual( status, "nocontent", "status text is 'nocontent'" );
1523 strictEqual( data, undefined, "data is undefined" );
1525 error: function( _, status, error ) {
1526 ok( false, "error" );
1527 strictEqual( status, "parsererror", "Parser Error" );
1528 strictEqual( error, "converter was called", "Converter was called" );
1532 ajaxTest( "#13388 - jQuery.ajax() - responseXML", 3, {
1533 url: url("data/with_fries.xml"),
1535 success: function( resp, _, jqXHR ) {
1536 notStrictEqual( resp, undefined, "XML document exists" );
1537 ok( "responseXML" in jqXHR, "jqXHR.responseXML exists" );
1538 strictEqual( resp, jqXHR.responseXML, "jqXHR.responseXML is set correctly" );
1542 ajaxTest( "#13922 - jQuery.ajax() - converter is bypassed for HEAD requests", 3, {
1543 url: "data/json.php",
1549 "text json": function() {
1550 throw "converter was called";
1553 success: function( data, status ) {
1554 ok( true, "success" );
1555 strictEqual( status, "nocontent", "data is undefined" );
1556 strictEqual( data, undefined, "data is undefined" );
1558 error: function( _, status, error ) {
1559 ok( false, "error" );
1560 strictEqual( status, "parsererror", "Parser Error" );
1561 strictEqual( error, "converter was called", "Converter was called" );
1565 testIframeWithCallback( "#14379 - jQuery.ajax() on unload", "ajax/onunload.html", function( status ) {
1567 strictEqual( status, "success", "Request completed" );
1570 ajaxTest( "#14683 - jQuery.ajax() - Exceptions thrown synchronously by xhr.send should be caught", 4, [
1572 url: "data/params_html.php",
1575 toString: function() {
1576 throw "Can't parse";
1580 done: function( data ) {
1581 ok( false, "done: " + data );
1583 fail: function( jqXHR, status, error ) {
1584 ok( true, "exception caught: " + error );
1585 strictEqual( jqXHR.status, 0, "proper status code" );
1586 strictEqual( status, "error", "proper status" );
1590 url: "http://domain.org:80d",
1591 done: function( data ) {
1592 ok( false, "done: " + data );
1594 fail: function( _, status, error ) {
1595 ok( true, "fail: " + status + " - " + error );
1600 //----------- jQuery.ajaxPrefilter()
1602 ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, {
1605 // Ensure prefix does not throw an error
1606 jQuery.ajaxPrefilter("+prefix", function( options, _, jqXHR ) {
1607 if ( options.abortInPrefilter ) {
1612 abortInPrefilter: true,
1614 ok( false, "error callback called" );
1616 fail: function( _, reason ) {
1617 strictEqual( reason, "canceled", "Request aborted by the prefilter must fail with 'canceled' status text" );
1621 //----------- jQuery.ajaxSetup()
1623 asyncTest( "jQuery.ajaxSetup()", 1, function() {
1625 url: url("data/name.php?name=foo"),
1626 success: function( msg ) {
1627 strictEqual( msg, "bar", "Check for GET" );
1634 asyncTest( "jQuery.ajaxSetup({ timeout: Number }) - with global timeout", 2, function() {
1637 ok( passed++ < 2, "Error callback executed" );
1638 if ( passed === 2 ) {
1639 jQuery( document ).off("ajaxError.setupTest");
1643 fail = function( a, b ) {
1644 ok( false, "Check for timeout failed " + a + " " + b );
1648 jQuery( document ).on( "ajaxError.setupTest", pass );
1656 url: url("data/name.php?wait=5"),
1662 asyncTest( "jQuery.ajaxSetup({ timeout: Number }) with localtimeout", 1, function() {
1669 url: url("data/name.php?wait=1"),
1671 ok( false, "Check for local timeout failed" );
1674 success: function() {
1675 ok( true, "Check for local timeout" );
1681 //----------- jQuery.domManip()
1683 test( "#11264 - jQuery.domManip() - no side effect because of ajaxSetup or global events", 1, function() {
1688 jQuery( document ).on( "ajaxStart ajaxStop", function() {
1689 ok( false, "Global event triggered" );
1692 jQuery("#qunit-fixture").append("<script src='data/ajax/evalScript.php'></script>");
1694 jQuery( document ).off("ajaxStart ajaxStop");
1697 asyncTest( "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1, function() {
1702 jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
1703 equal( method, "GET" );
1708 asyncTest( "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2, function() {
1709 jQuery("#qunit-fixture").load( "data/cleanScript.html", start );
1712 //----------- jQuery.get()
1714 asyncTest( "jQuery.get( String, Hash, Function ) - parse xml and use text() on nodes", 2, function() {
1715 jQuery.get( url("data/dashboard.xml"), function( xml ) {
1717 jQuery( "tab", xml ).each(function() {
1718 content.push( jQuery( this ).text() );
1720 strictEqual( content[ 0 ], "blabla", "Check first tab" );
1721 strictEqual( content[ 1 ], "blublu", "Check second tab" );
1726 asyncTest( "#8277 - jQuery.get( String, Function ) - data in ajaxSettings", 1, function() {
1730 jQuery.get( url("data/echoQuery.php"), function( data ) {
1731 ok( /helloworld$/.test( data ), "Data from ajaxSettings was used" );
1736 //----------- jQuery.getJSON()
1738 asyncTest( "jQuery.getJSON( String, Hash, Function ) - JSON array", 5, function() {
1740 url("data/json.php"),
1745 ok( json.length >= 2, "Check length" );
1746 strictEqual( json[ 0 ]["name"], "John", "Check JSON: first, name" );
1747 strictEqual( json[ 0 ]["age"], 21, "Check JSON: first, age" );
1748 strictEqual( json[ 1 ]["name"], "Peter", "Check JSON: second, name" );
1749 strictEqual( json[ 1 ]["age"], 25, "Check JSON: second, age" );
1755 asyncTest( "jQuery.getJSON( String, Function ) - JSON object", 2, function() {
1756 jQuery.getJSON( url("data/json.php"), function( json ) {
1757 if ( json && json["data"] ) {
1758 strictEqual( json["data"]["lang"], "en", "Check JSON: lang" );
1759 strictEqual( json["data"].length, 25, "Check JSON: length" );
1765 asyncTest( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", 2, function() {
1766 jQuery.getJSON( url( window.location.href.replace( /[^\/]*$/, "" ) + "data/json.php" ), function( json ) {
1767 strictEqual( json.data.lang, "en", "Check JSON: lang" );
1768 strictEqual( json.data.length, 25, "Check JSON: length" );
1773 //----------- jQuery.getScript()
1775 asyncTest( "jQuery.getScript( String, Function ) - with callback", 2, function() {
1776 Globals.register("testBar");
1777 jQuery.getScript( url("data/testbar.php"), function() {
1778 strictEqual( window["testBar"], "bar", "Check if script was evaluated" );
1783 asyncTest( "jQuery.getScript( String, Function ) - no callback", 1, function() {
1784 Globals.register("testBar");
1785 jQuery.getScript( url("data/testbar.php") ).done( start );
1788 asyncTest( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function() {
1789 Globals.register("testBar");
1790 jQuery.getScript( url("data/testbar.php"), function( data, _, jqXHR ) {
1791 strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
1796 //----------- jQuery.fn.load()
1798 // check if load can be called with only url
1799 asyncTest( "jQuery.fn.load( String )", 2, function() {
1801 beforeSend: function() {
1802 strictEqual( this.type, "GET", "no data means GET request" );
1805 jQuery("#first").load( "data/name.html", start );
1808 asyncTest( "jQuery.fn.load() - 404 error callbacks", 6, function() {
1809 addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError")();
1810 jQuery( document ).ajaxStop( start );
1811 jQuery("<div/>").load( "data/404.html", function() {
1812 ok( true, "complete" );
1816 // check if load can be called with url and null data
1817 asyncTest( "jQuery.fn.load( String, null )", 2, function() {
1819 beforeSend: function() {
1820 strictEqual( this.type, "GET", "no data means GET request" );
1823 jQuery("#first").load( "data/name.html", null, start );
1826 // check if load can be called with url and undefined data
1827 asyncTest( "jQuery.fn.load( String, undefined )", 2, function() {
1829 beforeSend: function() {
1830 strictEqual( this.type, "GET", "no data means GET request" );
1833 jQuery("#first").load( "data/name.html", undefined, start );
1836 // check if load can be called with only url
1837 asyncTest( "jQuery.fn.load( URL_SELECTOR )", 1, function() {
1838 jQuery("#first").load( "data/test3.html div.user", function() {
1839 strictEqual( jQuery( this ).children("div").length, 2, "Verify that specific elements were injected" );
1844 // Selector should be trimmed to avoid leading spaces (#14773)
1845 asyncTest( "jQuery.fn.load( URL_SELECTOR with spaces )", 1, function() {
1846 jQuery("#first").load( "data/test3.html #superuser ", function() {
1847 strictEqual( jQuery( this ).children("div").length, 1, "Verify that specific elements were injected" );
1852 asyncTest( "jQuery.fn.load( String, Function ) - simple: inject text into DOM", 2, function() {
1853 jQuery("#first").load( url("data/name.html"), function() {
1854 ok( /^ERROR/.test(jQuery("#first").text()), "Check if content was injected into the DOM" );
1859 asyncTest( "jQuery.fn.load( String, Function ) - check scripts", 7, function() {
1860 var verifyEvaluation = function() {
1861 strictEqual( window["testBar"], "bar", "Check if script src was evaluated after load" );
1862 strictEqual( jQuery("#ap").html(), "bar", "Check if script evaluation has modified DOM");
1866 Globals.register("testFoo");
1867 Globals.register("testBar");
1869 jQuery("#first").load( url("data/test.html"), function() {
1870 ok( jQuery("#first").html().match( /^html text/ ), "Check content after loading html" );
1871 strictEqual( jQuery("#foo").html(), "foo", "Check if script evaluation has modified DOM" );
1872 strictEqual( window["testFoo"], "foo", "Check if script was evaluated after load" );
1873 setTimeout( verifyEvaluation, 600 );
1877 asyncTest( "jQuery.fn.load( String, Function ) - check file with only a script tag", 3, function() {
1878 Globals.register("testFoo");
1880 jQuery("#first").load( url("data/test2.html"), function() {
1881 strictEqual( jQuery("#foo").html(), "foo", "Check if script evaluation has modified DOM");
1882 strictEqual( window["testFoo"], "foo", "Check if script was evaluated after load" );
1887 asyncTest( "jQuery.fn.load( String, Function ) - dataFilter in ajaxSettings", 2, function() {
1889 dataFilter: function() {
1890 return "Hello World";
1893 jQuery("<div/>").load( url("data/name.html"), function( responseText ) {
1894 strictEqual( jQuery( this ).html(), "Hello World", "Test div was filled with filtered data" );
1895 strictEqual( responseText, "Hello World", "Test callback receives filtered data" );
1900 asyncTest( "jQuery.fn.load( String, Object, Function )", 2, function() {
1901 jQuery("<div />").load( url("data/params_html.php"), {
1905 var $post = jQuery( this ).find("#post");
1906 strictEqual( $post.find("#foo").text(), "3", "Check if a hash of data is passed correctly" );
1907 strictEqual( $post.find("#bar").text(), "ok", "Check if a hash of data is passed correctly" );
1912 asyncTest( "jQuery.fn.load( String, String, Function )", 2, function() {
1913 jQuery("<div />").load( url("data/params_html.php"), "foo=3&bar=ok", function() {
1914 var $get = jQuery( this ).find("#get");
1915 strictEqual( $get.find("#foo").text(), "3", "Check if a string of data is passed correctly" );
1916 strictEqual( $get.find("#bar").text(), "ok", "Check if a of data is passed correctly" );
1921 asyncTest( "jQuery.fn.load() - callbacks get the correct parameters", 8, function() {
1922 var completeArgs = {};
1925 success: function( _, status, jqXHR ) {
1926 completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
1928 error: function( jqXHR, status ) {
1929 completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
1938 url: "data/echoQuery.php?arg=pop"
1945 function( options ) {
1946 return jQuery.Deferred(function( defer ) {
1947 jQuery("#foo").load( options.url, function() {
1948 var args = arguments;
1949 strictEqual( completeArgs[ options.url ].length, args.length, "same number of arguments (" + options.type + ")" );
1950 jQuery.each( completeArgs[ options.url ], function( i, value ) {
1951 strictEqual( args[ i ], value, "argument #" + i + " is the same (" + options.type + ")" );
1960 asyncTest( "#2046 - jQuery.fn.load( String, Function ) with ajaxSetup on dataType json", 1, function() {
1964 jQuery( document ).ajaxComplete(function( e, xml, s ) {
1965 strictEqual( s.dataType, "html", "Verify the load() dataType was html" );
1966 jQuery( document ).off("ajaxComplete");
1969 jQuery("#first").load("data/test3.html");
1972 asyncTest( "#10524 - jQuery.fn.load() - data specified in ajaxSettings is merged in", 1, function() {
1981 jQuery("#foo").load( "data/echoQuery.php", data );
1982 jQuery( document ).ajaxComplete(function( event, jqXHR, options ) {
1983 ok( ~options.data.indexOf("foo=bar"), "Data from ajaxSettings was used" );
1988 //----------- jQuery.post()
1990 asyncTest( "jQuery.post() - data", 3, function() {
1993 url("data/name.php"),
1999 jQuery( "math", xml ).each(function() {
2000 strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2001 strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2006 url: url("data/echoData.php"),
2014 success: function( data ) {
2015 strictEqual( data, "test%5Blength%5D=7&test%5Bfoo%5D=bar", "Check if a sub-object with a length param is serialized correctly" );
2018 ).always(function() {
2023 asyncTest( "jQuery.post( String, Hash, Function ) - simple with xml", 4, function() {
2026 url("data/name.php"),
2031 jQuery( "math", xml ).each(function() {
2032 strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2033 strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2037 jQuery.post( url("data/name.php?xml=5-2"), {}, function( xml ) {
2038 jQuery( "math", xml ).each(function() {
2039 strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
2040 strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2043 ).always(function() {
2048 asyncTest( "jQuery[get|post]( options ) - simple with xml", 2, function() {
2049 jQuery.when.apply( jQuery,
2050 jQuery.map( [ "get", "post" ] , function( method ) {
2051 return jQuery[ method ]({
2052 url: url( "data/name.php" ),
2056 success: function( xml ) {
2057 jQuery( "math", xml ).each(function() {
2058 strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
2063 ).always(function() {
2068 //----------- jQuery.active
2070 test( "jQuery.active", 1, function() {
2071 ok( jQuery.active === 0, "ajax active counter should be zero: " + jQuery.active );