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"); }
163 test("Ajax events with context", function() {
167 var context = document.createElement("div");
170 equals( this, context, e.type );
173 function callback(msg){
175 equals( this, context, "context is preserved on callback " + msg );
179 function nocallback(msg){
181 equals( typeof this.url, "string", "context is settings on callback " + msg );
185 jQuery('#foo').add(context)
192 url: url("data/name.html"),
193 beforeSend: callback("beforeSend"),
194 success: callback("success"),
195 error: callback("error"),
197 callback("complete").call(this);
200 url: url("data/404.html"),
202 beforeSend: callback("beforeSend"),
203 error: callback("error"),
204 complete: function(){
205 callback("complete").call(this);
207 jQuery('#foo').add(context).unbind();
210 url: url("data/404.html"),
211 beforeSend: nocallback("beforeSend"),
212 error: nocallback("error"),
213 complete: function(){
214 nocallback("complete").call(this);
225 test("jQuery.ajax context modification", function() {
233 url: url("data/name.html"),
235 beforeSend: function(){
238 complete: function() {
243 equals( obj.test, "foo", "Make sure the original object is maintained." );
246 test("jQuery.ajax() - disabled globals", function() {
250 jQuery('#foo').ajaxStart(function(){
251 ok( false, "ajaxStart" );
252 }).ajaxStop(function(){
253 ok( false, "ajaxStop" );
254 }).ajaxSend(function(){
255 ok( false, "ajaxSend" );
256 }).ajaxComplete(function(){
257 ok( false, "ajaxComplete" );
258 }).ajaxError(function(){
259 ok( false, "ajaxError" );
260 }).ajaxSuccess(function(){
261 ok( false, "ajaxSuccess" );
266 url: url("data/name.html"),
267 beforeSend: function(){ ok(true, "beforeSend"); },
268 success: function(){ ok(true, "success"); },
269 error: function(){ ok(false, "error"); },
270 complete: function(){
271 ok(true, "complete");
272 setTimeout(function(){ start(); }, 13);
277 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
281 url: url("data/with_fries.xml"),
283 success: function(resp) {
284 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
285 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
286 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
292 test("jQuery.ajax - beforeSend", function() {
298 jQuery.ajaxSetup({ timeout: 0 });
301 url: url("data/name.html"),
302 beforeSend: function(xml) {
305 success: function(data) {
306 ok( check, "check beforeSend was executed" );
312 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
314 var request = jQuery.ajax({
315 url: url("data/name.html"),
316 beforeSend: function() {
317 ok( true, "beforeSend got called, canceling" );
320 success: function() {
321 ok( false, "request didn't get canceled" );
323 complete: function() {
324 ok( false, "request didn't get canceled" );
327 ok( false, "request didn't get canceled" );
330 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
333 window.foobar = null;
334 window.testFoo = undefined;
336 test("jQuery.ajax - dataType html", function() {
340 var verifyEvaluation = function() {
341 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
342 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
349 url: url("data/test.html"),
350 success: function(data) {
351 jQuery("#ap").html(data);
352 ok( data.match(/^html text/), 'Check content for datatype html' );
353 setTimeout(verifyEvaluation, 600);
358 test("serialize()", function() {
361 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
362 jQuery("#search").after(
363 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
364 '<input type="number" id="html5number" name="number" value="43" />'
367 equals( jQuery('#form').serialize(),
368 "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",
369 'Check form serialization as query string');
371 equals( jQuery('#form :input').serialize(),
372 "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",
373 'Check input serialization as query string');
375 equals( jQuery('#testForm').serialize(),
376 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
377 'Check form serialization as query string');
379 equals( jQuery('#testForm :input').serialize(),
380 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
381 'Check input serialization as query string');
383 equals( jQuery('#form, #testForm').serialize(),
384 "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=",
385 'Multiple form serialization as query string');
387 /* Temporarily disabled. Opera 10 has problems with form serialization.
388 equals( jQuery('#form, #testForm :input').serialize(),
389 "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=",
390 'Mixed form/input serialization as query string');
392 jQuery("#html5email, #html5number").remove();
395 test("jQuery.param()", function() {
398 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
400 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
401 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
403 params = {someName: [1, 2, 3], regularThing: "blah" };
404 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
406 params = {foo: ['a', 'b', 'c']};
407 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
409 params = {foo: ["baz", 42, "All your base are belong to us"] };
410 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
412 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
413 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" );
415 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?" };
416 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" );
418 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 ] };
419 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" );
421 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?" };
422 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" );
424 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." );
426 // Make sure empty arrays and objects are handled #6481
427 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
428 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
429 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
431 jQuery.ajaxSetup({ traditional: true });
433 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
434 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
436 params = {someName: [1, 2, 3], regularThing: "blah" };
437 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
439 params = {foo: ['a', 'b', 'c']};
440 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
442 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
443 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
445 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
446 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" );
448 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?" };
449 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" );
451 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 ] };
452 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)" );
454 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?" };
455 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" );
457 params = { param1: null };
458 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
461 test("synchronous request", function() {
463 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
466 test("synchronous request with callbacks", function() {
469 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
470 ok( /^{ "data"/.test( result ), "check returned text" );
473 test("pass-through request object", function() {
477 var target = "data/name.html";
478 var successCount = 0;
481 var success = function() {
484 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
486 errorEx += ": " + xml.status;
488 jQuery("#foo").one('ajaxStop', function () {
489 equals(successCount, 5, "Check all ajax calls successful");
490 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
491 jQuery("#foo").unbind('ajaxError');
496 ok( jQuery.get(url(target), success), "get" );
497 ok( jQuery.post(url(target), success), "post" );
498 ok( jQuery.getScript(url("data/test.js"), success), "script" );
499 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
500 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
503 test("ajax cache", function () {
510 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
511 var re = /_=(.*?)(&|$)/g;
513 for (var i = 0; i < 6; i++) {
514 var ret = re.exec(s.url);
520 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
521 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
526 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
527 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
528 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
529 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
530 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
531 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
536 * The assertions expect that the passed-in object will be modified,
537 * which shouldn't be the case. Fixes #5439.
538 test("global ajaxSettings", function() {
541 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
542 var orig = { url: "data/with_fries.xml" };
545 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
547 t = jQuery.extend({}, orig);
550 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
552 t = jQuery.extend({}, orig);
553 t.data = { zoo: 'a', ping: 'b' };
555 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' }" );
557 jQuery.ajaxSettings = tmp;
561 test("load(String)", function() {
563 stop(); // check if load can be called with only url
564 jQuery('#first').load("data/name.html", start);
567 test("load('url selector')", function() {
569 stop(); // check if load can be called with only url
570 jQuery('#first').load("data/test3.html div.user", function(){
571 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
576 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
579 jQuery.ajaxSetup({ dataType: "json" });
580 jQuery("#first").ajaxComplete(function (e, xml, s) {
581 equals( s.dataType, "html", "Verify the load() dataType was html" );
582 jQuery("#first").unbind("ajaxComplete");
583 jQuery.ajaxSetup({ dataType: "" });
586 jQuery('#first').load("data/test3.html");
589 test("load(String, Function) - simple: inject text into DOM", function() {
592 jQuery('#first').load(url("data/name.html"), function() {
593 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
598 test("load(String, Function) - check scripts", function() {
602 var verifyEvaluation = function() {
603 equals( foobar, "bar", 'Check if script src was evaluated after load' );
604 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
608 jQuery('#first').load(url('data/test.html'), function() {
609 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
610 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
611 equals( testFoo, "foo", 'Check if script was evaluated after load' );
612 setTimeout(verifyEvaluation, 600);
616 test("load(String, Function) - check file with only a script tag", function() {
620 jQuery('#first').load(url('data/test2.html'), function() {
621 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
622 equals( testFoo, "foo", 'Check if script was evaluated after load' );
628 test("load(String, Object, Function)", function() {
632 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
633 var $post = jQuery(this).find('#post');
634 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
635 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
640 test("load(String, String, Function)", function() {
644 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
645 var $get = jQuery(this).find('#get');
646 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
647 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
652 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
655 jQuery.get(url('data/dashboard.xml'), function(xml) {
657 jQuery('tab', xml).each(function() {
658 content.push(jQuery(this).text());
660 equals( content[0], 'blabla', 'Check first tab');
661 equals( content[1], 'blublu', 'Check second tab');
666 test("jQuery.getScript(String, Function) - with callback", function() {
669 jQuery.getScript(url("data/test.js"), function() {
670 equals( foobar, "bar", 'Check if script was evaluated' );
671 setTimeout(start, 100);
675 test("jQuery.getScript(String, Function) - no callback", function() {
678 jQuery.getScript(url("data/test.js"), function(){
683 test("jQuery.ajax() - JSONP, Local", function() {
687 function plus(){ if ( ++count == 8 ) start(); }
692 url: "data/jsonp.php",
694 success: function(data){
695 ok( data.data, "JSON results returned (GET, no callback)" );
698 error: function(data){
699 ok( false, "Ajax error JSON (GET, no callback)" );
705 url: "data/jsonp.php?callback=?",
707 success: function(data){
708 ok( data.data, "JSON results returned (GET, url callback)" );
711 error: function(data){
712 ok( false, "Ajax error JSON (GET, url callback)" );
718 url: "data/jsonp.php",
721 success: function(data){
722 ok( data.data, "JSON results returned (GET, data callback)" );
725 error: function(data){
726 ok( false, "Ajax error JSON (GET, data callback)" );
732 url: "data/jsonp.php",
735 success: function(data){
736 ok( data.data, "JSON results returned (GET, data obj callback)" );
739 error: function(data){
740 ok( false, "Ajax error JSON (GET, data obj callback)" );
746 url: "data/jsonp.php",
748 jsonpCallback: "jsonpResults",
749 success: function(data){
750 ok( data.data, "JSON results returned (GET, custom callback name)" );
753 error: function(data){
754 ok( false, "Ajax error JSON (GET, custom callback name)" );
761 url: "data/jsonp.php",
763 success: function(data){
764 ok( data.data, "JSON results returned (POST, no callback)" );
767 error: function(data){
768 ok( false, "Ajax error JSON (GET, data obj callback)" );
775 url: "data/jsonp.php",
778 success: function(data){
779 ok( data.data, "JSON results returned (POST, data callback)" );
782 error: function(data){
783 ok( false, "Ajax error JSON (POST, data callback)" );
790 url: "data/jsonp.php",
793 success: function(data){
794 ok( data.data, "JSON results returned (POST, data obj callback)" );
797 error: function(data){
798 ok( false, "Ajax error JSON (POST, data obj callback)" );
804 test("JSONP - Custom JSONP Callback", function() {
808 window.jsonpResults = function(data) {
809 ok( data.data, "JSON results returned (GET, custom callback function)" );
814 url: "data/jsonp.php",
816 jsonpCallback: "jsonpResults"
820 test("jQuery.ajax() - JSONP, Remote", function() {
824 function plus(){ if ( ++count == 4 ) start(); }
826 var base = window.location.href.replace(/[^\/]*$/, "");
831 url: base + "data/jsonp.php",
833 success: function(data){
834 ok( data.data, "JSON results returned (GET, no callback)" );
837 error: function(data){
838 ok( false, "Ajax error JSON (GET, no callback)" );
844 url: base + "data/jsonp.php?callback=?",
846 success: function(data){
847 ok( data.data, "JSON results returned (GET, url callback)" );
850 error: function(data){
851 ok( false, "Ajax error JSON (GET, url callback)" );
857 url: base + "data/jsonp.php",
860 success: function(data){
861 ok( data.data, "JSON results returned (GET, data callback)" );
864 error: function(data){
865 ok( false, "Ajax error JSON (GET, data callback)" );
871 url: base + "data/jsonp.php",
874 success: function(data){
875 ok( data.data, "JSON results returned (GET, data obj callback)" );
878 error: function(data){
879 ok( false, "Ajax error JSON (GET, data obj callback)" );
885 test("jQuery.ajax() - script, Remote", function() {
888 var base = window.location.href.replace(/[^\/]*$/, "");
893 url: base + "data/test.js",
895 success: function(data){
896 ok( foobar, "Script results returned (GET, no callback)" );
902 test("jQuery.ajax() - script, Remote with POST", function() {
905 var base = window.location.href.replace(/[^\/]*$/, "");
910 url: base + "data/test.js",
913 success: function(data, status){
914 ok( foobar, "Script results returned (POST, no callback)" );
915 equals( status, "success", "Script results returned (POST, no callback)" );
918 error: function(xhr) {
919 ok( false, "ajax error, status code: " + xhr.status );
925 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
928 var base = window.location.href.replace(/[^\/]*$/, "");
929 base = base.replace(/^.*?\/\//, "//");
934 url: base + "data/test.js",
936 success: function(data){
937 ok( foobar, "Script results returned (GET, no callback)" );
943 test("jQuery.ajax() - malformed JSON", function() {
949 url: "data/badjson.js",
952 ok( false, "Success." );
955 error: function(xhr, msg, detailedMsg) {
956 equals( "parsererror", msg, "A parse error occurred." );
957 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
963 test("jQuery.ajax() - script by content-type", function() {
969 url: "data/script.php",
970 data: { header: "script" },
971 success: function() {
977 test("jQuery.ajax() - json by content-type", function() {
983 url: "data/json.php",
984 data: { header: "json", json: "array" },
985 success: function( json ) {
986 ok( json.length >= 2, "Check length");
987 equals( json[0].name, 'John', 'Check JSON: first, name' );
988 equals( json[0].age, 21, 'Check JSON: first, age' );
989 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
990 equals( json[1].age, 25, 'Check JSON: second, age' );
996 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
999 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1000 ok( json.length >= 2, "Check length");
1001 equals( json[0].name, 'John', 'Check JSON: first, name' );
1002 equals( json[0].age, 21, 'Check JSON: first, age' );
1003 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1004 equals( json[1].age, 25, 'Check JSON: second, age' );
1009 test("jQuery.getJSON(String, Function) - JSON object", function() {
1012 jQuery.getJSON(url("data/json.php"), function(json) {
1013 if (json && json.data) {
1014 equals( json.data.lang, 'en', 'Check JSON: lang' );
1015 equals( json.data.length, 25, 'Check JSON: length' );
1021 test("jQuery.getJSON - Using Native JSON", function() {
1024 var old = window.JSON;
1026 parse: function(str){
1027 ok( true, "Verifying that parse method was run" );
1033 jQuery.getJSON(url("data/json.php"), function(json) {
1035 equals( json, true, "Verifying return value" );
1040 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1043 var base = window.location.href.replace(/[^\/]*$/, "");
1046 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1047 equals( json.data.lang, 'en', 'Check JSON: lang' );
1048 equals( json.data.length, 25, 'Check JSON: length' );
1053 test("jQuery.post - data", function() {
1057 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1058 jQuery('math', xml).each(function() {
1059 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1060 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1066 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1071 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1072 jQuery('math', xml).each(function() {
1073 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1074 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1076 if ( ++done === 2 ) start();
1079 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1080 jQuery('math', xml).each(function() {
1081 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1082 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1084 if ( ++done === 2 ) start();
1088 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1093 jQuery.ajaxSetup({timeout: 1000});
1095 var pass = function() {
1097 if ( passed == 2 ) {
1098 ok( true, 'Check local and global callbacks after timeout' );
1099 jQuery('#main').unbind("ajaxError");
1104 var fail = function(a,b,c) {
1105 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1109 jQuery('#main').ajaxError(pass);
1113 url: url("data/name.php?wait=5"),
1119 jQuery.ajaxSetup({timeout: 0});
1122 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1124 jQuery.ajaxSetup({timeout: 50});
1129 url: url("data/name.php?wait=1"),
1131 ok( false, 'Check for local timeout failed' );
1134 success: function() {
1135 ok( true, 'Check for local timeout' );
1141 jQuery.ajaxSetup({timeout: 0});
1144 test("jQuery.ajax - simple get", function() {
1149 url: url("data/name.php?name=foo"),
1150 success: function(msg){
1151 equals( msg, 'bar', 'Check for GET' );
1157 test("jQuery.ajax - simple post", function() {
1162 url: url("data/name.php"),
1164 success: function(msg){
1165 equals( msg, 'pan', 'Check for POST' );
1171 test("ajaxSetup()", function() {
1175 url: url("data/name.php?name=foo"),
1176 success: function(msg){
1177 equals( msg, 'bar', 'Check for GET' );
1185 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1188 url: "data/name.php?wait=1",
1190 error: function(request, status) {
1191 ok( status != null, "status shouldn't be null in error handler" );
1192 equals( "timeout", status );
1199 test("data option: evaluate function values (#2806)", function() {
1202 url: "data/echoQuery.php",
1208 success: function(result) {
1209 equals( result, "key=value" );
1215 test("data option: empty bodies for non-GET requests", function() {
1218 url: "data/echoData.php",
1221 success: function(result) {
1222 equals( result, "" );
1228 test("jQuery.ajax - If-Modified-Since support", function() {
1233 var url = "data/if_modified_since.php?ts=" + new Date();
1238 success: function(data, status) {
1239 equals(status, "success");
1244 success: function(data, status) {
1245 if ( data === "FAIL" ) {
1246 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1247 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1249 equals(status, "notmodified");
1250 ok(data == null, "response body should be empty")
1255 // Do this because opera simply refuses to implement 304 handling :(
1256 // A feature-driven way of detecting this would be appreciated
1257 // See: http://gist.github.com/599419
1258 ok(jQuery.browser.opera, "error");
1259 ok(jQuery.browser.opera, "error");
1265 // Do this because opera simply refuses to implement 304 handling :(
1266 // A feature-driven way of detecting this would be appreciated
1267 // See: http://gist.github.com/599419
1268 ok(jQuery.browser.opera, "error");
1274 test("jQuery.ajax - Etag support", function() {
1279 var url = "data/etag.php?ts=" + new Date();
1284 success: function(data, status) {
1285 equals(status, "success");
1290 success: function(data, status) {
1291 if ( data === "FAIL" ) {
1292 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1293 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1295 equals(status, "notmodified");
1296 ok(data == null, "response body should be empty")
1301 // Do this because opera simply refuses to implement 304 handling :(
1302 // A feature-driven way of detecting this would be appreciated
1303 // See: http://gist.github.com/599419
1304 ok(jQuery.browser.opera, "error");
1305 ok(jQuery.browser.opera, "error");
1311 // Do this because opera simply refuses to implement 304 handling :(
1312 // A feature-driven way of detecting this would be appreciated
1313 // See: http://gist.github.com/599419
1314 ok(jQuery.browser.opera, "error");
1320 test("jQuery.ajax - active counter", function() {
1321 ok( jQuery.ajax.active == 0, "ajax active counter should be zero: " + jQuery.ajax.active );