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 error: function(){ ok(false, "304 not ok "); },
108 complete: function(xhr){ start(); }
112 test(".load()) - 404 error callbacks", function() {
116 jQuery('#foo').ajaxStart(function(){
117 ok( true, "ajaxStart" );
118 }).ajaxStop(function(){
119 ok( true, "ajaxStop" );
121 }).ajaxSend(function(){
122 ok( true, "ajaxSend" );
123 }).ajaxComplete(function(){
124 ok( true, "ajaxComplete" );
125 }).ajaxError(function(){
126 ok( true, "ajaxError" );
127 }).ajaxSuccess(function(){
128 ok( false, "ajaxSuccess" );
131 jQuery("<div/>").load("data/404.html", function(){
132 ok(true, "complete");
136 test("jQuery.ajax() - abort", function() {
140 jQuery('#foo').ajaxStart(function(){
141 ok( true, "ajaxStart" );
142 }).ajaxStop(function(){
143 ok( true, "ajaxStop" );
145 }).ajaxSend(function(){
146 ok( true, "ajaxSend" );
147 }).ajaxComplete(function(){
148 ok( true, "ajaxComplete" );
151 var xhr = jQuery.ajax({
152 url: url("data/name.php?wait=5"),
153 beforeSend: function(){ ok(true, "beforeSend"); },
154 complete: function(){ ok(true, "complete"); }
160 test("Ajax events with context", function() {
164 var context = document.createElement("div");
167 equals( this, context, e.type );
170 function callback(msg){
172 equals( this, context, "context is preserved on callback " + msg );
176 function nocallback(msg){
178 equals( typeof this.url, "string", "context is settings on callback " + msg );
182 jQuery('#foo').add(context)
189 url: url("data/name.html"),
190 beforeSend: callback("beforeSend"),
191 success: callback("success"),
192 error: callback("error"),
194 callback("complete").call(this);
197 url: url("data/404.html"),
199 beforeSend: callback("beforeSend"),
200 error: callback("error"),
201 complete: function(){
202 callback("complete").call(this);
204 jQuery('#foo').add(context).unbind();
207 url: url("data/404.html"),
208 beforeSend: nocallback("beforeSend"),
209 error: nocallback("error"),
210 complete: function(){
211 nocallback("complete").call(this);
222 test("jQuery.ajax context modification", function() {
230 url: url("data/name.html"),
232 beforeSend: function(){
235 complete: function() {
240 equals( obj.test, "foo", "Make sure the original object is maintained." );
243 test("jQuery.ajax() - disabled globals", function() {
247 jQuery('#foo').ajaxStart(function(){
248 ok( false, "ajaxStart" );
249 }).ajaxStop(function(){
250 ok( false, "ajaxStop" );
251 }).ajaxSend(function(){
252 ok( false, "ajaxSend" );
253 }).ajaxComplete(function(){
254 ok( false, "ajaxComplete" );
255 }).ajaxError(function(){
256 ok( false, "ajaxError" );
257 }).ajaxSuccess(function(){
258 ok( false, "ajaxSuccess" );
263 url: url("data/name.html"),
264 beforeSend: function(){ ok(true, "beforeSend"); },
265 success: function(){ ok(true, "success"); },
266 error: function(){ ok(false, "error"); },
267 complete: function(){
268 ok(true, "complete");
269 setTimeout(function(){ start(); }, 13);
274 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
278 url: url("data/with_fries.xml"),
280 success: function(resp) {
281 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
282 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
283 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
289 test("jQuery.ajax - beforeSend", function() {
295 jQuery.ajaxSetup({ timeout: 0 });
298 url: url("data/name.html"),
299 beforeSend: function(xml) {
302 success: function(data) {
303 ok( check, "check beforeSend was executed" );
309 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
311 var request = jQuery.ajax({
312 url: url("data/name.html"),
313 beforeSend: function() {
314 ok( true, "beforeSend got called, canceling" );
317 success: function() {
318 ok( false, "request didn't get canceled" );
320 complete: function() {
321 ok( false, "request didn't get canceled" );
324 ok( false, "request didn't get canceled" );
327 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
330 window.foobar = null;
331 window.testFoo = undefined;
333 test("jQuery.ajax - dataType html", function() {
337 var verifyEvaluation = function() {
338 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
339 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
346 url: url("data/test.html"),
347 success: function(data) {
348 jQuery("#ap").html(data);
349 ok( data.match(/^html text/), 'Check content for datatype html' );
350 setTimeout(verifyEvaluation, 600);
355 test("serialize()", function() {
358 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
359 jQuery("#search").after(
360 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
361 '<input type="number" id="html5number" name="number" value="43" />'
364 equals( jQuery('#form').serialize(),
365 "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",
366 'Check form serialization as query string');
368 equals( jQuery('#form :input').serialize(),
369 "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",
370 'Check input serialization as query string');
372 equals( jQuery('#testForm').serialize(),
373 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
374 'Check form serialization as query string');
376 equals( jQuery('#testForm :input').serialize(),
377 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
378 'Check input serialization as query string');
380 equals( jQuery('#form, #testForm').serialize(),
381 "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=",
382 'Multiple form serialization as query string');
384 /* Temporarily disabled. Opera 10 has problems with form serialization.
385 equals( jQuery('#form, #testForm :input').serialize(),
386 "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=",
387 'Mixed form/input serialization as query string');
389 jQuery("#html5email, #html5number").remove();
392 test("jQuery.param()", function() {
395 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
397 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
398 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
400 params = {someName: [1, 2, 3], regularThing: "blah" };
401 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
403 params = {foo: ['a', 'b', 'c']};
404 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
406 params = {foo: ["baz", 42, "All your base are belong to us"] };
407 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
409 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
410 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" );
412 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?" };
413 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" );
415 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 ] };
416 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" );
418 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?" };
419 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" );
421 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." );
423 jQuery.ajaxSetup({ traditional: true });
425 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
426 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
428 params = {someName: [1, 2, 3], regularThing: "blah" };
429 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
431 params = {foo: ['a', 'b', 'c']};
432 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
434 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
435 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
437 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
438 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" );
440 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?" };
441 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" );
443 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 ] };
444 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)" );
446 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?" };
447 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" );
449 params = { param1: null };
450 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
453 test("synchronous request", function() {
455 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
458 test("synchronous request with callbacks", function() {
461 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
462 ok( /^{ "data"/.test( result ), "check returned text" );
465 test("pass-through request object", function() {
469 var target = "data/name.html";
470 var successCount = 0;
473 var success = function() {
476 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
478 errorEx += ": " + xml.status;
480 jQuery("#foo").one('ajaxStop', function () {
481 equals(successCount, 5, "Check all ajax calls successful");
482 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
483 jQuery("#foo").unbind('ajaxError');
488 ok( jQuery.get(url(target), success), "get" );
489 ok( jQuery.post(url(target), success), "post" );
490 ok( jQuery.getScript(url("data/test.js"), success), "script" );
491 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
492 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
495 test("ajax cache", function () {
502 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
503 var re = /_=(.*?)(&|$)/g;
505 for (var i = 0; i < 6; i++) {
506 var ret = re.exec(s.url);
512 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
513 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
518 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
519 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
520 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
521 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
522 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
523 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
528 * The assertions expect that the passed-in object will be modified,
529 * which shouldn't be the case. Fixes #5439.
530 test("global ajaxSettings", function() {
533 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
534 var orig = { url: "data/with_fries.xml" };
537 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
539 t = jQuery.extend({}, orig);
542 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
544 t = jQuery.extend({}, orig);
545 t.data = { zoo: 'a', ping: 'b' };
547 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' }" );
549 jQuery.ajaxSettings = tmp;
553 test("load(String)", function() {
555 stop(); // check if load can be called with only url
556 jQuery('#first').load("data/name.html", start);
559 test("load('url selector')", function() {
561 stop(); // check if load can be called with only url
562 jQuery('#first').load("data/test3.html div.user", function(){
563 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
568 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
571 jQuery.ajaxSetup({ dataType: "json" });
572 jQuery("#first").ajaxComplete(function (e, xml, s) {
573 equals( s.dataType, "html", "Verify the load() dataType was html" );
574 jQuery("#first").unbind("ajaxComplete");
575 jQuery.ajaxSetup({ dataType: "" });
578 jQuery('#first').load("data/test3.html");
581 test("load(String, Function) - simple: inject text into DOM", function() {
584 jQuery('#first').load(url("data/name.html"), function() {
585 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
590 test("load(String, Function) - check scripts", function() {
594 var verifyEvaluation = function() {
595 equals( foobar, "bar", 'Check if script src was evaluated after load' );
596 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
600 jQuery('#first').load(url('data/test.html'), function() {
601 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
602 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
603 equals( testFoo, "foo", 'Check if script was evaluated after load' );
604 setTimeout(verifyEvaluation, 600);
608 test("load(String, Function) - check file with only a script tag", function() {
612 jQuery('#first').load(url('data/test2.html'), function() {
613 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
614 equals( testFoo, "foo", 'Check if script was evaluated after load' );
620 test("load(String, Object, Function)", function() {
624 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
625 var $post = jQuery(this).find('#post');
626 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
627 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
632 test("load(String, String, Function)", function() {
636 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
637 var $get = jQuery(this).find('#get');
638 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
639 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
644 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
647 jQuery.get(url('data/dashboard.xml'), function(xml) {
649 jQuery('tab', xml).each(function() {
650 content.push(jQuery(this).text());
652 equals( content[0], 'blabla', 'Check first tab');
653 equals( content[1], 'blublu', 'Check second tab');
658 test("jQuery.getScript(String, Function) - with callback", function() {
661 jQuery.getScript(url("data/test.js"), function() {
662 equals( foobar, "bar", 'Check if script was evaluated' );
663 setTimeout(start, 100);
667 test("jQuery.getScript(String, Function) - no callback", function() {
670 jQuery.getScript(url("data/test.js"), function(){
675 test("jQuery.ajax() - JSONP, Local", function() {
679 function plus(){ if ( ++count == 8 ) start(); }
684 url: "data/jsonp.php",
686 success: function(data){
687 ok( data.data, "JSON results returned (GET, no callback)" );
690 error: function(data){
691 ok( false, "Ajax error JSON (GET, no callback)" );
697 url: "data/jsonp.php?callback=?",
699 success: function(data){
700 ok( data.data, "JSON results returned (GET, url callback)" );
703 error: function(data){
704 ok( false, "Ajax error JSON (GET, url callback)" );
710 url: "data/jsonp.php",
713 success: function(data){
714 ok( data.data, "JSON results returned (GET, data callback)" );
717 error: function(data){
718 ok( false, "Ajax error JSON (GET, data callback)" );
724 url: "data/jsonp.php",
727 success: function(data){
728 ok( data.data, "JSON results returned (GET, data obj callback)" );
731 error: function(data){
732 ok( false, "Ajax error JSON (GET, data obj callback)" );
738 url: "data/jsonp.php",
740 jsonpCallback: "jsonpResults",
741 success: function(data){
742 ok( data.data, "JSON results returned (GET, custom callback name)" );
745 error: function(data){
746 ok( false, "Ajax error JSON (GET, custom callback name)" );
753 url: "data/jsonp.php",
755 success: function(data){
756 ok( data.data, "JSON results returned (POST, no callback)" );
759 error: function(data){
760 ok( false, "Ajax error JSON (GET, data obj callback)" );
767 url: "data/jsonp.php",
770 success: function(data){
771 ok( data.data, "JSON results returned (POST, data callback)" );
774 error: function(data){
775 ok( false, "Ajax error JSON (POST, data callback)" );
782 url: "data/jsonp.php",
785 success: function(data){
786 ok( data.data, "JSON results returned (POST, data obj callback)" );
789 error: function(data){
790 ok( false, "Ajax error JSON (POST, data obj callback)" );
796 test("JSONP - Custom JSONP Callback", function() {
800 window.jsonpResults = function(data) {
801 ok( data.data, "JSON results returned (GET, custom callback function)" );
806 url: "data/jsonp.php",
808 jsonpCallback: "jsonpResults"
812 test("jQuery.ajax() - JSONP, Remote", function() {
816 function plus(){ if ( ++count == 4 ) start(); }
818 var base = window.location.href.replace(/[^\/]*$/, "");
823 url: base + "data/jsonp.php",
825 success: function(data){
826 ok( data.data, "JSON results returned (GET, no callback)" );
829 error: function(data){
830 ok( false, "Ajax error JSON (GET, no callback)" );
836 url: base + "data/jsonp.php?callback=?",
838 success: function(data){
839 ok( data.data, "JSON results returned (GET, url callback)" );
842 error: function(data){
843 ok( false, "Ajax error JSON (GET, url callback)" );
849 url: base + "data/jsonp.php",
852 success: function(data){
853 ok( data.data, "JSON results returned (GET, data callback)" );
856 error: function(data){
857 ok( false, "Ajax error JSON (GET, data callback)" );
863 url: base + "data/jsonp.php",
866 success: function(data){
867 ok( data.data, "JSON results returned (GET, data obj callback)" );
870 error: function(data){
871 ok( false, "Ajax error JSON (GET, data obj callback)" );
877 test("jQuery.ajax() - script, Remote", function() {
880 var base = window.location.href.replace(/[^\/]*$/, "");
885 url: base + "data/test.js",
887 success: function(data){
888 ok( foobar, "Script results returned (GET, no callback)" );
894 test("jQuery.ajax() - script, Remote with POST", function() {
897 var base = window.location.href.replace(/[^\/]*$/, "");
902 url: base + "data/test.js",
905 success: function(data, status){
906 ok( foobar, "Script results returned (POST, no callback)" );
907 equals( status, "success", "Script results returned (POST, no callback)" );
910 error: function(xhr) {
911 ok( false, "ajax error, status code: " + xhr.status );
917 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
920 var base = window.location.href.replace(/[^\/]*$/, "");
921 base = base.replace(/^.*?\/\//, "//");
926 url: base + "data/test.js",
928 success: function(data){
929 ok( foobar, "Script results returned (GET, no callback)" );
935 test("jQuery.ajax() - malformed JSON", function() {
941 url: "data/badjson.js",
944 ok( false, "Success." );
947 error: function(xhr, msg, detailedMsg) {
948 equals( "parsererror", msg, "A parse error occurred." );
949 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
955 test("jQuery.ajax() - script by content-type", function() {
961 url: "data/script.php",
962 data: { header: "script" },
963 success: function() {
969 test("jQuery.ajax() - json by content-type", function() {
975 url: "data/json.php",
976 data: { header: "json", json: "array" },
977 success: function( json ) {
978 ok( json.length >= 2, "Check length");
979 equals( json[0].name, 'John', 'Check JSON: first, name' );
980 equals( json[0].age, 21, 'Check JSON: first, age' );
981 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
982 equals( json[1].age, 25, 'Check JSON: second, age' );
988 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
991 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
992 ok( json.length >= 2, "Check length");
993 equals( json[0].name, 'John', 'Check JSON: first, name' );
994 equals( json[0].age, 21, 'Check JSON: first, age' );
995 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
996 equals( json[1].age, 25, 'Check JSON: second, age' );
1001 test("jQuery.getJSON(String, Function) - JSON object", function() {
1004 jQuery.getJSON(url("data/json.php"), function(json) {
1005 if (json && json.data) {
1006 equals( json.data.lang, 'en', 'Check JSON: lang' );
1007 equals( json.data.length, 25, 'Check JSON: length' );
1013 test("jQuery.getJSON - Using Native JSON", function() {
1016 var old = window.JSON;
1018 parse: function(str){
1019 ok( true, "Verifying that parse method was run" );
1025 jQuery.getJSON(url("data/json.php"), function(json) {
1027 equals( json, true, "Verifying return value" );
1032 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1035 var base = window.location.href.replace(/[^\/]*$/, "");
1038 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1039 equals( json.data.lang, 'en', 'Check JSON: lang' );
1040 equals( json.data.length, 25, 'Check JSON: length' );
1045 test("jQuery.post - data", function() {
1049 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1050 jQuery('math', xml).each(function() {
1051 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1052 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1058 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1063 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1064 jQuery('math', xml).each(function() {
1065 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1066 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1068 if ( ++done === 2 ) start();
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();
1080 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1085 jQuery.ajaxSetup({timeout: 1000});
1087 var pass = function() {
1089 if ( passed == 2 ) {
1090 ok( true, 'Check local and global callbacks after timeout' );
1091 jQuery('#main').unbind("ajaxError");
1096 var fail = function(a,b,c) {
1097 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1101 jQuery('#main').ajaxError(pass);
1105 url: url("data/name.php?wait=5"),
1111 jQuery.ajaxSetup({timeout: 0});
1114 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1116 jQuery.ajaxSetup({timeout: 50});
1121 url: url("data/name.php?wait=1"),
1123 ok( false, 'Check for local timeout failed' );
1126 success: function() {
1127 ok( true, 'Check for local timeout' );
1133 jQuery.ajaxSetup({timeout: 0});
1136 test("jQuery.ajax - simple get", function() {
1141 url: url("data/name.php?name=foo"),
1142 success: function(msg){
1143 equals( msg, 'bar', 'Check for GET' );
1149 test("jQuery.ajax - simple post", function() {
1154 url: url("data/name.php"),
1156 success: function(msg){
1157 equals( msg, 'pan', 'Check for POST' );
1163 test("ajaxSetup()", function() {
1167 url: url("data/name.php?name=foo"),
1168 success: function(msg){
1169 equals( msg, 'bar', 'Check for GET' );
1177 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1180 url: "data/name.php?wait=1",
1182 error: function(request, status) {
1183 ok( status != null, "status shouldn't be null in error handler" );
1184 equals( "timeout", status );
1191 test("data option: evaluate function values (#2806)", function() {
1194 url: "data/echoQuery.php",
1200 success: function(result) {
1201 equals( result, "key=value" );
1207 test("data option: empty bodies for non-GET requests", function() {
1210 url: "data/echoData.php",
1213 success: function(result) {
1214 equals( result, "" );
1220 test("jQuery.ajax - If-Modified-Since support", function() {
1225 var url = "data/if_modified_since.php?ts=" + new Date();
1230 success: function(data, status) {
1231 equals(status, "success");
1236 success: function(data, status) {
1237 if ( data === "FAIL" ) {
1238 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1239 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1241 equals(status, "notmodified");
1242 ok(data == null, "response body should be empty")
1251 test("jQuery.ajax - Etag support", function() {
1256 var url = "data/etag.php?ts=" + new Date();
1261 success: function(data, status) {
1262 equals(status, "success");
1267 success: function(data, status) {
1268 if ( data === "FAIL" ) {
1269 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1270 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1272 equals(status, "notmodified");
1273 ok(data == null, "response body should be empty")
1283 test("jQuery.ajax - active counter", function() {
1284 ok( jQuery.ajax.active == 0, "ajax active counter should be zero: " + jQuery.ajax.active );