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&select5=3",
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&select5=3",
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&select5=3&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 // Make sure empty arrays and objects are handled #6481
424 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
425 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
426 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
428 jQuery.ajaxSetup({ traditional: true });
430 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
431 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
433 params = {someName: [1, 2, 3], regularThing: "blah" };
434 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
436 params = {foo: ['a', 'b', 'c']};
437 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
439 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
440 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
442 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
443 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" );
445 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?" };
446 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" );
448 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 ] };
449 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)" );
451 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?" };
452 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" );
454 params = { param1: null };
455 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
458 test("synchronous request", function() {
460 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
463 test("synchronous request with callbacks", function() {
466 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
467 ok( /^{ "data"/.test( result ), "check returned text" );
470 test("pass-through request object", function() {
474 var target = "data/name.html";
475 var successCount = 0;
478 var success = function() {
481 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
483 errorEx += ": " + xml.status;
485 jQuery("#foo").one('ajaxStop', function () {
486 equals(successCount, 5, "Check all ajax calls successful");
487 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
488 jQuery("#foo").unbind('ajaxError');
493 ok( jQuery.get(url(target), success), "get" );
494 ok( jQuery.post(url(target), success), "post" );
495 ok( jQuery.getScript(url("data/test.js"), success), "script" );
496 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
497 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
500 test("ajax cache", function () {
507 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
508 var re = /_=(.*?)(&|$)/g;
510 for (var i = 0; i < 6; i++) {
511 var ret = re.exec(s.url);
517 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
518 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
523 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
524 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
525 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
526 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
527 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
528 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
533 * The assertions expect that the passed-in object will be modified,
534 * which shouldn't be the case. Fixes #5439.
535 test("global ajaxSettings", function() {
538 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
539 var orig = { url: "data/with_fries.xml" };
542 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
544 t = jQuery.extend({}, orig);
547 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
549 t = jQuery.extend({}, orig);
550 t.data = { zoo: 'a', ping: 'b' };
552 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' }" );
554 jQuery.ajaxSettings = tmp;
558 test("load(String)", function() {
560 stop(); // check if load can be called with only url
561 jQuery('#first').load("data/name.html", start);
564 test("load('url selector')", function() {
566 stop(); // check if load can be called with only url
567 jQuery('#first').load("data/test3.html div.user", function(){
568 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
573 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
576 jQuery.ajaxSetup({ dataType: "json" });
577 jQuery("#first").ajaxComplete(function (e, xml, s) {
578 equals( s.dataType, "html", "Verify the load() dataType was html" );
579 jQuery("#first").unbind("ajaxComplete");
580 jQuery.ajaxSetup({ dataType: "" });
583 jQuery('#first').load("data/test3.html");
586 test("load(String, Function) - simple: inject text into DOM", function() {
589 jQuery('#first').load(url("data/name.html"), function() {
590 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
595 test("load(String, Function) - check scripts", function() {
599 var verifyEvaluation = function() {
600 equals( foobar, "bar", 'Check if script src was evaluated after load' );
601 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
605 jQuery('#first').load(url('data/test.html'), function() {
606 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
607 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
608 equals( testFoo, "foo", 'Check if script was evaluated after load' );
609 setTimeout(verifyEvaluation, 600);
613 test("load(String, Function) - check file with only a script tag", function() {
617 jQuery('#first').load(url('data/test2.html'), function() {
618 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
619 equals( testFoo, "foo", 'Check if script was evaluated after load' );
625 test("load(String, Object, Function)", function() {
629 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
630 var $post = jQuery(this).find('#post');
631 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
632 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
637 test("load(String, String, Function)", function() {
641 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
642 var $get = jQuery(this).find('#get');
643 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
644 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
649 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
652 jQuery.get(url('data/dashboard.xml'), function(xml) {
654 jQuery('tab', xml).each(function() {
655 content.push(jQuery(this).text());
657 equals( content[0], 'blabla', 'Check first tab');
658 equals( content[1], 'blublu', 'Check second tab');
663 test("jQuery.getScript(String, Function) - with callback", function() {
666 jQuery.getScript(url("data/test.js"), function() {
667 equals( foobar, "bar", 'Check if script was evaluated' );
668 setTimeout(start, 100);
672 test("jQuery.getScript(String, Function) - no callback", function() {
675 jQuery.getScript(url("data/test.js"), function(){
680 test("jQuery.ajax() - JSONP, Local", function() {
684 function plus(){ if ( ++count == 8 ) start(); }
689 url: "data/jsonp.php",
691 success: function(data){
692 ok( data.data, "JSON results returned (GET, no callback)" );
695 error: function(data){
696 ok( false, "Ajax error JSON (GET, no callback)" );
702 url: "data/jsonp.php?callback=?",
704 success: function(data){
705 ok( data.data, "JSON results returned (GET, url callback)" );
708 error: function(data){
709 ok( false, "Ajax error JSON (GET, url callback)" );
715 url: "data/jsonp.php",
718 success: function(data){
719 ok( data.data, "JSON results returned (GET, data callback)" );
722 error: function(data){
723 ok( false, "Ajax error JSON (GET, data callback)" );
729 url: "data/jsonp.php",
732 success: function(data){
733 ok( data.data, "JSON results returned (GET, data obj callback)" );
736 error: function(data){
737 ok( false, "Ajax error JSON (GET, data obj callback)" );
743 url: "data/jsonp.php",
745 jsonpCallback: "jsonpResults",
746 success: function(data){
747 ok( data.data, "JSON results returned (GET, custom callback name)" );
750 error: function(data){
751 ok( false, "Ajax error JSON (GET, custom callback name)" );
758 url: "data/jsonp.php",
760 success: function(data){
761 ok( data.data, "JSON results returned (POST, no callback)" );
764 error: function(data){
765 ok( false, "Ajax error JSON (GET, data obj callback)" );
772 url: "data/jsonp.php",
775 success: function(data){
776 ok( data.data, "JSON results returned (POST, data callback)" );
779 error: function(data){
780 ok( false, "Ajax error JSON (POST, data callback)" );
787 url: "data/jsonp.php",
790 success: function(data){
791 ok( data.data, "JSON results returned (POST, data obj callback)" );
794 error: function(data){
795 ok( false, "Ajax error JSON (POST, data obj callback)" );
801 test("JSONP - Custom JSONP Callback", function() {
805 window.jsonpResults = function(data) {
806 ok( data.data, "JSON results returned (GET, custom callback function)" );
811 url: "data/jsonp.php",
813 jsonpCallback: "jsonpResults"
817 test("jQuery.ajax() - JSONP, Remote", function() {
821 function plus(){ if ( ++count == 4 ) start(); }
823 var base = window.location.href.replace(/[^\/]*$/, "");
828 url: base + "data/jsonp.php",
830 success: function(data){
831 ok( data.data, "JSON results returned (GET, no callback)" );
834 error: function(data){
835 ok( false, "Ajax error JSON (GET, no callback)" );
841 url: base + "data/jsonp.php?callback=?",
843 success: function(data){
844 ok( data.data, "JSON results returned (GET, url callback)" );
847 error: function(data){
848 ok( false, "Ajax error JSON (GET, url callback)" );
854 url: base + "data/jsonp.php",
857 success: function(data){
858 ok( data.data, "JSON results returned (GET, data callback)" );
861 error: function(data){
862 ok( false, "Ajax error JSON (GET, data callback)" );
868 url: base + "data/jsonp.php",
871 success: function(data){
872 ok( data.data, "JSON results returned (GET, data obj callback)" );
875 error: function(data){
876 ok( false, "Ajax error JSON (GET, data obj callback)" );
882 test("jQuery.ajax() - script, Remote", function() {
885 var base = window.location.href.replace(/[^\/]*$/, "");
890 url: base + "data/test.js",
892 success: function(data){
893 ok( foobar, "Script results returned (GET, no callback)" );
899 test("jQuery.ajax() - script, Remote with POST", function() {
902 var base = window.location.href.replace(/[^\/]*$/, "");
907 url: base + "data/test.js",
910 success: function(data, status){
911 ok( foobar, "Script results returned (POST, no callback)" );
912 equals( status, "success", "Script results returned (POST, no callback)" );
915 error: function(xhr) {
916 ok( false, "ajax error, status code: " + xhr.status );
922 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
925 var base = window.location.href.replace(/[^\/]*$/, "");
926 base = base.replace(/^.*?\/\//, "//");
931 url: base + "data/test.js",
933 success: function(data){
934 ok( foobar, "Script results returned (GET, no callback)" );
940 test("jQuery.ajax() - malformed JSON", function() {
946 url: "data/badjson.js",
949 ok( false, "Success." );
952 error: function(xhr, msg, detailedMsg) {
953 equals( "parsererror", msg, "A parse error occurred." );
954 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
960 test("jQuery.ajax() - script by content-type", function() {
966 url: "data/script.php",
967 data: { header: "script" },
968 success: function() {
974 test("jQuery.ajax() - json by content-type", function() {
980 url: "data/json.php",
981 data: { header: "json", json: "array" },
982 success: function( json ) {
983 ok( json.length >= 2, "Check length");
984 equals( json[0].name, 'John', 'Check JSON: first, name' );
985 equals( json[0].age, 21, 'Check JSON: first, age' );
986 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
987 equals( json[1].age, 25, 'Check JSON: second, age' );
993 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
996 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
997 ok( json.length >= 2, "Check length");
998 equals( json[0].name, 'John', 'Check JSON: first, name' );
999 equals( json[0].age, 21, 'Check JSON: first, age' );
1000 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1001 equals( json[1].age, 25, 'Check JSON: second, age' );
1006 test("jQuery.getJSON(String, Function) - JSON object", function() {
1009 jQuery.getJSON(url("data/json.php"), function(json) {
1010 if (json && json.data) {
1011 equals( json.data.lang, 'en', 'Check JSON: lang' );
1012 equals( json.data.length, 25, 'Check JSON: length' );
1018 test("jQuery.getJSON - Using Native JSON", function() {
1021 var old = window.JSON;
1023 parse: function(str){
1024 ok( true, "Verifying that parse method was run" );
1030 jQuery.getJSON(url("data/json.php"), function(json) {
1032 equals( json, true, "Verifying return value" );
1037 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1040 var base = window.location.href.replace(/[^\/]*$/, "");
1043 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1044 equals( json.data.lang, 'en', 'Check JSON: lang' );
1045 equals( json.data.length, 25, 'Check JSON: length' );
1050 test("jQuery.post - data", function() {
1054 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1055 jQuery('math', xml).each(function() {
1056 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1057 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1063 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1068 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1069 jQuery('math', xml).each(function() {
1070 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1071 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1073 if ( ++done === 2 ) start();
1076 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1077 jQuery('math', xml).each(function() {
1078 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1079 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1081 if ( ++done === 2 ) start();
1085 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1090 jQuery.ajaxSetup({timeout: 1000});
1092 var pass = function() {
1094 if ( passed == 2 ) {
1095 ok( true, 'Check local and global callbacks after timeout' );
1096 jQuery('#main').unbind("ajaxError");
1101 var fail = function(a,b,c) {
1102 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1106 jQuery('#main').ajaxError(pass);
1110 url: url("data/name.php?wait=5"),
1116 jQuery.ajaxSetup({timeout: 0});
1119 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1121 jQuery.ajaxSetup({timeout: 50});
1126 url: url("data/name.php?wait=1"),
1128 ok( false, 'Check for local timeout failed' );
1131 success: function() {
1132 ok( true, 'Check for local timeout' );
1138 jQuery.ajaxSetup({timeout: 0});
1141 test("jQuery.ajax - simple get", function() {
1146 url: url("data/name.php?name=foo"),
1147 success: function(msg){
1148 equals( msg, 'bar', 'Check for GET' );
1154 test("jQuery.ajax - simple post", function() {
1159 url: url("data/name.php"),
1161 success: function(msg){
1162 equals( msg, 'pan', 'Check for POST' );
1168 test("ajaxSetup()", function() {
1172 url: url("data/name.php?name=foo"),
1173 success: function(msg){
1174 equals( msg, 'bar', 'Check for GET' );
1182 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1185 url: "data/name.php?wait=1",
1187 error: function(request, status) {
1188 ok( status != null, "status shouldn't be null in error handler" );
1189 equals( "timeout", status );
1196 test("data option: evaluate function values (#2806)", function() {
1199 url: "data/echoQuery.php",
1205 success: function(result) {
1206 equals( result, "key=value" );
1212 test("data option: empty bodies for non-GET requests", function() {
1215 url: "data/echoData.php",
1218 success: function(result) {
1219 equals( result, "" );
1225 test("jQuery.ajax - If-Modified-Since support", function() {
1230 var url = "data/if_modified_since.php?ts=" + new Date();
1235 success: function(data, status) {
1236 equals(status, "success");
1241 success: function(data, status) {
1242 if ( data === "FAIL" ) {
1243 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1244 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1246 equals(status, "notmodified");
1247 ok(data == null, "response body should be empty")
1252 equals(false, "error");
1258 equals(false, "error");
1264 test("jQuery.ajax - Etag support", function() {
1269 var url = "data/etag.php?ts=" + new Date();
1274 success: function(data, status) {
1275 equals(status, "success");
1280 success: function(data, status) {
1281 if ( data === "FAIL" ) {
1282 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1283 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1285 equals(status, "notmodified");
1286 ok(data == null, "response body should be empty")
1291 equals(false, "error");
1297 equals(false, "error");
1303 test("jQuery.ajax - active counter", function() {
1304 ok( jQuery.ajax.active == 0, "ajax active counter should be zero: " + jQuery.ajax.active );