3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 //if ( !jQuery.browser.safari ) {
10 test("jQuery.ajax() - success callbacks", function() {
13 jQuery.ajaxSetup({ timeout: 0 });
17 jQuery('#foo').ajaxStart(function(){
18 ok( true, "ajaxStart" );
19 }).ajaxStop(function(){
20 ok( true, "ajaxStop" );
22 }).ajaxSend(function(){
23 ok( true, "ajaxSend" );
24 }).ajaxComplete(function(){
25 ok( true, "ajaxComplete" );
26 }).ajaxError(function(){
27 ok( false, "ajaxError" );
28 }).ajaxSuccess(function(){
29 ok( true, "ajaxSuccess" );
33 url: url("data/name.html"),
34 beforeSend: function(){ ok(true, "beforeSend"); },
35 success: function(){ ok(true, "success"); },
36 error: function(){ ok(false, "error"); },
37 complete: function(){ ok(true, "complete"); }
41 test("jQuery.ajax() - error callbacks", function() {
45 jQuery('#foo').ajaxStart(function(){
46 ok( true, "ajaxStart" );
47 }).ajaxStop(function(){
48 ok( true, "ajaxStop" );
50 }).ajaxSend(function(){
51 ok( true, "ajaxSend" );
52 }).ajaxComplete(function(){
53 ok( true, "ajaxComplete" );
54 }).ajaxError(function(){
55 ok( true, "ajaxError" );
56 }).ajaxSuccess(function(){
57 ok( false, "ajaxSuccess" );
60 jQuery.ajaxSetup({ timeout: 500 });
63 url: url("data/name.php?wait=5"),
64 beforeSend: function(){ ok(true, "beforeSend"); },
65 success: function(){ ok(false, "success"); },
66 error: function(){ ok(true, "error"); },
67 complete: function(){ ok(true, "complete"); }
71 test(".ajax() - hash", function() {
75 url: "data/name.html#foo",
76 beforeSend: function( xhr, settings ) {
77 equals(settings.url, "data/name.html", "Make sure that the URL is trimmed.");
83 url: "data/name.html?abc#foo",
84 beforeSend: function( xhr, settings ) {
85 equals(settings.url, "data/name.html?abc", "Make sure that the URL is trimmed.");
91 url: "data/name.html?abc#foo",
92 data: { "test": 123 },
93 beforeSend: function( xhr, settings ) {
94 equals(settings.url, "data/name.html?abc&test=123", "Make sure that the URL is trimmed.");
100 test(".ajax() - 304", function() {
105 url: url("data/notmodified.php"),
106 success: function(){ ok(true, "304 ok"); },
107 // Do this because opera simply refuses to implement 304 handling :(
108 // A feature-driven way of detecting this would be appreciated
109 // See: http://gist.github.com/599419
110 error: function(){ ok(jQuery.browser.opera, "304 not ok "); },
111 complete: function(xhr){ start(); }
115 test(".load()) - 404 error callbacks", function() {
119 jQuery('#foo').ajaxStart(function(){
120 ok( true, "ajaxStart" );
121 }).ajaxStop(function(){
122 ok( true, "ajaxStop" );
124 }).ajaxSend(function(){
125 ok( true, "ajaxSend" );
126 }).ajaxComplete(function(){
127 ok( true, "ajaxComplete" );
128 }).ajaxError(function(){
129 ok( true, "ajaxError" );
130 }).ajaxSuccess(function(){
131 ok( false, "ajaxSuccess" );
134 jQuery("<div/>").load("data/404.html", function(){
135 ok(true, "complete");
139 test("jQuery.ajax() - abort", function() {
143 jQuery('#foo').ajaxStart(function(){
144 ok( true, "ajaxStart" );
145 }).ajaxStop(function(){
146 ok( true, "ajaxStop" );
148 }).ajaxSend(function(){
149 ok( true, "ajaxSend" );
150 }).ajaxComplete(function(){
151 ok( true, "ajaxComplete" );
154 var xhr = jQuery.ajax({
155 url: url("data/name.php?wait=5"),
156 beforeSend: function(){ ok(true, "beforeSend"); },
157 complete: function(){ ok(true, "complete"); }
160 equals( xhr.readyState, 1, "XHR readyState indicates successful dispatch" );
163 equals( xhr.readyState, 0, "XHR readyState indicates successful abortion" );
166 test("Ajax events with context", function() {
170 var context = document.createElement("div");
173 equals( this, context, e.type );
176 function callback(msg){
178 equals( this, context, "context is preserved on callback " + msg );
182 function nocallback(msg){
184 equals( typeof this.url, "string", "context is settings on callback " + msg );
188 jQuery('#foo').add(context)
195 url: url("data/name.html"),
196 beforeSend: callback("beforeSend"),
197 success: callback("success"),
198 error: callback("error"),
200 callback("complete").call(this);
203 url: url("data/404.html"),
205 beforeSend: callback("beforeSend"),
206 error: callback("error"),
207 complete: function(){
208 callback("complete").call(this);
210 jQuery('#foo').add(context).unbind();
213 url: url("data/404.html"),
214 beforeSend: nocallback("beforeSend"),
215 error: nocallback("error"),
216 complete: function(){
217 nocallback("complete").call(this);
228 test("jQuery.ajax context modification", function() {
236 url: url("data/name.html"),
238 beforeSend: function(){
241 complete: function() {
246 equals( obj.test, "foo", "Make sure the original object is maintained." );
249 test("jQuery.ajax() - disabled globals", function() {
253 jQuery('#foo').ajaxStart(function(){
254 ok( false, "ajaxStart" );
255 }).ajaxStop(function(){
256 ok( false, "ajaxStop" );
257 }).ajaxSend(function(){
258 ok( false, "ajaxSend" );
259 }).ajaxComplete(function(){
260 ok( false, "ajaxComplete" );
261 }).ajaxError(function(){
262 ok( false, "ajaxError" );
263 }).ajaxSuccess(function(){
264 ok( false, "ajaxSuccess" );
269 url: url("data/name.html"),
270 beforeSend: function(){ ok(true, "beforeSend"); },
271 success: function(){ ok(true, "success"); },
272 error: function(){ ok(false, "error"); },
273 complete: function(){
274 ok(true, "complete");
275 setTimeout(function(){ start(); }, 13);
280 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
284 url: url("data/with_fries.xml"),
286 success: function(resp) {
287 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
288 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
289 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
295 test("jQuery.ajax - HEAD requests", function() {
300 url: url("data/name.html"),
302 success: function(data, status, xhr){
303 var h = xhr.getAllResponseHeaders();
304 ok( /Date/i.test(h), 'No Date in HEAD response' );
307 url: url("data/name.html"),
308 data: { whip_it: "good" },
310 success: function(data, status, xhr){
311 var h = xhr.getAllResponseHeaders();
312 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
321 test("jQuery.ajax - beforeSend", function() {
327 jQuery.ajaxSetup({ timeout: 0 });
330 url: url("data/name.html"),
331 beforeSend: function(xml) {
334 success: function(data) {
335 ok( check, "check beforeSend was executed" );
341 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
343 var request = jQuery.ajax({
344 url: url("data/name.html"),
345 beforeSend: function() {
346 ok( true, "beforeSend got called, canceling" );
349 success: function() {
350 ok( false, "request didn't get canceled" );
352 complete: function() {
353 ok( false, "request didn't get canceled" );
356 ok( false, "request didn't get canceled" );
359 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
362 window.foobar = null;
363 window.testFoo = undefined;
365 test("jQuery.ajax - dataType html", function() {
369 var verifyEvaluation = function() {
370 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
371 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
378 url: url("data/test.html"),
379 success: function(data) {
380 jQuery("#ap").html(data);
381 ok( data.match(/^html text/), 'Check content for datatype html' );
382 setTimeout(verifyEvaluation, 600);
387 test("serialize()", function() {
390 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
391 jQuery("#search").after(
392 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
393 '<input type="number" id="html5number" name="number" value="43" />'
396 equals( jQuery('#form').serialize(),
397 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
398 'Check form serialization as query string');
400 equals( jQuery('#form :input').serialize(),
401 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3",
402 'Check input serialization as query string');
404 equals( jQuery('#testForm').serialize(),
405 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
406 'Check form serialization as query string');
408 equals( jQuery('#testForm :input').serialize(),
409 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
410 'Check input serialization as query string');
412 equals( jQuery('#form, #testForm').serialize(),
413 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&select5=3&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
414 'Multiple form serialization as query string');
416 /* Temporarily disabled. Opera 10 has problems with form serialization.
417 equals( jQuery('#form, #testForm :input').serialize(),
418 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
419 'Mixed form/input serialization as query string');
421 jQuery("#html5email, #html5number").remove();
424 test("jQuery.param()", function() {
427 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
429 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
430 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
432 params = {someName: [1, 2, 3], regularThing: "blah" };
433 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
435 params = {foo: ['a', 'b', 'c']};
436 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
438 params = {foo: ["baz", 42, "All your base are belong to us"] };
439 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
441 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
442 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
444 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
445 equals( decodeURIComponent( jQuery.param(params) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure" );
447 params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
448 equals( decodeURIComponent( jQuery.param(params) ), "a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17", "nested arrays" );
450 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
451 equals( jQuery.param(params,true), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure, forced traditional" );
453 equals( decodeURIComponent( jQuery.param({ a: [1,2,3], 'b[]': [4,5,6], 'c[d]': [7,8,9], e: { f: [10], g: [11,12], h: 13 } }) ), "a[]=1&a[]=2&a[]=3&b[]=4&b[]=5&b[]=6&c[d][]=7&c[d][]=8&c[d][]=9&e[f][]=10&e[g][]=11&e[g][]=12&e[h]=13", "Make sure params are not double-encoded." );
455 // Make sure empty arrays and objects are handled #6481
456 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
457 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
458 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
460 jQuery.ajaxSetup({ traditional: true });
462 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
463 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
465 params = {someName: [1, 2, 3], regularThing: "blah" };
466 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
468 params = {foo: ['a', 'b', 'c']};
469 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
471 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
472 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
474 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
475 equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
477 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
478 equals( jQuery.param(params), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure" );
480 params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
481 equals( jQuery.param(params), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject+Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
483 params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
484 equals( decodeURIComponent( jQuery.param(params,false) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
486 params = { param1: null };
487 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
490 test("synchronous request", function() {
492 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
495 test("synchronous request with callbacks", function() {
498 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
499 ok( /^{ "data"/.test( result ), "check returned text" );
502 test("pass-through request object", function() {
506 var target = "data/name.html";
507 var successCount = 0;
510 var success = function() {
513 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
515 errorEx += ": " + xml.status;
517 jQuery("#foo").one('ajaxStop', function () {
518 equals(successCount, 5, "Check all ajax calls successful");
519 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
520 jQuery("#foo").unbind('ajaxError');
525 ok( jQuery.get(url(target), success), "get" );
526 ok( jQuery.post(url(target), success), "post" );
527 ok( jQuery.getScript(url("data/test.js"), success), "script" );
528 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
529 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
532 test("ajax cache", function () {
539 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
540 var re = /_=(.*?)(&|$)/g;
542 for (var i = 0; i < 6; i++) {
543 var ret = re.exec(s.url);
549 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
550 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
555 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
556 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
557 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
558 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
559 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
560 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
565 * The assertions expect that the passed-in object will be modified,
566 * which shouldn't be the case. Fixes #5439.
567 test("global ajaxSettings", function() {
570 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
571 var orig = { url: "data/with_fries.xml" };
574 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
576 t = jQuery.extend({}, orig);
579 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
581 t = jQuery.extend({}, orig);
582 t.data = { zoo: 'a', ping: 'b' };
584 ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" );
586 jQuery.ajaxSettings = tmp;
590 test("load(String)", function() {
592 stop(); // check if load can be called with only url
593 jQuery('#first').load("data/name.html", start);
596 test("load('url selector')", function() {
598 stop(); // check if load can be called with only url
599 jQuery('#first').load("data/test3.html div.user", function(){
600 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
605 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
608 jQuery.ajaxSetup({ dataType: "json" });
609 jQuery("#first").ajaxComplete(function (e, xml, s) {
610 equals( s.dataType, "html", "Verify the load() dataType was html" );
611 jQuery("#first").unbind("ajaxComplete");
612 jQuery.ajaxSetup({ dataType: "" });
615 jQuery('#first').load("data/test3.html");
618 test("load(String, Function) - simple: inject text into DOM", function() {
621 jQuery('#first').load(url("data/name.html"), function() {
622 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
627 test("load(String, Function) - check scripts", function() {
631 var verifyEvaluation = function() {
632 equals( foobar, "bar", 'Check if script src was evaluated after load' );
633 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
637 jQuery('#first').load(url('data/test.html'), function() {
638 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
639 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
640 equals( testFoo, "foo", 'Check if script was evaluated after load' );
641 setTimeout(verifyEvaluation, 600);
645 test("load(String, Function) - check file with only a script tag", function() {
649 jQuery('#first').load(url('data/test2.html'), function() {
650 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
651 equals( testFoo, "foo", 'Check if script was evaluated after load' );
657 test("load(String, Object, Function)", function() {
661 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
662 var $post = jQuery(this).find('#post');
663 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
664 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
669 test("load(String, String, Function)", function() {
673 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
674 var $get = jQuery(this).find('#get');
675 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
676 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
681 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
684 jQuery.get(url('data/dashboard.xml'), function(xml) {
686 jQuery('tab', xml).each(function() {
687 content.push(jQuery(this).text());
689 equals( content[0], 'blabla', 'Check first tab');
690 equals( content[1], 'blublu', 'Check second tab');
695 test("jQuery.getScript(String, Function) - with callback", function() {
698 jQuery.getScript(url("data/test.js"), function() {
699 equals( foobar, "bar", 'Check if script was evaluated' );
700 setTimeout(start, 100);
704 test("jQuery.getScript(String, Function) - no callback", function() {
707 jQuery.getScript(url("data/test.js"), function(){
712 test("jQuery.ajax() - JSONP, Local", function() {
716 function plus(){ if ( ++count == 8 ) start(); }
721 url: "data/jsonp.php",
723 success: function(data){
724 ok( data.data, "JSON results returned (GET, no callback)" );
727 error: function(data){
728 ok( false, "Ajax error JSON (GET, no callback)" );
734 url: "data/jsonp.php?callback=?",
736 success: function(data){
737 ok( data.data, "JSON results returned (GET, url callback)" );
740 error: function(data){
741 ok( false, "Ajax error JSON (GET, url callback)" );
747 url: "data/jsonp.php",
750 success: function(data){
751 ok( data.data, "JSON results returned (GET, data callback)" );
754 error: function(data){
755 ok( false, "Ajax error JSON (GET, data callback)" );
761 url: "data/jsonp.php",
764 success: function(data){
765 ok( data.data, "JSON results returned (GET, data obj callback)" );
768 error: function(data){
769 ok( false, "Ajax error JSON (GET, data obj callback)" );
775 url: "data/jsonp.php",
777 jsonpCallback: "jsonpResults",
778 success: function(data){
779 ok( data.data, "JSON results returned (GET, custom callback name)" );
782 error: function(data){
783 ok( false, "Ajax error JSON (GET, custom callback name)" );
790 url: "data/jsonp.php",
792 success: function(data){
793 ok( data.data, "JSON results returned (POST, no callback)" );
796 error: function(data){
797 ok( false, "Ajax error JSON (GET, data obj callback)" );
804 url: "data/jsonp.php",
807 success: function(data){
808 ok( data.data, "JSON results returned (POST, data callback)" );
811 error: function(data){
812 ok( false, "Ajax error JSON (POST, data callback)" );
819 url: "data/jsonp.php",
822 success: function(data){
823 ok( data.data, "JSON results returned (POST, data obj callback)" );
826 error: function(data){
827 ok( false, "Ajax error JSON (POST, data obj callback)" );
833 test("JSONP - Custom JSONP Callback", function() {
837 window.jsonpResults = function(data) {
838 ok( data.data, "JSON results returned (GET, custom callback function)" );
843 url: "data/jsonp.php",
845 jsonpCallback: "jsonpResults"
849 test("jQuery.ajax() - JSONP, Remote", function() {
853 function plus(){ if ( ++count == 4 ) start(); }
855 var base = window.location.href.replace(/[^\/]*$/, "");
860 url: base + "data/jsonp.php",
862 success: function(data){
863 ok( data.data, "JSON results returned (GET, no callback)" );
866 error: function(data){
867 ok( false, "Ajax error JSON (GET, no callback)" );
873 url: base + "data/jsonp.php?callback=?",
875 success: function(data){
876 ok( data.data, "JSON results returned (GET, url callback)" );
879 error: function(data){
880 ok( false, "Ajax error JSON (GET, url callback)" );
886 url: base + "data/jsonp.php",
889 success: function(data){
890 ok( data.data, "JSON results returned (GET, data callback)" );
893 error: function(data){
894 ok( false, "Ajax error JSON (GET, data callback)" );
900 url: base + "data/jsonp.php",
903 success: function(data){
904 ok( data.data, "JSON results returned (GET, data obj callback)" );
907 error: function(data){
908 ok( false, "Ajax error JSON (GET, data obj callback)" );
914 test("jQuery.ajax() - script, Remote", function() {
917 var base = window.location.href.replace(/[^\/]*$/, "");
922 url: base + "data/test.js",
924 success: function(data){
925 ok( foobar, "Script results returned (GET, no callback)" );
931 test("jQuery.ajax() - script, Remote with POST", function() {
934 var base = window.location.href.replace(/[^\/]*$/, "");
939 url: base + "data/test.js",
942 success: function(data, status){
943 ok( foobar, "Script results returned (POST, no callback)" );
944 equals( status, "success", "Script results returned (POST, no callback)" );
947 error: function(xhr) {
948 ok( false, "ajax error, status code: " + xhr.status );
954 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
957 var base = window.location.href.replace(/[^\/]*$/, "");
958 base = base.replace(/^.*?\/\//, "//");
963 url: base + "data/test.js",
965 success: function(data){
966 ok( foobar, "Script results returned (GET, no callback)" );
972 test("jQuery.ajax() - malformed JSON", function() {
978 url: "data/badjson.js",
981 ok( false, "Success." );
984 error: function(xhr, msg, detailedMsg) {
985 equals( "parsererror", msg, "A parse error occurred." );
986 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
992 test("jQuery.ajax() - script by content-type", function() {
998 url: "data/script.php",
999 data: { header: "script" },
1000 success: function() {
1006 test("jQuery.ajax() - json by content-type", function() {
1012 url: "data/json.php",
1013 data: { header: "json", json: "array" },
1014 success: function( json ) {
1015 ok( json.length >= 2, "Check length");
1016 equals( json[0].name, 'John', 'Check JSON: first, name' );
1017 equals( json[0].age, 21, 'Check JSON: first, age' );
1018 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1019 equals( json[1].age, 25, 'Check JSON: second, age' );
1025 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1028 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1029 ok( json.length >= 2, "Check length");
1030 equals( json[0].name, 'John', 'Check JSON: first, name' );
1031 equals( json[0].age, 21, 'Check JSON: first, age' );
1032 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1033 equals( json[1].age, 25, 'Check JSON: second, age' );
1038 test("jQuery.getJSON(String, Function) - JSON object", function() {
1041 jQuery.getJSON(url("data/json.php"), function(json) {
1042 if (json && json.data) {
1043 equals( json.data.lang, 'en', 'Check JSON: lang' );
1044 equals( json.data.length, 25, 'Check JSON: length' );
1050 test("jQuery.getJSON - Using Native JSON", function() {
1053 var old = window.JSON;
1055 parse: function(str){
1056 ok( true, "Verifying that parse method was run" );
1062 jQuery.getJSON(url("data/json.php"), function(json) {
1064 equals( json, true, "Verifying return value" );
1069 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1072 var base = window.location.href.replace(/[^\/]*$/, "");
1075 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1076 equals( json.data.lang, 'en', 'Check JSON: lang' );
1077 equals( json.data.length, 25, 'Check JSON: length' );
1082 test("jQuery.post - data", function() {
1086 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1087 jQuery('math', xml).each(function() {
1088 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1089 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1095 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1100 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1101 jQuery('math', xml).each(function() {
1102 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1103 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1105 if ( ++done === 2 ) start();
1108 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1109 jQuery('math', xml).each(function() {
1110 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1111 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1113 if ( ++done === 2 ) start();
1117 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1122 jQuery.ajaxSetup({timeout: 1000});
1124 var pass = function() {
1126 if ( passed == 2 ) {
1127 ok( true, 'Check local and global callbacks after timeout' );
1128 jQuery('#main').unbind("ajaxError");
1133 var fail = function(a,b,c) {
1134 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1138 jQuery('#main').ajaxError(pass);
1142 url: url("data/name.php?wait=5"),
1148 jQuery.ajaxSetup({timeout: 0});
1151 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1153 jQuery.ajaxSetup({timeout: 50});
1158 url: url("data/name.php?wait=1"),
1160 ok( false, 'Check for local timeout failed' );
1163 success: function() {
1164 ok( true, 'Check for local timeout' );
1170 jQuery.ajaxSetup({timeout: 0});
1173 test("jQuery.ajax - simple get", function() {
1178 url: url("data/name.php?name=foo"),
1179 success: function(msg){
1180 equals( msg, 'bar', 'Check for GET' );
1186 test("jQuery.ajax - simple post", function() {
1191 url: url("data/name.php"),
1193 success: function(msg){
1194 equals( msg, 'pan', 'Check for POST' );
1200 test("ajaxSetup()", function() {
1204 url: url("data/name.php?name=foo"),
1205 success: function(msg){
1206 equals( msg, 'bar', 'Check for GET' );
1214 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1217 url: "data/name.php?wait=1",
1219 error: function(request, status) {
1220 ok( status != null, "status shouldn't be null in error handler" );
1221 equals( "timeout", status );
1228 test("data option: evaluate function values (#2806)", function() {
1231 url: "data/echoQuery.php",
1237 success: function(result) {
1238 equals( result, "key=value" );
1244 test("data option: empty bodies for non-GET requests", function() {
1247 url: "data/echoData.php",
1250 success: function(result) {
1251 equals( result, "" );
1257 test("jQuery.ajax - If-Modified-Since support", function() {
1262 var url = "data/if_modified_since.php?ts=" + new Date();
1267 success: function(data, status) {
1268 equals(status, "success");
1273 success: function(data, status) {
1274 if ( data === "FAIL" ) {
1275 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1276 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1278 equals(status, "notmodified");
1279 ok(data == null, "response body should be empty")
1284 // Do this because opera simply refuses to implement 304 handling :(
1285 // A feature-driven way of detecting this would be appreciated
1286 // See: http://gist.github.com/599419
1287 ok(jQuery.browser.opera, "error");
1288 ok(jQuery.browser.opera, "error");
1294 // Do this because opera simply refuses to implement 304 handling :(
1295 // A feature-driven way of detecting this would be appreciated
1296 // See: http://gist.github.com/599419
1297 ok(jQuery.browser.opera, "error");
1303 test("jQuery.ajax - Etag support", function() {
1308 var url = "data/etag.php?ts=" + new Date();
1313 success: function(data, status) {
1314 equals(status, "success");
1319 success: function(data, status) {
1320 if ( data === "FAIL" ) {
1321 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1322 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1324 equals(status, "notmodified");
1325 ok(data == null, "response body should be empty")
1330 // Do this because opera simply refuses to implement 304 handling :(
1331 // A feature-driven way of detecting this would be appreciated
1332 // See: http://gist.github.com/599419
1333 ok(jQuery.browser.opera, "error");
1334 ok(jQuery.browser.opera, "error");
1340 // Do this because opera simply refuses to implement 304 handling :(
1341 // A feature-driven way of detecting this would be appreciated
1342 // See: http://gist.github.com/599419
1343 ok(jQuery.browser.opera, "error");
1349 test("jQuery.ajax - active counter", function() {
1350 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );