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() - 304", function() {
76 url: url("data/notmodified.php"),
77 success: function(){ ok(true, "304 ok"); },
78 error: function(){ ok(false, "304 not ok "); },
79 complete: function(xhr){ start(); }
83 test(".load()) - 404 error callbacks", function() {
87 jQuery('#foo').ajaxStart(function(){
88 ok( true, "ajaxStart" );
89 }).ajaxStop(function(){
90 ok( true, "ajaxStop" );
92 }).ajaxSend(function(){
93 ok( true, "ajaxSend" );
94 }).ajaxComplete(function(){
95 ok( true, "ajaxComplete" );
96 }).ajaxError(function(){
97 ok( true, "ajaxError" );
98 }).ajaxSuccess(function(){
99 ok( false, "ajaxSuccess" );
102 jQuery("<div/>").load("data/404.html", function(){
103 ok(true, "complete");
107 test("jQuery.ajax() - abort", function() {
111 jQuery('#foo').ajaxStart(function(){
112 ok( true, "ajaxStart" );
113 }).ajaxStop(function(){
114 ok( true, "ajaxStop" );
116 }).ajaxSend(function(){
117 ok( true, "ajaxSend" );
118 }).ajaxComplete(function(){
119 ok( true, "ajaxComplete" );
122 var xhr = jQuery.ajax({
123 url: url("data/name.php?wait=5"),
124 beforeSend: function(){ ok(true, "beforeSend"); },
125 complete: function(){ ok(true, "complete"); }
131 test("Ajax events with context", function() {
135 var context = document.createElement("div");
138 equals( this, context, e.type );
141 function callback(msg){
143 equals( this, context, "context is preserved on callback " + msg );
147 function nocallback(msg){
149 equals( typeof this.url, "string", "context is settings on callback " + msg );
153 jQuery('#foo').add(context)
160 url: url("data/name.html"),
161 beforeSend: callback("beforeSend"),
162 success: callback("success"),
163 error: callback("error"),
165 callback("complete").call(this);
168 url: url("data/404.html"),
170 beforeSend: callback("beforeSend"),
171 error: callback("error"),
172 complete: function(){
173 callback("complete").call(this);
175 jQuery('#foo').add(context).unbind();
178 url: url("data/404.html"),
179 beforeSend: nocallback("beforeSend"),
180 error: nocallback("error"),
181 complete: function(){
182 nocallback("complete").call(this);
193 test("jQuery.ajax context modification", function() {
201 url: url("data/name.html"),
203 beforeSend: function(){
206 complete: function() {
211 equals( obj.test, "foo", "Make sure the original object is maintained." );
214 test("jQuery.ajax() - disabled globals", function() {
218 jQuery('#foo').ajaxStart(function(){
219 ok( false, "ajaxStart" );
220 }).ajaxStop(function(){
221 ok( false, "ajaxStop" );
222 }).ajaxSend(function(){
223 ok( false, "ajaxSend" );
224 }).ajaxComplete(function(){
225 ok( false, "ajaxComplete" );
226 }).ajaxError(function(){
227 ok( false, "ajaxError" );
228 }).ajaxSuccess(function(){
229 ok( false, "ajaxSuccess" );
234 url: url("data/name.html"),
235 beforeSend: function(){ ok(true, "beforeSend"); },
236 success: function(){ ok(true, "success"); },
237 error: function(){ ok(false, "error"); },
238 complete: function(){
239 ok(true, "complete");
240 setTimeout(function(){ start(); }, 13);
245 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
249 url: url("data/with_fries.xml"),
251 success: function(resp) {
252 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
253 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
254 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
260 test("jQuery.ajax - beforeSend", function() {
266 jQuery.ajaxSetup({ timeout: 0 });
269 url: url("data/name.html"),
270 beforeSend: function(xml) {
273 success: function(data) {
274 ok( check, "check beforeSend was executed" );
280 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
282 var request = jQuery.ajax({
283 url: url("data/name.html"),
284 beforeSend: function() {
285 ok( true, "beforeSend got called, canceling" );
288 success: function() {
289 ok( false, "request didn't get canceled" );
291 complete: function() {
292 ok( false, "request didn't get canceled" );
295 ok( false, "request didn't get canceled" );
298 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
301 window.foobar = null;
302 window.testFoo = undefined;
304 test("jQuery.ajax - dataType html", function() {
308 var verifyEvaluation = function() {
309 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
310 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
317 url: url("data/test.html"),
318 success: function(data) {
319 jQuery("#ap").html(data);
320 ok( data.match(/^html text/), 'Check content for datatype html' );
321 setTimeout(verifyEvaluation, 600);
326 test("serialize()", function() {
329 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
330 jQuery("#search").after(
331 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
332 '<input type="number" id="html5number" name="number" value="43" />'
335 equals( jQuery('#form').serialize(),
336 "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",
337 'Check form serialization as query string');
339 equals( jQuery('#form :input').serialize(),
340 "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",
341 'Check input serialization as query string');
343 equals( jQuery('#testForm').serialize(),
344 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
345 'Check form serialization as query string');
347 equals( jQuery('#testForm :input').serialize(),
348 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
349 'Check input serialization as query string');
351 equals( jQuery('#form, #testForm').serialize(),
352 "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=",
353 'Multiple form serialization as query string');
355 /* Temporarily disabled. Opera 10 has problems with form serialization.
356 equals( jQuery('#form, #testForm :input').serialize(),
357 "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=",
358 'Mixed form/input serialization as query string');
360 jQuery("#html5email, #html5number").remove();
363 test("jQuery.param()", function() {
366 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
368 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
369 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
371 params = {someName: [1, 2, 3], regularThing: "blah" };
372 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
374 params = {foo: ['a', 'b', 'c']};
375 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
377 params = {foo: ["baz", 42, "All your base are belong to us"] };
378 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
380 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
381 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" );
383 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?" };
384 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" );
386 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 ] };
387 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" );
389 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?" };
390 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" );
392 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." );
394 jQuery.ajaxSetup({ traditional: true });
396 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
397 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
399 params = {someName: [1, 2, 3], regularThing: "blah" };
400 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
402 params = {foo: ['a', 'b', 'c']};
403 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
405 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
406 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
408 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
409 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" );
411 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?" };
412 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" );
414 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 ] };
415 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)" );
417 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?" };
418 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" );
420 params = { param1: null };
421 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
424 test("synchronous request", function() {
426 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
429 test("synchronous request with callbacks", function() {
432 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
433 ok( /^{ "data"/.test( result ), "check returned text" );
436 test("pass-through request object", function() {
440 var target = "data/name.html";
441 var successCount = 0;
444 var success = function() {
447 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
449 errorEx += ": " + xml.status;
451 jQuery("#foo").one('ajaxStop', function () {
452 equals(successCount, 5, "Check all ajax calls successful");
453 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
454 jQuery("#foo").unbind('ajaxError');
459 ok( jQuery.get(url(target), success), "get" );
460 ok( jQuery.post(url(target), success), "post" );
461 ok( jQuery.getScript(url("data/test.js"), success), "script" );
462 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
463 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
466 test("ajax cache", function () {
473 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
474 var re = /_=(.*?)(&|$)/g;
476 for (var i = 0; i < 6; i++) {
477 var ret = re.exec(s.url);
483 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
484 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
489 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
490 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
491 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
492 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
493 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
494 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
499 * The assertions expect that the passed-in object will be modified,
500 * which shouldn't be the case. Fixes #5439.
501 test("global ajaxSettings", function() {
504 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
505 var orig = { url: "data/with_fries.xml" };
508 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
510 t = jQuery.extend({}, orig);
513 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
515 t = jQuery.extend({}, orig);
516 t.data = { zoo: 'a', ping: 'b' };
518 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' }" );
520 jQuery.ajaxSettings = tmp;
524 test("load(String)", function() {
526 stop(); // check if load can be called with only url
527 jQuery('#first').load("data/name.html", start);
530 test("load('url selector')", function() {
532 stop(); // check if load can be called with only url
533 jQuery('#first').load("data/test3.html div.user", function(){
534 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
539 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
542 jQuery.ajaxSetup({ dataType: "json" });
543 jQuery("#first").ajaxComplete(function (e, xml, s) {
544 equals( s.dataType, "html", "Verify the load() dataType was html" );
545 jQuery("#first").unbind("ajaxComplete");
546 jQuery.ajaxSetup({ dataType: "" });
549 jQuery('#first').load("data/test3.html");
552 test("load(String, Function) - simple: inject text into DOM", function() {
555 jQuery('#first').load(url("data/name.html"), function() {
556 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
561 test("load(String, Function) - check scripts", function() {
565 var verifyEvaluation = function() {
566 equals( foobar, "bar", 'Check if script src was evaluated after load' );
567 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
571 jQuery('#first').load(url('data/test.html'), function() {
572 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
573 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
574 equals( testFoo, "foo", 'Check if script was evaluated after load' );
575 setTimeout(verifyEvaluation, 600);
579 test("load(String, Function) - check file with only a script tag", function() {
583 jQuery('#first').load(url('data/test2.html'), function() {
584 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
585 equals( testFoo, "foo", 'Check if script was evaluated after load' );
591 test("load(String, Object, Function)", function() {
595 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
596 var $post = jQuery(this).find('#post');
597 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
598 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
603 test("load(String, String, Function)", function() {
607 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
608 var $get = jQuery(this).find('#get');
609 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
610 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
615 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
618 jQuery.get(url('data/dashboard.xml'), function(xml) {
620 jQuery('tab', xml).each(function() {
621 content.push(jQuery(this).text());
623 equals( content[0], 'blabla', 'Check first tab');
624 equals( content[1], 'blublu', 'Check second tab');
629 test("jQuery.getScript(String, Function) - with callback", function() {
632 jQuery.getScript(url("data/test.js"), function() {
633 equals( foobar, "bar", 'Check if script was evaluated' );
634 setTimeout(start, 100);
638 test("jQuery.getScript(String, Function) - no callback", function() {
641 jQuery.getScript(url("data/test.js"), function(){
646 test("jQuery.ajax() - JSONP, Local", function() {
650 function plus(){ if ( ++count == 8 ) start(); }
655 url: "data/jsonp.php",
657 success: function(data){
658 ok( data.data, "JSON results returned (GET, no callback)" );
661 error: function(data){
662 ok( false, "Ajax error JSON (GET, no callback)" );
668 url: "data/jsonp.php?callback=?",
670 success: function(data){
671 ok( data.data, "JSON results returned (GET, url callback)" );
674 error: function(data){
675 ok( false, "Ajax error JSON (GET, url callback)" );
681 url: "data/jsonp.php",
684 success: function(data){
685 ok( data.data, "JSON results returned (GET, data callback)" );
688 error: function(data){
689 ok( false, "Ajax error JSON (GET, data callback)" );
695 url: "data/jsonp.php",
698 success: function(data){
699 ok( data.data, "JSON results returned (GET, data obj callback)" );
702 error: function(data){
703 ok( false, "Ajax error JSON (GET, data obj callback)" );
709 url: "data/jsonp.php",
711 jsonpCallback: "jsonpResults",
712 success: function(data){
713 ok( data.data, "JSON results returned (GET, custom callback name)" );
716 error: function(data){
717 ok( false, "Ajax error JSON (GET, custom callback name)" );
724 url: "data/jsonp.php",
726 success: function(data){
727 ok( data.data, "JSON results returned (POST, no callback)" );
730 error: function(data){
731 ok( false, "Ajax error JSON (GET, data obj callback)" );
738 url: "data/jsonp.php",
741 success: function(data){
742 ok( data.data, "JSON results returned (POST, data callback)" );
745 error: function(data){
746 ok( false, "Ajax error JSON (POST, data callback)" );
753 url: "data/jsonp.php",
756 success: function(data){
757 ok( data.data, "JSON results returned (POST, data obj callback)" );
760 error: function(data){
761 ok( false, "Ajax error JSON (POST, data obj callback)" );
767 test("JSONP - Custom JSONP Callback", function() {
771 window.jsonpResults = function(data) {
772 ok( data.data, "JSON results returned (GET, custom callback function)" );
777 url: "data/jsonp.php",
779 jsonpCallback: "jsonpResults"
783 test("jQuery.ajax() - JSONP, Remote", function() {
787 function plus(){ if ( ++count == 4 ) start(); }
789 var base = window.location.href.replace(/[^\/]*$/, "");
794 url: base + "data/jsonp.php",
796 success: function(data){
797 ok( data.data, "JSON results returned (GET, no callback)" );
800 error: function(data){
801 ok( false, "Ajax error JSON (GET, no callback)" );
807 url: base + "data/jsonp.php?callback=?",
809 success: function(data){
810 ok( data.data, "JSON results returned (GET, url callback)" );
813 error: function(data){
814 ok( false, "Ajax error JSON (GET, url callback)" );
820 url: base + "data/jsonp.php",
823 success: function(data){
824 ok( data.data, "JSON results returned (GET, data callback)" );
827 error: function(data){
828 ok( false, "Ajax error JSON (GET, data callback)" );
834 url: base + "data/jsonp.php",
837 success: function(data){
838 ok( data.data, "JSON results returned (GET, data obj callback)" );
841 error: function(data){
842 ok( false, "Ajax error JSON (GET, data obj callback)" );
848 test("jQuery.ajax() - script, Remote", function() {
851 var base = window.location.href.replace(/[^\/]*$/, "");
856 url: base + "data/test.js",
858 success: function(data){
859 ok( foobar, "Script results returned (GET, no callback)" );
865 test("jQuery.ajax() - script, Remote with POST", function() {
868 var base = window.location.href.replace(/[^\/]*$/, "");
873 url: base + "data/test.js",
876 success: function(data, status){
877 ok( foobar, "Script results returned (POST, no callback)" );
878 equals( status, "success", "Script results returned (POST, no callback)" );
881 error: function(xhr) {
882 ok( false, "ajax error, status code: " + xhr.status );
888 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
891 var base = window.location.href.replace(/[^\/]*$/, "");
892 base = base.replace(/^.*?\/\//, "//");
897 url: base + "data/test.js",
899 success: function(data){
900 ok( foobar, "Script results returned (GET, no callback)" );
906 test("jQuery.ajax() - malformed JSON", function() {
912 url: "data/badjson.js",
915 ok( false, "Success." );
918 error: function(xhr, msg, detailedMsg) {
919 equals( "parsererror", msg, "A parse error occurred." );
920 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
926 test("jQuery.ajax() - script by content-type", function() {
932 url: "data/script.php",
933 data: { header: "script" },
934 success: function() {
940 test("jQuery.ajax() - json by content-type", function() {
946 url: "data/json.php",
947 data: { header: "json", json: "array" },
948 success: function( json ) {
949 ok( json.length >= 2, "Check length");
950 equals( json[0].name, 'John', 'Check JSON: first, name' );
951 equals( json[0].age, 21, 'Check JSON: first, age' );
952 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
953 equals( json[1].age, 25, 'Check JSON: second, age' );
959 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
962 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
963 ok( json.length >= 2, "Check length");
964 equals( json[0].name, 'John', 'Check JSON: first, name' );
965 equals( json[0].age, 21, 'Check JSON: first, age' );
966 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
967 equals( json[1].age, 25, 'Check JSON: second, age' );
972 test("jQuery.getJSON(String, Function) - JSON object", function() {
975 jQuery.getJSON(url("data/json.php"), function(json) {
976 if (json && json.data) {
977 equals( json.data.lang, 'en', 'Check JSON: lang' );
978 equals( json.data.length, 25, 'Check JSON: length' );
984 test("jQuery.getJSON - Using Native JSON", function() {
987 var old = window.JSON;
989 parse: function(str){
990 ok( true, "Verifying that parse method was run" );
996 jQuery.getJSON(url("data/json.php"), function(json) {
998 equals( json, true, "Verifying return value" );
1003 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1006 var base = window.location.href.replace(/[^\/]*$/, "");
1009 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1010 equals( json.data.lang, 'en', 'Check JSON: lang' );
1011 equals( json.data.length, 25, 'Check JSON: length' );
1016 test("jQuery.post - data", function() {
1020 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1021 jQuery('math', xml).each(function() {
1022 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1023 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1029 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1034 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1035 jQuery('math', xml).each(function() {
1036 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1037 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1039 if ( ++done === 2 ) start();
1042 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1043 jQuery('math', xml).each(function() {
1044 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1045 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1047 if ( ++done === 2 ) start();
1051 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1056 jQuery.ajaxSetup({timeout: 1000});
1058 var pass = function() {
1060 if ( passed == 2 ) {
1061 ok( true, 'Check local and global callbacks after timeout' );
1062 jQuery('#main').unbind("ajaxError");
1067 var fail = function(a,b,c) {
1068 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1072 jQuery('#main').ajaxError(pass);
1076 url: url("data/name.php?wait=5"),
1082 jQuery.ajaxSetup({timeout: 0});
1085 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1087 jQuery.ajaxSetup({timeout: 50});
1092 url: url("data/name.php?wait=1"),
1094 ok( false, 'Check for local timeout failed' );
1097 success: function() {
1098 ok( true, 'Check for local timeout' );
1104 jQuery.ajaxSetup({timeout: 0});
1107 test("jQuery.ajax - simple get", function() {
1112 url: url("data/name.php?name=foo"),
1113 success: function(msg){
1114 equals( msg, 'bar', 'Check for GET' );
1120 test("jQuery.ajax - simple post", function() {
1125 url: url("data/name.php"),
1127 success: function(msg){
1128 equals( msg, 'pan', 'Check for POST' );
1134 test("ajaxSetup()", function() {
1138 url: url("data/name.php?name=foo"),
1139 success: function(msg){
1140 equals( msg, 'bar', 'Check for GET' );
1148 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1151 url: "data/name.php?wait=1",
1153 error: function(request, status) {
1154 ok( status != null, "status shouldn't be null in error handler" );
1155 equals( "timeout", status );
1162 test("data option: evaluate function values (#2806)", function() {
1165 url: "data/echoQuery.php",
1171 success: function(result) {
1172 equals( result, "key=value" );
1178 test("data option: empty bodies for non-GET requests", function() {
1181 url: "data/echoData.php",
1184 success: function(result) {
1185 equals( result, "" );
1191 test("jQuery.ajax - If-Modified-Since support", function() {
1196 var url = "data/if_modified_since.php?ts=" + new Date();
1201 success: function(data, status) {
1202 equals(status, "success");
1207 success: function(data, status) {
1208 if ( data === "FAIL" ) {
1209 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1210 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1212 equals(status, "notmodified");
1213 ok(data == null, "response body should be empty")
1222 test("jQuery.ajax - Etag support", function() {
1227 var url = "data/etag.php?ts=" + new Date();
1232 success: function(data, status) {
1233 equals(status, "success");
1238 success: function(data, status) {
1239 if ( data === "FAIL" ) {
1240 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1241 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1243 equals(status, "notmodified");
1244 ok(data == null, "response body should be empty")
1254 test("jQuery.ajax - active counter", function() {
1255 ok( jQuery.ajax.active == 0, "ajax active counter should be zero: " + jQuery.ajax.active );