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 - HEAD requests", function() {
297 url: url("data/name.html"),
299 success: function(data, status, xhr){
300 var h = xhr.getAllResponseHeaders();
301 ok( /Date/i.test(h), 'No Date in HEAD response' );
304 url: url("data/name.html"),
305 data: { whip_it: "good" },
307 success: function(data, status, xhr){
308 var h = xhr.getAllResponseHeaders();
309 ok( /Date/i.test(h), 'No Date in HEAD response with data' );
318 test("jQuery.ajax - beforeSend", function() {
324 jQuery.ajaxSetup({ timeout: 0 });
327 url: url("data/name.html"),
328 beforeSend: function(xml) {
331 success: function(data) {
332 ok( check, "check beforeSend was executed" );
338 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
340 var request = jQuery.ajax({
341 url: url("data/name.html"),
342 beforeSend: function() {
343 ok( true, "beforeSend got called, canceling" );
346 success: function() {
347 ok( false, "request didn't get canceled" );
349 complete: function() {
350 ok( false, "request didn't get canceled" );
353 ok( false, "request didn't get canceled" );
356 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
359 window.foobar = null;
360 window.testFoo = undefined;
362 test("jQuery.ajax - dataType html", function() {
366 var verifyEvaluation = function() {
367 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
368 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
375 url: url("data/test.html"),
376 success: function(data) {
377 jQuery("#ap").html(data);
378 ok( data.match(/^html text/), 'Check content for datatype html' );
379 setTimeout(verifyEvaluation, 600);
384 test("serialize()", function() {
387 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
388 jQuery("#search").after(
389 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
390 '<input type="number" id="html5number" name="number" value="43" />'
393 equals( jQuery('#form').serialize(),
394 "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",
395 'Check form serialization as query string');
397 equals( jQuery('#form :input').serialize(),
398 "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",
399 'Check input serialization as query string');
401 equals( jQuery('#testForm').serialize(),
402 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
403 'Check form serialization as query string');
405 equals( jQuery('#testForm :input').serialize(),
406 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
407 'Check input serialization as query string');
409 equals( jQuery('#form, #testForm').serialize(),
410 "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=",
411 'Multiple form serialization as query string');
413 /* Temporarily disabled. Opera 10 has problems with form serialization.
414 equals( jQuery('#form, #testForm :input').serialize(),
415 "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=",
416 'Mixed form/input serialization as query string');
418 jQuery("#html5email, #html5number").remove();
421 test("jQuery.param()", function() {
424 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
426 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
427 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
429 params = {someName: [1, 2, 3], regularThing: "blah" };
430 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
432 params = {foo: ['a', 'b', 'c']};
433 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
435 params = {foo: ["baz", 42, "All your base are belong to us"] };
436 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
438 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
439 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" );
441 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?" };
442 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" );
444 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 ] };
445 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" );
447 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?" };
448 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" );
450 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." );
452 // Make sure empty arrays and objects are handled #6481
453 equals( jQuery.param({"foo": {"bar": []} }), "foo%5Bbar%5D=", "Empty array param" );
454 equals( jQuery.param({"foo": {"bar": [], foo: 1} }), "foo%5Bbar%5D=&foo%5Bfoo%5D=1", "Empty array param" );
455 equals( jQuery.param({"foo": {"bar": {}} }), "foo%5Bbar%5D=", "Empty object param" );
457 jQuery.ajaxSetup({ traditional: true });
459 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
460 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
462 params = {someName: [1, 2, 3], regularThing: "blah" };
463 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
465 params = {foo: ['a', 'b', 'c']};
466 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
468 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
469 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
471 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
472 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" );
474 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?" };
475 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" );
477 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 ] };
478 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)" );
480 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?" };
481 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" );
483 params = { param1: null };
484 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
487 test("synchronous request", function() {
489 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
492 test("synchronous request with callbacks", function() {
495 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
496 ok( /^{ "data"/.test( result ), "check returned text" );
499 test("pass-through request object", function() {
503 var target = "data/name.html";
504 var successCount = 0;
507 var success = function() {
510 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
512 errorEx += ": " + xml.status;
514 jQuery("#foo").one('ajaxStop', function () {
515 equals(successCount, 5, "Check all ajax calls successful");
516 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
517 jQuery("#foo").unbind('ajaxError');
522 ok( jQuery.get(url(target), success), "get" );
523 ok( jQuery.post(url(target), success), "post" );
524 ok( jQuery.getScript(url("data/test.js"), success), "script" );
525 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
526 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
529 test("ajax cache", function () {
536 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
537 var re = /_=(.*?)(&|$)/g;
539 for (var i = 0; i < 6; i++) {
540 var ret = re.exec(s.url);
546 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
547 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
552 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
553 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
554 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
555 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
556 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
557 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
562 * The assertions expect that the passed-in object will be modified,
563 * which shouldn't be the case. Fixes #5439.
564 test("global ajaxSettings", function() {
567 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
568 var orig = { url: "data/with_fries.xml" };
571 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
573 t = jQuery.extend({}, orig);
576 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
578 t = jQuery.extend({}, orig);
579 t.data = { zoo: 'a', ping: 'b' };
581 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' }" );
583 jQuery.ajaxSettings = tmp;
587 test("load(String)", function() {
589 stop(); // check if load can be called with only url
590 jQuery('#first').load("data/name.html", start);
593 test("load('url selector')", function() {
595 stop(); // check if load can be called with only url
596 jQuery('#first').load("data/test3.html div.user", function(){
597 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
602 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
605 jQuery.ajaxSetup({ dataType: "json" });
606 jQuery("#first").ajaxComplete(function (e, xml, s) {
607 equals( s.dataType, "html", "Verify the load() dataType was html" );
608 jQuery("#first").unbind("ajaxComplete");
609 jQuery.ajaxSetup({ dataType: "" });
612 jQuery('#first').load("data/test3.html");
615 test("load(String, Function) - simple: inject text into DOM", function() {
618 jQuery('#first').load(url("data/name.html"), function() {
619 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
624 test("load(String, Function) - check scripts", function() {
628 var verifyEvaluation = function() {
629 equals( foobar, "bar", 'Check if script src was evaluated after load' );
630 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
634 jQuery('#first').load(url('data/test.html'), function() {
635 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
636 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
637 equals( testFoo, "foo", 'Check if script was evaluated after load' );
638 setTimeout(verifyEvaluation, 600);
642 test("load(String, Function) - check file with only a script tag", function() {
646 jQuery('#first').load(url('data/test2.html'), function() {
647 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
648 equals( testFoo, "foo", 'Check if script was evaluated after load' );
654 test("load(String, Object, Function)", function() {
658 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
659 var $post = jQuery(this).find('#post');
660 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
661 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
666 test("load(String, String, Function)", function() {
670 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
671 var $get = jQuery(this).find('#get');
672 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
673 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
678 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
681 jQuery.get(url('data/dashboard.xml'), function(xml) {
683 jQuery('tab', xml).each(function() {
684 content.push(jQuery(this).text());
686 equals( content[0], 'blabla', 'Check first tab');
687 equals( content[1], 'blublu', 'Check second tab');
692 test("jQuery.getScript(String, Function) - with callback", function() {
695 jQuery.getScript(url("data/test.js"), function() {
696 equals( foobar, "bar", 'Check if script was evaluated' );
697 setTimeout(start, 100);
701 test("jQuery.getScript(String, Function) - no callback", function() {
704 jQuery.getScript(url("data/test.js"), function(){
709 test("jQuery.ajax() - JSONP, Local", function() {
713 function plus(){ if ( ++count == 8 ) start(); }
718 url: "data/jsonp.php",
720 success: function(data){
721 ok( data.data, "JSON results returned (GET, no callback)" );
724 error: function(data){
725 ok( false, "Ajax error JSON (GET, no callback)" );
731 url: "data/jsonp.php?callback=?",
733 success: function(data){
734 ok( data.data, "JSON results returned (GET, url callback)" );
737 error: function(data){
738 ok( false, "Ajax error JSON (GET, url callback)" );
744 url: "data/jsonp.php",
747 success: function(data){
748 ok( data.data, "JSON results returned (GET, data callback)" );
751 error: function(data){
752 ok( false, "Ajax error JSON (GET, data callback)" );
758 url: "data/jsonp.php",
761 success: function(data){
762 ok( data.data, "JSON results returned (GET, data obj callback)" );
765 error: function(data){
766 ok( false, "Ajax error JSON (GET, data obj callback)" );
772 url: "data/jsonp.php",
774 jsonpCallback: "jsonpResults",
775 success: function(data){
776 ok( data.data, "JSON results returned (GET, custom callback name)" );
779 error: function(data){
780 ok( false, "Ajax error JSON (GET, custom callback name)" );
787 url: "data/jsonp.php",
789 success: function(data){
790 ok( data.data, "JSON results returned (POST, no callback)" );
793 error: function(data){
794 ok( false, "Ajax error JSON (GET, data obj callback)" );
801 url: "data/jsonp.php",
804 success: function(data){
805 ok( data.data, "JSON results returned (POST, data callback)" );
808 error: function(data){
809 ok( false, "Ajax error JSON (POST, data callback)" );
816 url: "data/jsonp.php",
819 success: function(data){
820 ok( data.data, "JSON results returned (POST, data obj callback)" );
823 error: function(data){
824 ok( false, "Ajax error JSON (POST, data obj callback)" );
830 test("JSONP - Custom JSONP Callback", function() {
834 window.jsonpResults = function(data) {
835 ok( data.data, "JSON results returned (GET, custom callback function)" );
840 url: "data/jsonp.php",
842 jsonpCallback: "jsonpResults"
846 test("jQuery.ajax() - JSONP, Remote", function() {
850 function plus(){ if ( ++count == 4 ) start(); }
852 var base = window.location.href.replace(/[^\/]*$/, "");
857 url: base + "data/jsonp.php",
859 success: function(data){
860 ok( data.data, "JSON results returned (GET, no callback)" );
863 error: function(data){
864 ok( false, "Ajax error JSON (GET, no callback)" );
870 url: base + "data/jsonp.php?callback=?",
872 success: function(data){
873 ok( data.data, "JSON results returned (GET, url callback)" );
876 error: function(data){
877 ok( false, "Ajax error JSON (GET, url callback)" );
883 url: base + "data/jsonp.php",
886 success: function(data){
887 ok( data.data, "JSON results returned (GET, data callback)" );
890 error: function(data){
891 ok( false, "Ajax error JSON (GET, data callback)" );
897 url: base + "data/jsonp.php",
900 success: function(data){
901 ok( data.data, "JSON results returned (GET, data obj callback)" );
904 error: function(data){
905 ok( false, "Ajax error JSON (GET, data obj callback)" );
911 test("jQuery.ajax() - script, Remote", function() {
914 var base = window.location.href.replace(/[^\/]*$/, "");
919 url: base + "data/test.js",
921 success: function(data){
922 ok( foobar, "Script results returned (GET, no callback)" );
928 test("jQuery.ajax() - script, Remote with POST", function() {
931 var base = window.location.href.replace(/[^\/]*$/, "");
936 url: base + "data/test.js",
939 success: function(data, status){
940 ok( foobar, "Script results returned (POST, no callback)" );
941 equals( status, "success", "Script results returned (POST, no callback)" );
944 error: function(xhr) {
945 ok( false, "ajax error, status code: " + xhr.status );
951 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
954 var base = window.location.href.replace(/[^\/]*$/, "");
955 base = base.replace(/^.*?\/\//, "//");
960 url: base + "data/test.js",
962 success: function(data){
963 ok( foobar, "Script results returned (GET, no callback)" );
969 test("jQuery.ajax() - malformed JSON", function() {
975 url: "data/badjson.js",
978 ok( false, "Success." );
981 error: function(xhr, msg, detailedMsg) {
982 equals( "parsererror", msg, "A parse error occurred." );
983 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
989 test("jQuery.ajax() - script by content-type", function() {
995 url: "data/script.php",
996 data: { header: "script" },
997 success: function() {
1003 test("jQuery.ajax() - json by content-type", function() {
1009 url: "data/json.php",
1010 data: { header: "json", json: "array" },
1011 success: function( json ) {
1012 ok( json.length >= 2, "Check length");
1013 equals( json[0].name, 'John', 'Check JSON: first, name' );
1014 equals( json[0].age, 21, 'Check JSON: first, age' );
1015 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1016 equals( json[1].age, 25, 'Check JSON: second, age' );
1022 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1025 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1026 ok( json.length >= 2, "Check length");
1027 equals( json[0].name, 'John', 'Check JSON: first, name' );
1028 equals( json[0].age, 21, 'Check JSON: first, age' );
1029 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1030 equals( json[1].age, 25, 'Check JSON: second, age' );
1035 test("jQuery.getJSON(String, Function) - JSON object", function() {
1038 jQuery.getJSON(url("data/json.php"), function(json) {
1039 if (json && json.data) {
1040 equals( json.data.lang, 'en', 'Check JSON: lang' );
1041 equals( json.data.length, 25, 'Check JSON: length' );
1047 test("jQuery.getJSON - Using Native JSON", function() {
1050 var old = window.JSON;
1052 parse: function(str){
1053 ok( true, "Verifying that parse method was run" );
1059 jQuery.getJSON(url("data/json.php"), function(json) {
1061 equals( json, true, "Verifying return value" );
1066 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1069 var base = window.location.href.replace(/[^\/]*$/, "");
1072 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1073 equals( json.data.lang, 'en', 'Check JSON: lang' );
1074 equals( json.data.length, 25, 'Check JSON: length' );
1079 test("jQuery.post - data", function() {
1083 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1084 jQuery('math', xml).each(function() {
1085 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1086 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1092 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1097 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1098 jQuery('math', xml).each(function() {
1099 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1100 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1102 if ( ++done === 2 ) start();
1105 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1106 jQuery('math', xml).each(function() {
1107 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1108 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1110 if ( ++done === 2 ) start();
1114 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1119 jQuery.ajaxSetup({timeout: 1000});
1121 var pass = function() {
1123 if ( passed == 2 ) {
1124 ok( true, 'Check local and global callbacks after timeout' );
1125 jQuery('#main').unbind("ajaxError");
1130 var fail = function(a,b,c) {
1131 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1135 jQuery('#main').ajaxError(pass);
1139 url: url("data/name.php?wait=5"),
1145 jQuery.ajaxSetup({timeout: 0});
1148 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1150 jQuery.ajaxSetup({timeout: 50});
1155 url: url("data/name.php?wait=1"),
1157 ok( false, 'Check for local timeout failed' );
1160 success: function() {
1161 ok( true, 'Check for local timeout' );
1167 jQuery.ajaxSetup({timeout: 0});
1170 test("jQuery.ajax - simple get", function() {
1175 url: url("data/name.php?name=foo"),
1176 success: function(msg){
1177 equals( msg, 'bar', 'Check for GET' );
1183 test("jQuery.ajax - simple post", function() {
1188 url: url("data/name.php"),
1190 success: function(msg){
1191 equals( msg, 'pan', 'Check for POST' );
1197 test("ajaxSetup()", function() {
1201 url: url("data/name.php?name=foo"),
1202 success: function(msg){
1203 equals( msg, 'bar', 'Check for GET' );
1211 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1214 url: "data/name.php?wait=1",
1216 error: function(request, status) {
1217 ok( status != null, "status shouldn't be null in error handler" );
1218 equals( "timeout", status );
1225 test("data option: evaluate function values (#2806)", function() {
1228 url: "data/echoQuery.php",
1234 success: function(result) {
1235 equals( result, "key=value" );
1241 test("data option: empty bodies for non-GET requests", function() {
1244 url: "data/echoData.php",
1247 success: function(result) {
1248 equals( result, "" );
1254 test("jQuery.ajax - If-Modified-Since support", function() {
1259 var url = "data/if_modified_since.php?ts=" + new Date();
1264 success: function(data, status) {
1265 equals(status, "success");
1270 success: function(data, status) {
1271 if ( data === "FAIL" ) {
1272 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1273 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1275 equals(status, "notmodified");
1276 ok(data == null, "response body should be empty")
1281 // Do this because opera simply refuses to implement 304 handling :(
1282 // A feature-driven way of detecting this would be appreciated
1283 // See: http://gist.github.com/599419
1284 ok(jQuery.browser.opera, "error");
1285 ok(jQuery.browser.opera, "error");
1291 // Do this because opera simply refuses to implement 304 handling :(
1292 // A feature-driven way of detecting this would be appreciated
1293 // See: http://gist.github.com/599419
1294 ok(jQuery.browser.opera, "error");
1300 test("jQuery.ajax - Etag support", function() {
1305 var url = "data/etag.php?ts=" + new Date();
1310 success: function(data, status) {
1311 equals(status, "success");
1316 success: function(data, status) {
1317 if ( data === "FAIL" ) {
1318 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1319 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1321 equals(status, "notmodified");
1322 ok(data == null, "response body should be empty")
1327 // Do this because opera simply refuses to implement 304 handling :(
1328 // A feature-driven way of detecting this would be appreciated
1329 // See: http://gist.github.com/599419
1330 ok(jQuery.browser.opera, "error");
1331 ok(jQuery.browser.opera, "error");
1337 // Do this because opera simply refuses to implement 304 handling :(
1338 // A feature-driven way of detecting this would be appreciated
1339 // See: http://gist.github.com/599419
1340 ok(jQuery.browser.opera, "error");
1346 test("jQuery.ajax - active counter", function() {
1347 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );