ajax.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. module( "ajax", {
  2. setup: function() {
  3. var jsonpCallback = this.jsonpCallback = jQuery.ajaxSettings.jsonpCallback;
  4. jQuery.ajaxSettings.jsonpCallback = function() {
  5. var callback = jsonpCallback.apply( this, arguments );
  6. Globals.register( callback );
  7. return callback;
  8. };
  9. },
  10. teardown: function() {
  11. jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
  12. moduleTeardown.apply( this, arguments );
  13. }
  14. });
  15. (function() {
  16. if ( !jQuery.ajax || ( isLocal && !hasPHP ) ) {
  17. return;
  18. }
  19. function addGlobalEvents( expected ) {
  20. return function() {
  21. expected = expected || "";
  22. jQuery( document ).on( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess", function( e ) {
  23. ok( expected.indexOf(e.type) !== -1, e.type );
  24. });
  25. };
  26. }
  27. //----------- jQuery.ajax()
  28. ajaxTest( "jQuery.ajax() - success callbacks", 8, {
  29. setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
  30. url: url("data/name.html"),
  31. beforeSend: function() {
  32. ok( true, "beforeSend" );
  33. },
  34. success: function() {
  35. ok( true, "success" );
  36. },
  37. complete: function() {
  38. ok( true, "complete");
  39. }
  40. });
  41. ajaxTest( "jQuery.ajax() - success callbacks - (url, options) syntax", 8, {
  42. setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
  43. create: function( options ) {
  44. return jQuery.ajax( url("data/name.html"), options );
  45. },
  46. beforeSend: function() {
  47. ok( true, "beforeSend" );
  48. },
  49. success: function() {
  50. ok( true, "success" );
  51. },
  52. complete: function() {
  53. ok( true, "complete" );
  54. }
  55. });
  56. ajaxTest( "jQuery.ajax() - success callbacks (late binding)", 8, {
  57. setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
  58. url: url("data/name.html"),
  59. beforeSend: function() {
  60. ok( true, "beforeSend" );
  61. },
  62. success: true,
  63. afterSend: function( request ) {
  64. request.complete(function() {
  65. ok( true, "complete" );
  66. }).success(function() {
  67. ok( true, "success" );
  68. }).error(function() {
  69. ok( false, "error" );
  70. });
  71. }
  72. });
  73. ajaxTest( "jQuery.ajax() - success callbacks (oncomplete binding)", 8, {
  74. setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
  75. url: url("data/name.html"),
  76. beforeSend: function() {
  77. ok( true, "beforeSend" );
  78. },
  79. success: true,
  80. complete: function( xhr ) {
  81. xhr.complete(function() {
  82. ok( true, "complete" );
  83. }).success(function() {
  84. ok( true, "success" );
  85. }).error(function() {
  86. ok( false, "error" );
  87. });
  88. }
  89. });
  90. ajaxTest( "jQuery.ajax() - error callbacks", 8, {
  91. setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError"),
  92. url: url("data/name.php?wait=5"),
  93. beforeSend: function() {
  94. ok( true, "beforeSend" );
  95. },
  96. afterSend: function( request ) {
  97. request.abort();
  98. },
  99. error: function() {
  100. ok( true, "error" );
  101. },
  102. complete: function() {
  103. ok( true, "complete" );
  104. }
  105. });
  106. ajaxTest( "jQuery.ajax() - textStatus and errorThrown values", 4, [
  107. {
  108. url: url("data/name.php?wait=5"),
  109. error: function( _, textStatus, errorThrown ) {
  110. strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" );
  111. strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort" );
  112. },
  113. afterSend: function( request ) {
  114. request.abort();
  115. }
  116. },
  117. {
  118. url: url("data/name.php?wait=5"),
  119. error: function( _, textStatus, errorThrown ) {
  120. strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" );
  121. strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')" );
  122. },
  123. afterSend: function( request ) {
  124. request.abort("mystatus");
  125. }
  126. }
  127. ]);
  128. ajaxTest( "jQuery.ajax() - responseText on error", 1, {
  129. url: url("data/errorWithText.php"),
  130. error: function( xhr ) {
  131. strictEqual( xhr.responseText, "plain text message", "Test jqXHR.responseText is filled for HTTP errors" );
  132. }
  133. });
  134. asyncTest( "jQuery.ajax() - retry with jQuery.ajax( this )", 2, function() {
  135. var previousUrl,
  136. firstTime = true;
  137. jQuery.ajax({
  138. url: url("data/errorWithText.php"),
  139. error: function() {
  140. if ( firstTime ) {
  141. firstTime = false;
  142. jQuery.ajax( this );
  143. } else {
  144. ok ( true, "Test retrying with jQuery.ajax(this) works" );
  145. jQuery.ajax({
  146. url: url("data/errorWithText.php"),
  147. data: {
  148. "x": 1
  149. },
  150. beforeSend: function() {
  151. if ( !previousUrl ) {
  152. previousUrl = this.url;
  153. } else {
  154. strictEqual( this.url, previousUrl, "url parameters are not re-appended" );
  155. start();
  156. return false;
  157. }
  158. },
  159. error: function() {
  160. jQuery.ajax( this );
  161. }
  162. });
  163. }
  164. }
  165. });
  166. });
  167. ajaxTest( "jQuery.ajax() - headers", 4, {
  168. setup: function() {
  169. jQuery( document ).ajaxSend(function( evt, xhr ) {
  170. xhr.setRequestHeader( "ajax-send", "test" );
  171. });
  172. },
  173. url: url("data/headers.php?keys=siMPle_SometHing-elsE_OthEr_ajax-send"),
  174. headers: {
  175. "siMPle": "value",
  176. "SometHing-elsE": "other value",
  177. "OthEr": "something else"
  178. },
  179. success: function( data, _, xhr ) {
  180. var i, emptyHeader,
  181. requestHeaders = jQuery.extend( this.headers, {
  182. "ajax-send": "test"
  183. }),
  184. tmp = [];
  185. for ( i in requestHeaders ) {
  186. tmp.push( i, ": ", requestHeaders[ i ], "\n" );
  187. }
  188. tmp = tmp.join("");
  189. strictEqual( data, tmp, "Headers were sent" );
  190. strictEqual( xhr.getResponseHeader("Sample-Header"), "Hello World", "Sample header received" );
  191. emptyHeader = xhr.getResponseHeader("Empty-Header");
  192. if ( emptyHeader === null ) {
  193. ok( true, "Firefox doesn't support empty headers" );
  194. } else {
  195. strictEqual( emptyHeader, "", "Empty header received" );
  196. }
  197. strictEqual( xhr.getResponseHeader("Sample-Header2"), "Hello World 2", "Second sample header received" );
  198. }
  199. });
  200. ajaxTest( "jQuery.ajax() - Accept header", 1, {
  201. url: url("data/headers.php?keys=accept"),
  202. headers: {
  203. Accept: "very wrong accept value"
  204. },
  205. beforeSend: function( xhr ) {
  206. xhr.setRequestHeader("Accept", "*/*");
  207. },
  208. success: function( data ) {
  209. strictEqual( data, "accept: */*\n", "Test Accept header is set to last value provided" );
  210. }
  211. });
  212. ajaxTest( "jQuery.ajax() - contentType", 2, [
  213. {
  214. url: url("data/headers.php?keys=content-type"),
  215. contentType: "test",
  216. success: function( data ) {
  217. strictEqual( data, "content-type: test\n", "Test content-type is sent when options.contentType is set" );
  218. }
  219. },
  220. {
  221. url: url("data/headers.php?keys=content-type"),
  222. contentType: false,
  223. success: function( data ) {
  224. strictEqual( data, "content-type: \n", "Test content-type is not sent when options.contentType===false" );
  225. }
  226. }
  227. ]);
  228. ajaxTest( "jQuery.ajax() - protocol-less urls", 1, {
  229. url: "//somedomain.com",
  230. beforeSend: function( xhr, settings ) {
  231. equal( settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added." );
  232. return false;
  233. },
  234. error: true
  235. });
  236. ajaxTest( "jQuery.ajax() - hash", 3, [
  237. {
  238. url: "data/name.html#foo",
  239. beforeSend: function( xhr, settings ) {
  240. equal( settings.url, "data/name.html", "Make sure that the URL is trimmed." );
  241. return false;
  242. },
  243. error: true
  244. },
  245. {
  246. url: "data/name.html?abc#foo",
  247. beforeSend: function( xhr, settings ) {
  248. equal( settings.url, "data/name.html?abc", "Make sure that the URL is trimmed." );
  249. return false;
  250. },
  251. error: true
  252. },
  253. {
  254. url: "data/name.html?abc#foo",
  255. data: {
  256. "test": 123
  257. },
  258. beforeSend: function( xhr, settings ) {
  259. equal( settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed." );
  260. return false;
  261. },
  262. error: true
  263. }
  264. ]);
  265. ajaxTest( "jQuery.ajax() - cross-domain detection", 7, function() {
  266. function request( url, title, crossDomainOrOptions ) {
  267. return jQuery.extend( {
  268. dataType: "jsonp",
  269. url: url,
  270. beforeSend: function( _, s ) {
  271. ok( crossDomainOrOptions === false ? !s.crossDomain : s.crossDomain, title );
  272. return false;
  273. },
  274. error: true
  275. }, crossDomainOrOptions );
  276. }
  277. var loc = document.location,
  278. samePort = loc.port || ( loc.protocol === "http:" ? 80 : 443 ),
  279. otherPort = loc.port === 666 ? 667 : 666,
  280. otherProtocol = loc.protocol === "http:" ? "https:" : "http:";
  281. return [
  282. request(
  283. loc.protocol + "//" + loc.host + ":" + samePort,
  284. "Test matching ports are not detected as cross-domain",
  285. false
  286. ),
  287. request(
  288. otherProtocol + "//" + loc.host,
  289. "Test different protocols are detected as cross-domain"
  290. ),
  291. request(
  292. "app:/path",
  293. "Adobe AIR app:/ URL detected as cross-domain"
  294. ),
  295. request(
  296. loc.protocol + "//example.invalid:" + ( loc.port || 80 ),
  297. "Test different hostnames are detected as cross-domain"
  298. ),
  299. request(
  300. loc.protocol + "//" + loc.hostname + ":" + otherPort,
  301. "Test different ports are detected as cross-domain"
  302. ),
  303. request(
  304. "about:blank",
  305. "Test about:blank is detected as cross-domain"
  306. ),
  307. request(
  308. loc.protocol + "//" + loc.host,
  309. "Test forced crossDomain is detected as cross-domain",
  310. {
  311. crossDomain: true
  312. }
  313. )
  314. ];
  315. });
  316. ajaxTest( "jQuery.ajax() - abort", 9, {
  317. setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxError ajaxComplete"),
  318. url: url("data/name.php?wait=5"),
  319. beforeSend: function() {
  320. ok( true, "beforeSend" );
  321. },
  322. afterSend: function( xhr ) {
  323. strictEqual( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
  324. xhr.abort();
  325. strictEqual( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
  326. },
  327. error: true,
  328. complete: function() {
  329. ok( true, "complete" );
  330. }
  331. });
  332. ajaxTest( "jQuery.ajax() - events with context", 12, function() {
  333. var context = document.createElement("div");
  334. function event( e ) {
  335. equal( this, context, e.type );
  336. }
  337. function callback( msg ) {
  338. return function() {
  339. equal( this, context, "context is preserved on callback " + msg );
  340. };
  341. }
  342. return {
  343. setup: function() {
  344. jQuery( context ).appendTo("#foo")
  345. .ajaxSend( event )
  346. .ajaxComplete( event )
  347. .ajaxError( event )
  348. .ajaxSuccess( event );
  349. },
  350. requests: [{
  351. url: url("data/name.html"),
  352. context: context,
  353. beforeSend: callback("beforeSend"),
  354. success: callback("success"),
  355. complete: callback("complete")
  356. }, {
  357. url: url("data/404.html"),
  358. context: context,
  359. beforeSend: callback("beforeSend"),
  360. error: callback("error"),
  361. complete: callback("complete")
  362. }]
  363. };
  364. });
  365. ajaxTest( "jQuery.ajax() - events without context", 3, function() {
  366. function nocallback( msg ) {
  367. return function() {
  368. equal( typeof this.url, "string", "context is settings on callback " + msg );
  369. };
  370. }
  371. return {
  372. url: url("data/404.html"),
  373. beforeSend: nocallback("beforeSend"),
  374. error: nocallback("error"),
  375. complete: nocallback("complete")
  376. };
  377. });
  378. ajaxTest( "jQuery.ajax() - context modification", 1, {
  379. url: url("data/name.html"),
  380. context: {},
  381. beforeSend: function() {
  382. this.test = "foo";
  383. },
  384. afterSend: function() {
  385. strictEqual( this.context.test, "foo", "Make sure the original object is maintained." );
  386. },
  387. success: true
  388. });
  389. ajaxTest( "jQuery.ajax() - context modification through ajaxSetup", 3, function() {
  390. var obj = {};
  391. return {
  392. setup: function() {
  393. jQuery.ajaxSetup({
  394. context: obj
  395. });
  396. strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." );
  397. },
  398. requests: [{
  399. url: url("data/name.html"),
  400. success: function() {
  401. strictEqual( this, obj, "Make sure the original object is maintained." );
  402. }
  403. }, {
  404. url: url("data/name.html"),
  405. context: {},
  406. success: function() {
  407. ok( this !== obj, "Make sure overidding context is possible." );
  408. }
  409. }]
  410. };
  411. });
  412. ajaxTest( "jQuery.ajax() - disabled globals", 3, {
  413. setup: addGlobalEvents(""),
  414. global: false,
  415. url: url("data/name.html"),
  416. beforeSend: function() {
  417. ok( true, "beforeSend" );
  418. },
  419. success: function() {
  420. ok( true, "success" );
  421. },
  422. complete: function() {
  423. ok( true, "complete" );
  424. }
  425. });
  426. ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements", 3, {
  427. url: url("data/with_fries.xml"),
  428. dataType: "xml",
  429. success: function( resp ) {
  430. equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
  431. equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
  432. equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
  433. }
  434. });
  435. ajaxTest( "jQuery.ajax() - xml: non-namespace elements inside namespaced elements (over JSONP)", 3, {
  436. url: url("data/with_fries_over_jsonp.php"),
  437. dataType: "jsonp xml",
  438. success: function( resp ) {
  439. equal( jQuery( "properties", resp ).length, 1, "properties in responseXML" );
  440. equal( jQuery( "jsconf", resp ).length, 1, "jsconf in responseXML" );
  441. equal( jQuery( "thing", resp ).length, 2, "things in responseXML" );
  442. }
  443. });
  444. ajaxTest( "jQuery.ajax() - HEAD requests", 2, [
  445. {
  446. url: url("data/name.html"),
  447. type: "HEAD",
  448. success: function( data, status, xhr ) {
  449. ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response" );
  450. }
  451. },
  452. {
  453. url: url("data/name.html"),
  454. data: {
  455. "whip_it": "good"
  456. },
  457. type: "HEAD",
  458. success: function( data, status, xhr ) {
  459. ok( /Date/i.test( xhr.getAllResponseHeaders() ), "No Date in HEAD response with data" );
  460. }
  461. }
  462. ]);
  463. ajaxTest( "jQuery.ajax() - beforeSend", 1, {
  464. url: url("data/name.html"),
  465. beforeSend: function( xml ) {
  466. this.check = true;
  467. },
  468. success: function( data ) {
  469. ok( this.check, "check beforeSend was executed" );
  470. }
  471. });
  472. ajaxTest( "jQuery.ajax() - beforeSend, cancel request manually", 2, {
  473. create: function() {
  474. return jQuery.ajax({
  475. url: url("data/name.html"),
  476. beforeSend: function( xhr ) {
  477. ok( true, "beforeSend got called, canceling" );
  478. xhr.abort();
  479. },
  480. success: function() {
  481. ok( false, "request didn't get canceled" );
  482. },
  483. complete: function() {
  484. ok( false, "request didn't get canceled" );
  485. },
  486. error: function() {
  487. ok( false, "request didn't get canceled" );
  488. }
  489. });
  490. },
  491. fail: function( _, reason ) {
  492. strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
  493. }
  494. });
  495. ajaxTest( "jQuery.ajax() - dataType html", 5, {
  496. setup: function() {
  497. Globals.register("testFoo");
  498. Globals.register("testBar");
  499. },
  500. dataType: "html",
  501. url: url("data/test.html"),
  502. success: function( data ) {
  503. ok( data.match( /^html text/ ), "Check content for datatype html" );
  504. jQuery("#ap").html( data );
  505. strictEqual( window["testFoo"], "foo", "Check if script was evaluated for datatype html" );
  506. strictEqual( window["testBar"], "bar", "Check if script src was evaluated for datatype html" );
  507. }
  508. });
  509. ajaxTest( "jQuery.ajax() - synchronous request", 1, {
  510. url: url("data/json_obj.js"),
  511. dataType: "text",
  512. async: false,
  513. success: true,
  514. afterSend: function( xhr ) {
  515. ok( /^\{ "data"/.test( xhr.responseText ), "check returned text" );
  516. }
  517. });
  518. ajaxTest( "jQuery.ajax() - synchronous request with callbacks", 2, {
  519. url: url("data/json_obj.js"),
  520. async: false,
  521. dataType: "text",
  522. success: true,
  523. afterSend: function( xhr ) {
  524. var result;
  525. xhr.done(function( data ) {
  526. ok( true, "success callback executed" );
  527. result = data;
  528. });
  529. ok( /^\{ "data"/.test( result ), "check returned text" );
  530. }
  531. });
  532. asyncTest( "jQuery.ajax(), jQuery.get[Script|JSON](), jQuery.post(), pass-through request object", 8, function() {
  533. var target = "data/name.html";
  534. var successCount = 0;
  535. var errorCount = 0;
  536. var errorEx = "";
  537. var success = function() {
  538. successCount++;
  539. };
  540. jQuery( document ).on( "ajaxError.passthru", function( e, xml, s, ex ) {
  541. errorCount++;
  542. errorEx += ": " + xml.status;
  543. });
  544. jQuery( document ).one( "ajaxStop", function() {
  545. equal( successCount, 5, "Check all ajax calls successful" );
  546. equal( errorCount, 0, "Check no ajax errors (status" + errorEx + ")" );
  547. jQuery( document ).off("ajaxError.passthru");
  548. start();
  549. });
  550. Globals.register("testBar");
  551. ok( jQuery.get( url(target), success ), "get" );
  552. ok( jQuery.post( url(target), success ), "post" );
  553. ok( jQuery.getScript( url("data/test.js"), success ), "script" );
  554. ok( jQuery.getJSON( url("data/json_obj.js"), success ), "json" );
  555. ok( jQuery.ajax({
  556. url: url( target ),
  557. success: success
  558. }), "generic" );
  559. });
  560. ajaxTest( "jQuery.ajax() - cache", 12, function() {
  561. var re = /_=(.*?)(&|$)/g;
  562. function request( url, title ) {
  563. return {
  564. url: url,
  565. cache: false,
  566. beforeSend: function() {
  567. var parameter, tmp;
  568. while(( tmp = re.exec( this.url ) )) {
  569. strictEqual( parameter, undefined, title + ": only one 'no-cache' parameter" );
  570. parameter = tmp[ 1 ];
  571. notStrictEqual( parameter, "tobereplaced555", title + ": parameter (if it was there) was replaced" );
  572. }
  573. return false;
  574. },
  575. error: true
  576. };
  577. }
  578. return [
  579. request(
  580. "data/text.php",
  581. "no parameter"
  582. ),
  583. request(
  584. "data/text.php?pizza=true",
  585. "1 parameter"
  586. ),
  587. request(
  588. "data/text.php?_=tobereplaced555",
  589. "_= parameter"
  590. ),
  591. request(
  592. "data/text.php?pizza=true&_=tobereplaced555",
  593. "1 parameter and _="
  594. ),
  595. request(
  596. "data/text.php?_=tobereplaced555&tv=false",
  597. "_= and 1 parameter"
  598. ),
  599. request(
  600. "data/text.php?name=David&_=tobereplaced555&washere=true",
  601. "2 parameters surrounding _="
  602. )
  603. ];
  604. });
  605. jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
  606. ajaxTest( "jQuery.ajax() - JSONP - Query String (?n)" + label, 4, [
  607. {
  608. url: "data/jsonp.php?callback=?",
  609. dataType: "jsonp",
  610. crossDomain: crossDomain,
  611. success: function( data ) {
  612. ok( data.data, "JSON results returned (GET, url callback)" );
  613. }
  614. },
  615. {
  616. url: "data/jsonp.php?callback=??",
  617. dataType: "jsonp",
  618. crossDomain: crossDomain,
  619. success: function( data ) {
  620. ok( data.data, "JSON results returned (GET, url context-free callback)" );
  621. }
  622. },
  623. {
  624. url: "data/jsonp.php/??",
  625. dataType: "jsonp",
  626. crossDomain: crossDomain,
  627. success: function( data ) {
  628. ok( data.data, "JSON results returned (GET, REST-like)" );
  629. }
  630. },
  631. {
  632. url: "data/jsonp.php/???json=1",
  633. dataType: "jsonp",
  634. crossDomain: crossDomain,
  635. success: function( data ) {
  636. strictEqual( jQuery.type( data ), "array", "JSON results returned (GET, REST-like with param)" );
  637. }
  638. }
  639. ]);
  640. ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 9, {
  641. setup: function() {
  642. Globals.register("functionToCleanUp");
  643. Globals.register("XXX");
  644. Globals.register("jsonpResults");
  645. window["jsonpResults"] = function( data ) {
  646. ok( data["data"], "JSON results returned (GET, custom callback function)" );
  647. };
  648. },
  649. requests: [{
  650. url: "data/jsonp.php",
  651. dataType: "jsonp",
  652. crossDomain: crossDomain,
  653. jsonp: "callback",
  654. success: function( data ) {
  655. ok( data["data"], "JSON results returned (GET, data obj callback)" );
  656. }
  657. }, {
  658. url: "data/jsonp.php",
  659. dataType: "jsonp",
  660. crossDomain: crossDomain,
  661. jsonpCallback: "jsonpResults",
  662. success: function( data ) {
  663. ok( data.data, "JSON results returned (GET, custom callback name)" );
  664. }
  665. }, {
  666. url: "data/jsonp.php",
  667. dataType: "jsonp",
  668. crossDomain: crossDomain,
  669. jsonpCallback: "functionToCleanUp",
  670. success: function( data ) {
  671. ok( data["data"], "JSON results returned (GET, custom callback name to be cleaned up)" );
  672. strictEqual( window["functionToCleanUp"], undefined, "Callback was removed (GET, custom callback name to be cleaned up)" );
  673. var xhr;
  674. jQuery.ajax({
  675. url: "data/jsonp.php",
  676. dataType: "jsonp",
  677. crossDomain: crossDomain,
  678. jsonpCallback: "functionToCleanUp",
  679. beforeSend: function( jqXHR ) {
  680. xhr = jqXHR;
  681. return false;
  682. }
  683. });
  684. xhr.fail(function() {
  685. ok( true, "Ajax error JSON (GET, custom callback name to be cleaned up)" );
  686. strictEqual( window["functionToCleanUp"], undefined, "Callback was removed after early abort (GET, custom callback name to be cleaned up)" );
  687. });
  688. }
  689. }, {
  690. url: "data/jsonp.php?callback=XXX",
  691. dataType: "jsonp",
  692. jsonp: false,
  693. jsonpCallback: "XXX",
  694. crossDomain: crossDomain,
  695. beforeSend: function() {
  696. ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ), "The URL wasn't messed with (GET, custom callback name with no url manipulation)" );
  697. },
  698. success: function( data ) {
  699. ok( data["data"], "JSON results returned (GET, custom callback name with no url manipulation)" );
  700. }
  701. }]
  702. });
  703. ajaxTest( "jQuery.ajax() - JSONP - Callback in data" + label, 2, [
  704. {
  705. url: "data/jsonp.php",
  706. dataType: "jsonp",
  707. crossDomain: crossDomain,
  708. data: "callback=?",
  709. success: function( data ) {
  710. ok( data.data, "JSON results returned (GET, data callback)" );
  711. }
  712. },
  713. {
  714. url: "data/jsonp.php",
  715. dataType: "jsonp",
  716. crossDomain: crossDomain,
  717. data: "callback=??",
  718. success: function( data ) {
  719. ok( data.data, "JSON results returned (GET, data context-free callback)" );
  720. }
  721. }
  722. ]);
  723. ajaxTest( "jQuery.ajax() - JSONP - POST" + label, 3, [
  724. {
  725. type: "POST",
  726. url: "data/jsonp.php",
  727. dataType: "jsonp",
  728. crossDomain: crossDomain,
  729. success: function( data ) {
  730. ok( data["data"], "JSON results returned (POST, no callback)" );
  731. }
  732. },
  733. {
  734. type: "POST",
  735. url: "data/jsonp.php",
  736. data: "callback=?",
  737. dataType: "jsonp",
  738. crossDomain: crossDomain,
  739. success: function( data ) {
  740. ok( data["data"], "JSON results returned (POST, data callback)" );
  741. }
  742. },
  743. {
  744. type: "POST",
  745. url: "data/jsonp.php",
  746. jsonp: "callback",
  747. dataType: "jsonp",
  748. crossDomain: crossDomain,
  749. success: function( data ) {
  750. ok( data["data"], "JSON results returned (POST, data obj callback)" );
  751. }
  752. }
  753. ]);
  754. ajaxTest( "jQuery.ajax() - JSONP" + label, 3, [
  755. {
  756. url: "data/jsonp.php",
  757. dataType: "jsonp",
  758. crossDomain: crossDomain,
  759. success: function( data ) {
  760. ok( data.data, "JSON results returned (GET, no callback)" );
  761. }
  762. },
  763. {
  764. create: function( options ) {
  765. var request = jQuery.ajax( options ),
  766. promise = request.then(function( data ) {
  767. ok( data.data, "first request: JSON results returned (GET, no callback)" );
  768. request = jQuery.ajax( this ).done(function( data ) {
  769. ok( data.data, "this re-used: JSON results returned (GET, no callback)" );
  770. });
  771. promise.abort = request.abort;
  772. return request;
  773. });
  774. promise.abort = request.abort;
  775. return promise;
  776. },
  777. url: "data/jsonp.php",
  778. dataType: "jsonp",
  779. crossDomain: crossDomain,
  780. success: true
  781. }
  782. ]);
  783. });
  784. ajaxTest( "jQuery.ajax() - script, Remote", 2, {
  785. setup: function() {
  786. Globals.register("testBar");
  787. },
  788. url: window.location.href.replace( /[^\/]*$/, "" ) + "data/test.js",
  789. dataType: "script",
  790. success: function( data ) {
  791. strictEqual( window["testBar"], "bar", "Script results returned (GET, no callback)" );
  792. }
  793. });
  794. ajaxTest( "jQuery.ajax() - script, Remote with POST", 3, {
  795. setup: function() {
  796. Globals.register("testBar");
  797. },
  798. url: window.location.href.replace( /[^\/]*$/, "" ) + "data/test.js",
  799. type: "POST",
  800. dataType: "script",
  801. success: function( data, status ) {
  802. strictEqual( window["testBar"], "bar", "Script results returned (POST, no callback)" );
  803. strictEqual( status, "success", "Script results returned (POST, no callback)" );
  804. }
  805. });
  806. ajaxTest( "jQuery.ajax() - script, Remote with scheme-less URL", 2, {
  807. setup: function() {
  808. Globals.register("testBar");
  809. },
  810. url: window.location.href.replace( /[^\/]*$/, "" ).replace( /^.*?\/\//, "//" ) + "data/test.js",
  811. dataType: "script",
  812. success: function( data ) {
  813. strictEqual( window["testBar"], "bar", "Script results returned (GET, no callback)" );
  814. }
  815. });
  816. ajaxTest( "jQuery.ajax() - malformed JSON", 2, {
  817. url: "data/badjson.js",
  818. dataType: "json",
  819. error: function( xhr, msg, detailedMsg ) {
  820. strictEqual( msg, "parsererror", "A parse error occurred." );
  821. ok( /(invalid|error|exception)/i.test( detailedMsg ), "Detailed parsererror message provided" );
  822. }
  823. });
  824. ajaxTest( "jQuery.ajax() - script by content-type", 2, [
  825. {
  826. url: "data/script.php",
  827. data: {
  828. "header": "script"
  829. },
  830. success: true
  831. },
  832. {
  833. url: "data/script.php",
  834. data: {
  835. "header": "ecma"
  836. },
  837. success: true
  838. }
  839. ]);
  840. ajaxTest( "jQuery.ajax() - JSON by content-type", 5, {
  841. url: "data/json.php",
  842. data: {
  843. "header": "json",
  844. "json": "array"
  845. },
  846. success: function( json ) {
  847. ok( json.length >= 2, "Check length" );
  848. strictEqual( json[ 0 ]["name"], "John", "Check JSON: first, name" );
  849. strictEqual( json[ 0 ]["age"], 21, "Check JSON: first, age" );
  850. strictEqual( json[ 1 ]["name"], "Peter", "Check JSON: second, name" );
  851. strictEqual( json[ 1 ]["age"], 25, "Check JSON: second, age" );
  852. }
  853. });
  854. ajaxTest( "jQuery.ajax() - JSON by content-type disabled with options", 6, {
  855. url: url("data/json.php"),
  856. data: {
  857. "header": "json",
  858. "json": "array"
  859. },
  860. contents: {
  861. "json": false
  862. },
  863. success: function( text ) {
  864. strictEqual( typeof text, "string", "json wasn't auto-determined" );
  865. var json = jQuery.parseJSON( text );
  866. ok( json.length >= 2, "Check length");
  867. strictEqual( json[ 0 ]["name"], "John", "Check JSON: first, name" );
  868. strictEqual( json[ 0 ]["age"], 21, "Check JSON: first, age" );
  869. strictEqual( json[ 1 ]["name"], "Peter", "Check JSON: second, name" );
  870. strictEqual( json[ 1 ]["age"], 25, "Check JSON: second, age" );
  871. }
  872. });
  873. ajaxTest( "jQuery.ajax() - simple get", 1, {
  874. type: "GET",
  875. url: url("data/name.php?name=foo"),
  876. success: function( msg ) {
  877. strictEqual( msg, "bar", "Check for GET" );
  878. }
  879. });
  880. ajaxTest( "jQuery.ajax() - simple post", 1, {
  881. type: "POST",
  882. url: url("data/name.php"),
  883. data: "name=peter",
  884. success: function( msg ) {
  885. strictEqual( msg, "pan", "Check for POST" );
  886. }
  887. });
  888. ajaxTest( "jQuery.ajax() - data option - empty bodies for non-GET requests", 1, {
  889. url: "data/echoData.php",
  890. data: undefined,
  891. type: "post",
  892. success: function( result ) {
  893. strictEqual( result, "" );
  894. }
  895. });
  896. var ifModifiedNow = new Date();
  897. jQuery.each(
  898. /* jQuery.each arguments start */
  899. {
  900. " (cache)": true,
  901. " (no cache)": false
  902. },
  903. function( label, cache ) {
  904. var isOpera = !!window.opera;
  905. asyncTest( "jQuery.ajax() - If-Modified-Since support" + label, 3, function() {
  906. var url = "data/if_modified_since.php?ts=" + ifModifiedNow++;
  907. jQuery.ajax({
  908. url: url,
  909. ifModified: true,
  910. cache: cache,
  911. success: function( data, status ) {
  912. strictEqual( status, "success" );
  913. jQuery.ajax({
  914. url: url,
  915. ifModified: true,
  916. cache: cache,
  917. success: function( data, status ) {
  918. if ( data === "FAIL" ) {
  919. ok( isOpera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since')." );
  920. ok( isOpera, "Opera is incapable of doing .setRequestHeader('If-Modified-Since')." );
  921. } else {
  922. strictEqual( status, "notmodified" );
  923. ok( data == null, "response body should be empty" );
  924. }
  925. start();
  926. },
  927. error: function() {
  928. // Do this because opera simply refuses to implement 304 handling :(
  929. // A feature-driven way of detecting this would be appreciated
  930. // See: http://gist.github.com/599419
  931. ok( isOpera, "error" );
  932. ok( isOpera, "error" );
  933. start();
  934. }
  935. });
  936. },
  937. error: function() {
  938. strictEqual( false, "error" );
  939. // Do this because opera simply refuses to implement 304 handling :(
  940. // A feature-driven way of detecting this would be appreciated
  941. // See: http://gist.github.com/599419
  942. ok( isOpera, "error" );
  943. start();
  944. }
  945. });
  946. });
  947. asyncTest( "jQuery.ajax() - Etag support" + label, 3, function() {
  948. var url = "data/etag.php?ts=" + ifModifiedNow++;
  949. jQuery.ajax({
  950. url: url,
  951. ifModified: true,
  952. cache: cache,
  953. success: function( data, status ) {
  954. strictEqual( status, "success" );
  955. jQuery.ajax({
  956. url: url,
  957. ifModified: true,
  958. cache: cache,
  959. success: function( data, status ) {
  960. if ( data === "FAIL" ) {
  961. ok( isOpera, "Opera is incapable of doing .setRequestHeader('If-None-Match')." );
  962. ok( isOpera, "Opera is incapable of doing .setRequestHeader('If-None-Match')." );
  963. } else {
  964. strictEqual( status, "notmodified" );
  965. ok( data == null, "response body should be empty" );
  966. }
  967. start();
  968. },
  969. error: function() {
  970. // Do this because opera simply refuses to implement 304 handling :(
  971. // A feature-driven way of detecting this would be appreciated
  972. // See: http://gist.github.com/599419
  973. ok( isOpera, "error" );
  974. ok( isOpera, "error" );
  975. start();
  976. }
  977. });
  978. },
  979. error: function() {
  980. // Do this because opera simply refuses to implement 304 handling :(
  981. // A feature-driven way of detecting this would be appreciated
  982. // See: http://gist.github.com/599419
  983. ok( isOpera, "error" );
  984. start();
  985. }
  986. });
  987. });
  988. }
  989. /* jQuery.each arguments end */
  990. );
  991. ajaxTest( "jQuery.ajax() - failing cross-domain (non-existing)", 1, {
  992. // see RFC 2606
  993. url: "http://example.invalid",
  994. error: function( xhr, _, e ) {
  995. ok( true, "file not found: " + xhr.status + " => " + e );
  996. }
  997. });
  998. ajaxTest( "jQuery.ajax() - failing cross-domain", 1, {
  999. url: "http://" + externalHost,
  1000. error: function( xhr, _, e ) {
  1001. ok( true, "access denied: " + xhr.status + " => " + e );
  1002. }
  1003. });
  1004. ajaxTest( "jQuery.ajax() - atom+xml", 1, {
  1005. url: url("data/atom+xml.php"),
  1006. success: function() {
  1007. ok( true, "success" );
  1008. }
  1009. });
  1010. asyncTest( "jQuery.ajax() - statusText", 3, function() {
  1011. jQuery.ajax( url("data/statusText.php?status=200&text=Hello") ).done(function( _, statusText, jqXHR ) {
  1012. strictEqual( statusText, "success", "callback status text ok for success" );
  1013. ok( jqXHR.statusText === "Hello" || jqXHR.statusText === "OK", "jqXHR status text ok for success (" + jqXHR.statusText + ")" );
  1014. jQuery.ajax( url("data/statusText.php?status=404&text=World") ).fail(function( jqXHR, statusText ) {
  1015. strictEqual( statusText, "error", "callback status text ok for error" );
  1016. // ok( jqXHR.statusText === "World" || jQuery.browser.safari && jqXHR.statusText === "Not Found", "jqXHR status text ok for error (" + jqXHR.statusText + ")" );
  1017. start();
  1018. });
  1019. });
  1020. });
  1021. asyncTest( "jQuery.ajax() - statusCode", 20, function() {
  1022. var count = 12;
  1023. function countComplete() {
  1024. if ( ! --count ) {
  1025. start();
  1026. }
  1027. }
  1028. function createStatusCodes( name, isSuccess ) {
  1029. name = "Test " + name + " " + ( isSuccess ? "success" : "error" );
  1030. return {
  1031. 200: function() {
  1032. ok( isSuccess, name );
  1033. },
  1034. 404: function() {
  1035. ok( !isSuccess, name );
  1036. }
  1037. };
  1038. }
  1039. jQuery.each(
  1040. /* jQuery.each arguments start */
  1041. {
  1042. "data/name.html": true,
  1043. "data/someFileThatDoesNotExist.html": false
  1044. },
  1045. function( uri, isSuccess ) {
  1046. jQuery.ajax( url(uri), {
  1047. statusCode: createStatusCodes( "in options", isSuccess ),
  1048. complete: countComplete
  1049. });
  1050. jQuery.ajax( url(uri), {
  1051. complete: countComplete
  1052. }).statusCode( createStatusCodes("immediately with method", isSuccess) );
  1053. jQuery.ajax( url(uri), {
  1054. complete: function( jqXHR ) {
  1055. jqXHR.statusCode( createStatusCodes("on complete", isSuccess) );
  1056. countComplete();
  1057. }
  1058. });
  1059. jQuery.ajax( url(uri), {
  1060. complete: function( jqXHR ) {
  1061. setTimeout(function() {
  1062. jqXHR.statusCode( createStatusCodes("very late binding", isSuccess) );
  1063. countComplete();
  1064. }, 100 );
  1065. }
  1066. });
  1067. jQuery.ajax( url(uri), {
  1068. statusCode: createStatusCodes( "all (options)", isSuccess ),
  1069. complete: function( jqXHR ) {
  1070. jqXHR.statusCode( createStatusCodes("all (on complete)", isSuccess) );
  1071. setTimeout(function() {
  1072. jqXHR.statusCode( createStatusCodes("all (very late binding)", isSuccess) );
  1073. countComplete();
  1074. }, 100 );
  1075. }
  1076. }).statusCode( createStatusCodes("all (immediately with method)", isSuccess) );
  1077. var testString = "";
  1078. jQuery.ajax( url(uri), {
  1079. success: function( a, b, jqXHR ) {
  1080. ok( isSuccess, "success" );
  1081. var statusCode = {};
  1082. statusCode[ jqXHR.status ] = function() {
  1083. testString += "B";
  1084. };
  1085. jqXHR.statusCode( statusCode );
  1086. testString += "A";
  1087. },
  1088. error: function( jqXHR ) {
  1089. ok( !isSuccess, "error" );
  1090. var statusCode = {};
  1091. statusCode[ jqXHR.status ] = function() {
  1092. testString += "B";
  1093. };
  1094. jqXHR.statusCode( statusCode );
  1095. testString += "A";
  1096. },
  1097. complete: function() {
  1098. strictEqual(
  1099. testString,
  1100. "AB",
  1101. "Test statusCode callbacks are ordered like " + ( isSuccess ? "success" : "error" ) + " callbacks"
  1102. );
  1103. countComplete();
  1104. }
  1105. });
  1106. }
  1107. /* jQuery.each arguments end*/
  1108. );
  1109. });
  1110. ajaxTest( "jQuery.ajax() - transitive conversions", 8, [
  1111. {
  1112. url: url("data/json.php"),
  1113. converters: {
  1114. "json myJson": function( data ) {
  1115. ok( true, "converter called" );
  1116. return data;
  1117. }
  1118. },
  1119. dataType: "myJson",
  1120. success: function() {
  1121. ok( true, "Transitive conversion worked" );
  1122. strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text" );
  1123. strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType" );
  1124. }
  1125. },
  1126. {
  1127. url: url("data/json.php"),
  1128. converters: {
  1129. "json myJson": function( data ) {
  1130. ok( true, "converter called (*)" );
  1131. return data;
  1132. }
  1133. },
  1134. contents: false, /* headers are wrong so we ignore them */
  1135. dataType: "* myJson",
  1136. success: function() {
  1137. ok( true, "Transitive conversion worked (*)" );
  1138. strictEqual( this.dataTypes[ 0 ], "text", "response was retrieved as text (*)" );
  1139. strictEqual( this.dataTypes[ 1 ], "myjson", "request expected myjson dataType (*)" );
  1140. }
  1141. }
  1142. ]);
  1143. ajaxTest( "jQuery.ajax() - overrideMimeType", 2, [
  1144. {
  1145. url: url("data/json.php"),
  1146. beforeSend: function( xhr ) {
  1147. xhr.overrideMimeType( "application/json" );
  1148. },
  1149. success: function( json ) {
  1150. ok( json.data, "Mimetype overriden using beforeSend" );
  1151. }
  1152. },
  1153. {
  1154. url: url("data/json.php"),
  1155. mimeType: "application/json",
  1156. success: function( json ) {
  1157. ok( json.data, "Mimetype overriden using mimeType option" );
  1158. }
  1159. }
  1160. ]);
  1161. ajaxTest( "jQuery.ajax() - empty json gets to error callback instead of success callback.", 1, {
  1162. url: url("data/echoData.php"),
  1163. error: function( _, __, error ) {
  1164. equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
  1165. },
  1166. dataType: "json"
  1167. });
  1168. ajaxTest( "#2688 - jQuery.ajax() - beforeSend, cancel request", 2, {
  1169. create: function() {
  1170. return jQuery.ajax({
  1171. url: url("data/name.html"),
  1172. beforeSend: function() {
  1173. ok( true, "beforeSend got called, canceling" );
  1174. return false;
  1175. },
  1176. success: function() {
  1177. ok( false, "request didn't get canceled" );
  1178. },
  1179. complete: function() {
  1180. ok( false, "request didn't get canceled" );
  1181. },
  1182. error: function() {
  1183. ok( false, "request didn't get canceled" );
  1184. }
  1185. });
  1186. },
  1187. fail: function( _, reason ) {
  1188. strictEqual( reason, "canceled", "canceled request must fail with 'canceled' status text" );
  1189. }
  1190. });
  1191. ajaxTest( "#2806 - jQuery.ajax() - data option - evaluate function values", 1, {
  1192. url: "data/echoQuery.php",
  1193. data: {
  1194. key: function() {
  1195. return "value";
  1196. }
  1197. },
  1198. success: function( result ) {
  1199. strictEqual( result, "key=value" );
  1200. }
  1201. });
  1202. test( "#7531 - jQuery.ajax() - Location object as url", 1, function () {
  1203. var success = false;
  1204. try {
  1205. var xhr = jQuery.ajax({
  1206. url: window.location
  1207. });
  1208. success = true;
  1209. xhr.abort();
  1210. } catch (e) {
  1211. }
  1212. ok( success, "document.location did not generate exception" );
  1213. });
  1214. jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
  1215. ajaxTest( "#7578 - jQuery.ajax() - JSONP - default for cache option" + label, 1, {
  1216. url: "data/jsonp.php",
  1217. dataType: "jsonp",
  1218. crossDomain: crossDomain,
  1219. beforeSend: function( jqXHR, s ) {
  1220. strictEqual( this.cache, false, "cache must be false on JSON request" );
  1221. return false;
  1222. },
  1223. error: true
  1224. });
  1225. });
  1226. ajaxTest( "#8107 - jQuery.ajax() - multiple method signatures introduced in 1.5", 4, [
  1227. {
  1228. create: function() {
  1229. return jQuery.ajax();
  1230. },
  1231. done: function() {
  1232. ok( true, "With no arguments" );
  1233. }
  1234. },
  1235. {
  1236. create: function() {
  1237. return jQuery.ajax("data/name.html");
  1238. },
  1239. done: function() {
  1240. ok( true, "With only string URL argument" );
  1241. }
  1242. },
  1243. {
  1244. create: function() {
  1245. return jQuery.ajax( "data/name.html", {});
  1246. },
  1247. done: function() {
  1248. ok( true, "With string URL param and map" );
  1249. }
  1250. },
  1251. {
  1252. create: function( options ) {
  1253. return jQuery.ajax( options );
  1254. },
  1255. url: "data/name.html",
  1256. success: function() {
  1257. ok( true, "With only map" );
  1258. }
  1259. }
  1260. ]);
  1261. jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
  1262. ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 2, {
  1263. url: "data/jsonp.php",
  1264. dataType: "jsonp",
  1265. crossDomain: crossDomain,
  1266. beforeSend: function( jqXHR, s ) {
  1267. s.callback = s.jsonpCallback;
  1268. },
  1269. success: function() {
  1270. var previous = this;
  1271. strictEqual( previous.jsonpCallback, undefined, "jsonpCallback option is set back to default in callbacks" );
  1272. jQuery.ajax({
  1273. url: "data/jsonp.php",
  1274. dataType: "jsonp",
  1275. crossDomain: crossDomain,
  1276. beforeSend: function() {
  1277. strictEqual( this.jsonpCallback, previous.callback, "JSONP callback name is re-used" );
  1278. return false;
  1279. }
  1280. });
  1281. }
  1282. });
  1283. });
  1284. test( "#9887 - jQuery.ajax() - Context with circular references (#9887)", 2, function () {
  1285. var success = false,
  1286. context = {};
  1287. context.field = context;
  1288. try {
  1289. jQuery.ajax( "non-existing", {
  1290. context: context,
  1291. beforeSend: function() {
  1292. ok( this === context, "context was not deep extended" );
  1293. return false;
  1294. }
  1295. });
  1296. success = true;
  1297. } catch ( e ) {
  1298. console.log( e );
  1299. }
  1300. ok( success, "context with circular reference did not generate an exception" );
  1301. });
  1302. jQuery.each( [ "as argument", "in settings object" ], function( inSetting, title ) {
  1303. function request( url, test ) {
  1304. return {
  1305. create: function() {
  1306. return jQuery.ajax( inSetting ? { url: url } : url );
  1307. },
  1308. done: function() {
  1309. ok( true, ( test || url ) + " " + title );
  1310. }
  1311. };
  1312. }
  1313. ajaxTest( "#10093 - jQuery.ajax() - falsy url " + title, 4, [
  1314. request( "", "empty string" ),
  1315. request( false ),
  1316. request( null ),
  1317. request( undefined )
  1318. ]);
  1319. });
  1320. ajaxTest( "#11426 - jQuery.ajax() - loading binary data shouldn't throw an exception in IE", 1, {
  1321. url: url("data/1x1.jpg"),
  1322. success: function( data ) {
  1323. ok( data === undefined || /JFIF/.test( data ), "success callback reached" );
  1324. }
  1325. });
  1326. test( "#11743 - jQuery.ajax() - script, throws exception", 1, function() {
  1327. raises(function() {
  1328. jQuery.ajax({
  1329. url: "data/badjson.js",
  1330. dataType: "script",
  1331. throws: true,
  1332. // TODO find a way to test this asynchronously, too
  1333. async: false,
  1334. // Global events get confused by the exception
  1335. global: false,
  1336. success: function() {
  1337. ok( false, "Success." );
  1338. },
  1339. error: function() {
  1340. ok( false, "Error." );
  1341. }
  1342. });
  1343. }, "exception bubbled" );
  1344. });
  1345. jQuery.each( [ "method", "type" ], function( _, globalOption ) {
  1346. function request( option ) {
  1347. var options = {
  1348. url: url("data/echoData.php"),
  1349. data: "hello",
  1350. success: function( msg ) {
  1351. strictEqual( msg, "hello", "Check for POST (no override)" );
  1352. }
  1353. };
  1354. if ( option ) {
  1355. options[ option ] = "GET";
  1356. options.success = function( msg ) {
  1357. strictEqual( msg, "", "Check for no POST (overriding with " + option + ")" );
  1358. };
  1359. }
  1360. return options;
  1361. }
  1362. ajaxTest( "#12004 - jQuery.ajax() - method is an alias of type - " + globalOption + " set globally", 3, {
  1363. setup: function() {
  1364. var options = {};
  1365. options[ globalOption ] = "POST";
  1366. jQuery.ajaxSetup( options );
  1367. },
  1368. requests: [
  1369. request("type"),
  1370. request("method"),
  1371. request()
  1372. ]
  1373. });
  1374. });
  1375. ajaxTest( "#13276 - jQuery.ajax() - compatibility between XML documents from ajax requests and parsed string", 1, {
  1376. url: "data/dashboard.xml",
  1377. dataType: "xml",
  1378. success: function( ajaxXML ) {
  1379. var parsedXML = jQuery( jQuery.parseXML("<tab title=\"Added\">blibli</tab>") ).find("tab");
  1380. ajaxXML = jQuery( ajaxXML );
  1381. try {
  1382. ajaxXML.find("infowindowtab").append( parsedXML );
  1383. } catch( e ) {
  1384. strictEqual( e, undefined, "error" );
  1385. return;
  1386. }
  1387. strictEqual( ajaxXML.find("tab").length, 3, "Parsed node was added properly" );
  1388. }
  1389. });
  1390. ajaxTest( "#13292 - jQuery.ajax() - converter is bypassed for 204 requests", 3, {
  1391. url: "data/nocontent.php",
  1392. dataType: "testing",
  1393. converters: {
  1394. "* testing": function() {
  1395. throw "converter was called";
  1396. }
  1397. },
  1398. success: function( data, status, jqXHR ) {
  1399. strictEqual( jqXHR.status, 204, "status code is 204" );
  1400. strictEqual( status, "nocontent", "status text is 'nocontent'" );
  1401. strictEqual( data, undefined, "data is undefined" );
  1402. },
  1403. error: function( _, status, error ) {
  1404. ok( false, "error" );
  1405. strictEqual( status, "parsererror", "Parser Error" );
  1406. strictEqual( error, "converter was called", "Converter was called" );
  1407. }
  1408. });
  1409. //----------- jQuery.ajaxPrefilter()
  1410. ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, {
  1411. setup: function() {
  1412. jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
  1413. if ( options.abortInPrefilter ) {
  1414. jqXHR.abort();
  1415. }
  1416. });
  1417. },
  1418. abortInPrefilter: true,
  1419. error: function() {
  1420. ok( false, "error callback called" );
  1421. },
  1422. fail: function( _, reason ) {
  1423. strictEqual( reason, "canceled", "Request aborted by the prefilter must fail with 'canceled' status text" );
  1424. }
  1425. });
  1426. //----------- jQuery.ajaxSetup()
  1427. asyncTest( "jQuery.ajaxSetup()", 1, function() {
  1428. jQuery.ajaxSetup({
  1429. url: url("data/name.php?name=foo"),
  1430. success: function( msg ) {
  1431. strictEqual( msg, "bar", "Check for GET" );
  1432. start();
  1433. }
  1434. });
  1435. jQuery.ajax();
  1436. });
  1437. asyncTest( "jQuery.ajaxSetup({ timeout: Number }) - with global timeout", 2, function() {
  1438. var passed = 0,
  1439. pass = function() {
  1440. ok( passed++ < 2, "Error callback executed" );
  1441. if ( passed == 2 ) {
  1442. jQuery( document ).off("ajaxError.setupTest");
  1443. start();
  1444. }
  1445. },
  1446. fail = function( a, b, c ) {
  1447. ok( false, "Check for timeout failed " + a + " " + b );
  1448. start();
  1449. };
  1450. jQuery( document ).on( "ajaxError.setupTest", pass );
  1451. jQuery.ajaxSetup({
  1452. timeout: 1000
  1453. });
  1454. jQuery.ajax({
  1455. type: "GET",
  1456. url: url("data/name.php?wait=5"),
  1457. error: pass,
  1458. success: fail
  1459. });
  1460. });
  1461. asyncTest( "jQuery.ajaxSetup({ timeout: Number }) with localtimeout", 1, function() {
  1462. jQuery.ajaxSetup({
  1463. timeout: 50
  1464. });
  1465. jQuery.ajax({
  1466. type: "GET",
  1467. timeout: 15000,
  1468. url: url("data/name.php?wait=1"),
  1469. error: function() {
  1470. ok( false, "Check for local timeout failed" );
  1471. start();
  1472. },
  1473. success: function() {
  1474. ok( true, "Check for local timeout" );
  1475. start();
  1476. }
  1477. });
  1478. });
  1479. //----------- jQuery.domManip()
  1480. test( "#11264 - jQuery.domManip() - no side effect because of ajaxSetup or global events", 1, function() {
  1481. jQuery.ajaxSetup({
  1482. type: "POST"
  1483. });
  1484. jQuery( document ).bind( "ajaxStart ajaxStop", function() {
  1485. ok( false, "Global event triggered" );
  1486. });
  1487. jQuery("#qunit-fixture").append("<script src='data/evalScript.php'></script>");
  1488. jQuery( document ).unbind("ajaxStart ajaxStop");
  1489. });
  1490. asyncTest( "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2, function() {
  1491. jQuery("#qunit-fixture").load( "data/cleanScript.html", start );
  1492. });
  1493. //----------- jQuery.get()
  1494. asyncTest( "jQuery.get( String, Hash, Function ) - parse xml and use text() on nodes", 2, function() {
  1495. jQuery.get( url("data/dashboard.xml"), function( xml ) {
  1496. var content = [];
  1497. jQuery( "tab", xml ).each(function() {
  1498. content.push( jQuery( this ).text() );
  1499. });
  1500. strictEqual( content[ 0 ], "blabla", "Check first tab" );
  1501. strictEqual( content[ 1 ], "blublu", "Check second tab" );
  1502. start();
  1503. });
  1504. });
  1505. asyncTest( "#8277 - jQuery.get( String, Function ) - data in ajaxSettings", 1, function() {
  1506. jQuery.ajaxSetup({
  1507. data: "helloworld"
  1508. });
  1509. jQuery.get( url("data/echoQuery.php"), function( data ) {
  1510. ok( /helloworld$/.test( data ), "Data from ajaxSettings was used" );
  1511. start();
  1512. });
  1513. });
  1514. //----------- jQuery.getJSON()
  1515. asyncTest( "jQuery.getJSON( String, Hash, Function ) - JSON array", 5, function() {
  1516. jQuery.getJSON(
  1517. url("data/json.php"),
  1518. {
  1519. "json": "array"
  1520. },
  1521. function( json ) {
  1522. ok( json.length >= 2, "Check length" );
  1523. strictEqual( json[ 0 ]["name"], "John", "Check JSON: first, name" );
  1524. strictEqual( json[ 0 ]["age"], 21, "Check JSON: first, age" );
  1525. strictEqual( json[ 1 ]["name"], "Peter", "Check JSON: second, name" );
  1526. strictEqual( json[ 1 ]["age"], 25, "Check JSON: second, age" );
  1527. start();
  1528. }
  1529. );
  1530. });
  1531. asyncTest( "jQuery.getJSON( String, Function ) - JSON object", 2, function() {
  1532. jQuery.getJSON( url("data/json.php"), function( json ) {
  1533. if ( json && json["data"] ) {
  1534. strictEqual( json["data"]["lang"], "en", "Check JSON: lang" );
  1535. strictEqual( json["data"].length, 25, "Check JSON: length" );
  1536. start();
  1537. }
  1538. });
  1539. });
  1540. asyncTest( "jQuery.getJSON() - Using Native JSON", 2, function() {
  1541. var restore = "JSON" in window,
  1542. old = window.JSON;
  1543. if ( !restore ) {
  1544. Globals.register("JSON");
  1545. }
  1546. window.JSON = {
  1547. parse: function( str ) {
  1548. ok( true, "Verifying that parse method was run" );
  1549. window.JSON = old;
  1550. return true;
  1551. }
  1552. };
  1553. jQuery.getJSON( url("data/json.php"), function( json ) {
  1554. strictEqual( json, true, "Verifying return value" );
  1555. start();
  1556. });
  1557. });
  1558. asyncTest( "jQuery.getJSON( String, Function ) - JSON object with absolute url to local content", 2, function() {
  1559. jQuery.getJSON( url( window.location.href.replace( /[^\/]*$/, "" ) + "data/json.php" ), function( json ) {
  1560. strictEqual( json.data.lang, "en", "Check JSON: lang" );
  1561. strictEqual( json.data.length, 25, "Check JSON: length" );
  1562. start();
  1563. });
  1564. });
  1565. //----------- jQuery.getScript()
  1566. asyncTest( "jQuery.getScript( String, Function ) - with callback", 2, function() {
  1567. Globals.register("testBar");
  1568. jQuery.getScript( url("data/test.js"), function( data, _, jqXHR ) {
  1569. strictEqual( window["testBar"], "bar", "Check if script was evaluated" );
  1570. start();
  1571. });
  1572. });
  1573. asyncTest( "jQuery.getScript( String, Function ) - no callback", 1, function() {
  1574. Globals.register("testBar");
  1575. jQuery.getScript( url("data/test.js") ).done( start );
  1576. });
  1577. asyncTest( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function() {
  1578. Globals.register("testBar");
  1579. jQuery.getScript( url("data/test.js"), function( data, _, jqXHR ) {
  1580. strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
  1581. start();
  1582. });
  1583. });
  1584. //----------- jQuery.fn.load()
  1585. // check if load can be called with only url
  1586. asyncTest( "jQuery.fn.load( String )", 2, function() {
  1587. jQuery.ajaxSetup({
  1588. beforeSend: function() {
  1589. strictEqual( this.type, "GET", "no data means GET request" );
  1590. }
  1591. });
  1592. jQuery("#first").load( "data/name.html", start );
  1593. });
  1594. asyncTest( "jQuery.fn.load() - 404 error callbacks", 6, function() {
  1595. addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError")();
  1596. jQuery( document ).ajaxStop( start );
  1597. jQuery("<div/>").load( "data/404.html", function() {
  1598. ok( true, "complete" );
  1599. });
  1600. });
  1601. // check if load can be called with url and null data
  1602. asyncTest( "jQuery.fn.load( String, null )", 2, function() {
  1603. jQuery.ajaxSetup({
  1604. beforeSend: function() {
  1605. strictEqual( this.type, "GET", "no data means GET request" );
  1606. }
  1607. });
  1608. jQuery("#first").load( "data/name.html", null, start );
  1609. });
  1610. // check if load can be called with url and undefined data
  1611. asyncTest( "jQuery.fn.load( String, undefined )", 2, function() {
  1612. jQuery.ajaxSetup({
  1613. beforeSend: function() {
  1614. strictEqual( this.type, "GET", "no data means GET request" );
  1615. }
  1616. });
  1617. jQuery("#first").load( "data/name.html", undefined, start );
  1618. });
  1619. // check if load can be called with only url
  1620. asyncTest( "jQuery.fn.load( URL_SELECTOR )", 1, function() {
  1621. jQuery("#first").load( "data/test3.html div.user", function() {
  1622. strictEqual( jQuery( this ).children("div").length, 2, "Verify that specific elements were injected" );
  1623. start();
  1624. });
  1625. });
  1626. asyncTest( "jQuery.fn.load( String, Function ) - simple: inject text into DOM", 2, function() {
  1627. jQuery("#first").load( url("data/name.html"), function() {
  1628. ok( /^ERROR/.test(jQuery("#first").text()), "Check if content was injected into the DOM" );
  1629. start();
  1630. });
  1631. });
  1632. asyncTest( "jQuery.fn.load( String, Function ) - check scripts", 7, function() {
  1633. var verifyEvaluation = function() {
  1634. strictEqual( window["testBar"], "bar", "Check if script src was evaluated after load" );
  1635. strictEqual( jQuery("#ap").html(), "bar", "Check if script evaluation has modified DOM");
  1636. start();
  1637. };
  1638. Globals.register("testFoo");
  1639. Globals.register("testBar");
  1640. jQuery("#first").load( url("data/test.html"), function() {
  1641. ok( jQuery("#first").html().match( /^html text/ ), "Check content after loading html" );
  1642. strictEqual( jQuery("#foo").html(), "foo", "Check if script evaluation has modified DOM" );
  1643. strictEqual( window["testFoo"], "foo", "Check if script was evaluated after load" );
  1644. setTimeout( verifyEvaluation, 600 );
  1645. });
  1646. });
  1647. asyncTest( "jQuery.fn.load( String, Function ) - check file with only a script tag", 3, function() {
  1648. Globals.register("testFoo");
  1649. jQuery("#first").load( url("data/test2.html"), function() {
  1650. strictEqual( jQuery("#foo").html(), "foo", "Check if script evaluation has modified DOM");
  1651. strictEqual( window["testFoo"], "foo", "Check if script was evaluated after load" );
  1652. start();
  1653. });
  1654. });
  1655. asyncTest( "jQuery.fn.load( String, Function ) - dataFilter in ajaxSettings", 2, function() {
  1656. jQuery.ajaxSetup({
  1657. dataFilter: function() {
  1658. return "Hello World";
  1659. }
  1660. });
  1661. jQuery("<div/>").load( url("data/name.html"), function( responseText ) {
  1662. strictEqual( jQuery( this ).html(), "Hello World", "Test div was filled with filtered data" );
  1663. strictEqual( responseText, "Hello World", "Test callback receives filtered data" );
  1664. start();
  1665. });
  1666. });
  1667. asyncTest( "jQuery.fn.load( String, Object, Function )", 2, function() {
  1668. jQuery("<div />").load( url("data/params_html.php"), {
  1669. "foo": 3,
  1670. "bar": "ok"
  1671. }, function() {
  1672. var $post = jQuery( this ).find("#post");
  1673. strictEqual( $post.find("#foo").text(), "3", "Check if a hash of data is passed correctly" );
  1674. strictEqual( $post.find("#bar").text(), "ok", "Check if a hash of data is passed correctly" );
  1675. start();
  1676. });
  1677. });
  1678. asyncTest( "jQuery.fn.load( String, String, Function )", 2, function() {
  1679. jQuery("<div />").load( url("data/params_html.php"), "foo=3&bar=ok", function() {
  1680. var $get = jQuery( this ).find("#get");
  1681. strictEqual( $get.find("#foo").text(), "3", "Check if a string of data is passed correctly" );
  1682. strictEqual( $get.find("#bar").text(), "ok", "Check if a of data is passed correctly" );
  1683. start();
  1684. });
  1685. });
  1686. asyncTest( "jQuery.fn.load() - callbacks get the correct parameters", 8, function() {
  1687. var slice = [].slice,
  1688. completeArgs = {};
  1689. jQuery.ajaxSetup({
  1690. success: function( _, status, jqXHR ) {
  1691. completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
  1692. },
  1693. error: function( jqXHR, status ) {
  1694. completeArgs[ this.url ] = [ jqXHR.responseText, status, jqXHR ];
  1695. }
  1696. });
  1697. jQuery.when.apply(
  1698. jQuery,
  1699. jQuery.map([
  1700. {
  1701. type: "success",
  1702. url: "data/echoQuery.php?arg=pop"
  1703. },
  1704. {
  1705. type: "error",
  1706. url: "data/404.php"
  1707. }
  1708. ],
  1709. function( options ) {
  1710. return jQuery.Deferred(function( defer ) {
  1711. jQuery("#foo").load( options.url, function() {
  1712. var args = arguments;
  1713. strictEqual( completeArgs[ options.url ].length, args.length, "same number of arguments (" + options.type + ")" );
  1714. jQuery.each( completeArgs[ options.url ], function( i, value ) {
  1715. strictEqual( args[ i ], value, "argument #" + i + " is the same (" + options.type + ")" );
  1716. });
  1717. defer.resolve();
  1718. });
  1719. });
  1720. })
  1721. ).always( start );
  1722. });
  1723. asyncTest( "#2046 - jQuery.fn.load( String, Function ) with ajaxSetup on dataType json", 1, function() {
  1724. jQuery.ajaxSetup({
  1725. dataType: "json"
  1726. });
  1727. jQuery( document ).ajaxComplete(function( e, xml, s ) {
  1728. strictEqual( s.dataType, "html", "Verify the load() dataType was html" );
  1729. jQuery( document ).unbind("ajaxComplete");
  1730. start();
  1731. });
  1732. jQuery("#first").load("data/test3.html");
  1733. });
  1734. asyncTest( "#10524 - jQuery.fn.load() - data specified in ajaxSettings is merged in", 1, function() {
  1735. var data = {
  1736. "baz": 1
  1737. };
  1738. jQuery.ajaxSetup({
  1739. data: {
  1740. "foo": "bar"
  1741. }
  1742. });
  1743. jQuery("#foo").load( "data/echoQuery.php", data );
  1744. jQuery( document ).ajaxComplete(function( event, jqXHR, options ) {
  1745. ok( ~options.data.indexOf("foo=bar"), "Data from ajaxSettings was used" );
  1746. start();
  1747. });
  1748. });
  1749. //----------- jQuery.post()
  1750. asyncTest( "jQuery.post() - data", 3, function() {
  1751. jQuery.when(
  1752. jQuery.post(
  1753. url("data/name.php"),
  1754. {
  1755. xml: "5-2",
  1756. length: 3
  1757. },
  1758. function( xml ) {
  1759. jQuery( "math", xml ).each(function() {
  1760. strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
  1761. strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  1762. });
  1763. }
  1764. ),
  1765. jQuery.ajax({
  1766. url: url("data/echoData.php"),
  1767. type: "POST",
  1768. data: {
  1769. "test": {
  1770. "length": 7,
  1771. "foo": "bar"
  1772. }
  1773. },
  1774. success: function( data ) {
  1775. strictEqual( data, "test%5Blength%5D=7&test%5Bfoo%5D=bar", "Check if a sub-object with a length param is serialized correctly" );
  1776. }
  1777. })
  1778. ).always( start );
  1779. });
  1780. asyncTest( "jQuery.post( String, Hash, Function ) - simple with xml", 4, function() {
  1781. jQuery.when(
  1782. jQuery.post(
  1783. url("data/name.php"),
  1784. {
  1785. "xml": "5-2"
  1786. },
  1787. function( xml ) {
  1788. jQuery( "math", xml ).each(function() {
  1789. strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
  1790. strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  1791. });
  1792. }
  1793. ),
  1794. jQuery.post( url("data/name.php?xml=5-2"), {}, function( xml ) {
  1795. jQuery( "math", xml ).each(function() {
  1796. strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
  1797. strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
  1798. });
  1799. })
  1800. ).always( start );
  1801. });
  1802. //----------- jQuery.active
  1803. test( "jQuery.active", 1, function() {
  1804. ok( jQuery.active === 0, "ajax active counter should be zero: " + jQuery.active );
  1805. });
  1806. })();