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 == 9 ) 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)" );
834 url: "data/jsonp.php",
836 beforeSend: function(){
837 strictEqual( this.cache, false, "cache must be false on JSON request" );
844 test("JSONP - Custom JSONP Callback", function() {
848 window.jsonpResults = function(data) {
849 ok( data.data, "JSON results returned (GET, custom callback function)" );
854 url: "data/jsonp.php",
856 jsonpCallback: "jsonpResults"
860 test("jQuery.ajax() - JSONP, Remote", function() {
864 function plus(){ if ( ++count == 4 ) start(); }
866 var base = window.location.href.replace(/[^\/]*$/, "");
871 url: base + "data/jsonp.php",
873 success: function(data){
874 ok( data.data, "JSON results returned (GET, no callback)" );
877 error: function(data){
878 ok( false, "Ajax error JSON (GET, no callback)" );
884 url: base + "data/jsonp.php?callback=?",
886 success: function(data){
887 ok( data.data, "JSON results returned (GET, url callback)" );
890 error: function(data){
891 ok( false, "Ajax error JSON (GET, url callback)" );
897 url: base + "data/jsonp.php",
900 success: function(data){
901 ok( data.data, "JSON results returned (GET, data callback)" );
904 error: function(data){
905 ok( false, "Ajax error JSON (GET, data callback)" );
911 url: base + "data/jsonp.php",
914 success: function(data){
915 ok( data.data, "JSON results returned (GET, data obj callback)" );
918 error: function(data){
919 ok( false, "Ajax error JSON (GET, data obj callback)" );
925 test("jQuery.ajax() - script, Remote", function() {
928 var base = window.location.href.replace(/[^\/]*$/, "");
933 url: base + "data/test.js",
935 success: function(data){
936 ok( foobar, "Script results returned (GET, no callback)" );
942 test("jQuery.ajax() - script, Remote with POST", function() {
945 var base = window.location.href.replace(/[^\/]*$/, "");
950 url: base + "data/test.js",
953 success: function(data, status){
954 ok( foobar, "Script results returned (POST, no callback)" );
955 equals( status, "success", "Script results returned (POST, no callback)" );
958 error: function(xhr) {
959 ok( false, "ajax error, status code: " + xhr.status );
965 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
968 var base = window.location.href.replace(/[^\/]*$/, "");
969 base = base.replace(/^.*?\/\//, "//");
974 url: base + "data/test.js",
976 success: function(data){
977 ok( foobar, "Script results returned (GET, no callback)" );
983 test("jQuery.ajax() - malformed JSON", function() {
989 url: "data/badjson.js",
992 ok( false, "Success." );
995 error: function(xhr, msg, detailedMsg) {
996 equals( "parsererror", msg, "A parse error occurred." );
997 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1003 test("jQuery.ajax() - script by content-type", function() {
1009 url: "data/script.php",
1010 data: { header: "script" },
1011 success: function() {
1017 test("jQuery.ajax() - json by content-type", function() {
1023 url: "data/json.php",
1024 data: { header: "json", json: "array" },
1025 success: function( json ) {
1026 ok( json.length >= 2, "Check length");
1027 equals( json[0].name, 'John', 'Check JSON: first, name' );
1028 equals( json[0].age, 21, 'Check JSON: first, age' );
1029 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1030 equals( json[1].age, 25, 'Check JSON: second, age' );
1036 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1039 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1040 ok( json.length >= 2, "Check length");
1041 equals( json[0].name, 'John', 'Check JSON: first, name' );
1042 equals( json[0].age, 21, 'Check JSON: first, age' );
1043 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1044 equals( json[1].age, 25, 'Check JSON: second, age' );
1049 test("jQuery.getJSON(String, Function) - JSON object", function() {
1052 jQuery.getJSON(url("data/json.php"), function(json) {
1053 if (json && json.data) {
1054 equals( json.data.lang, 'en', 'Check JSON: lang' );
1055 equals( json.data.length, 25, 'Check JSON: length' );
1061 test("jQuery.getJSON - Using Native JSON", function() {
1064 var old = window.JSON;
1066 parse: function(str){
1067 ok( true, "Verifying that parse method was run" );
1073 jQuery.getJSON(url("data/json.php"), function(json) {
1075 equals( json, true, "Verifying return value" );
1080 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1083 var base = window.location.href.replace(/[^\/]*$/, "");
1086 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1087 equals( json.data.lang, 'en', 'Check JSON: lang' );
1088 equals( json.data.length, 25, 'Check JSON: length' );
1093 test("jQuery.post - data", function() {
1097 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1098 jQuery('math', xml).each(function() {
1099 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1100 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1106 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1111 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1112 jQuery('math', xml).each(function() {
1113 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1114 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1116 if ( ++done === 2 ) start();
1119 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1120 jQuery('math', xml).each(function() {
1121 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1122 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1124 if ( ++done === 2 ) start();
1128 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1133 jQuery.ajaxSetup({timeout: 1000});
1135 var pass = function() {
1137 if ( passed == 2 ) {
1138 ok( true, 'Check local and global callbacks after timeout' );
1139 jQuery('#main').unbind("ajaxError");
1144 var fail = function(a,b,c) {
1145 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1149 jQuery('#main').ajaxError(pass);
1153 url: url("data/name.php?wait=5"),
1159 jQuery.ajaxSetup({timeout: 0});
1162 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1164 jQuery.ajaxSetup({timeout: 50});
1169 url: url("data/name.php?wait=1"),
1171 ok( false, 'Check for local timeout failed' );
1174 success: function() {
1175 ok( true, 'Check for local timeout' );
1181 jQuery.ajaxSetup({timeout: 0});
1184 test("jQuery.ajax - simple get", function() {
1189 url: url("data/name.php?name=foo"),
1190 success: function(msg){
1191 equals( msg, 'bar', 'Check for GET' );
1197 test("jQuery.ajax - simple post", function() {
1202 url: url("data/name.php"),
1204 success: function(msg){
1205 equals( msg, 'pan', 'Check for POST' );
1211 test("ajaxSetup()", function() {
1215 url: url("data/name.php?name=foo"),
1216 success: function(msg){
1217 equals( msg, 'bar', 'Check for GET' );
1225 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1228 url: "data/name.php?wait=1",
1230 error: function(request, status) {
1231 ok( status != null, "status shouldn't be null in error handler" );
1232 equals( "timeout", status );
1239 test("data option: evaluate function values (#2806)", function() {
1242 url: "data/echoQuery.php",
1248 success: function(result) {
1249 equals( result, "key=value" );
1255 test("data option: empty bodies for non-GET requests", function() {
1258 url: "data/echoData.php",
1261 success: function(result) {
1262 equals( result, "" );
1268 test("jQuery.ajax - If-Modified-Since support", function() {
1273 var url = "data/if_modified_since.php?ts=" + new Date();
1278 success: function(data, status) {
1279 equals(status, "success");
1284 success: function(data, status) {
1285 if ( data === "FAIL" ) {
1286 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1287 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1289 equals(status, "notmodified");
1290 ok(data == null, "response body should be empty")
1295 // Do this because opera simply refuses to implement 304 handling :(
1296 // A feature-driven way of detecting this would be appreciated
1297 // See: http://gist.github.com/599419
1298 ok(jQuery.browser.opera, "error");
1299 ok(jQuery.browser.opera, "error");
1305 // Do this because opera simply refuses to implement 304 handling :(
1306 // A feature-driven way of detecting this would be appreciated
1307 // See: http://gist.github.com/599419
1308 ok(jQuery.browser.opera, "error");
1314 test("jQuery.ajax - Etag support", function() {
1319 var url = "data/etag.php?ts=" + new Date();
1324 success: function(data, status) {
1325 equals(status, "success");
1330 success: function(data, status) {
1331 if ( data === "FAIL" ) {
1332 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1333 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1335 equals(status, "notmodified");
1336 ok(data == null, "response body should be empty")
1341 // Do this because opera simply refuses to implement 304 handling :(
1342 // A feature-driven way of detecting this would be appreciated
1343 // See: http://gist.github.com/599419
1344 ok(jQuery.browser.opera, "error");
1345 ok(jQuery.browser.opera, "error");
1351 // Do this because opera simply refuses to implement 304 handling :(
1352 // A feature-driven way of detecting this would be appreciated
1353 // See: http://gist.github.com/599419
1354 ok(jQuery.browser.opera, "error");
1360 test("jQuery.ajax - active counter", function() {
1361 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );
1364 test( "jQuery.ajax - Location object as url (#7531)", 1, function () {
1365 var success = false;
1367 var xhr = jQuery.ajax({ url: document.location });
1372 ok( success, "document.location did not generate exception" );