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)" );
829 // Supports Ticket #5803
831 url: "data/jsonp.php",
832 jsonpCallback: "jsonpResults",
833 success: function(data){
834 ok( data.data, "JSON results returned without dataType:jsonp when jsonpCallback is defined" );
837 error: function(data){
838 ok( false, "Ajax error JSON (GET, custom callback name)" );
845 test("JSONP - Custom JSONP Callback", function() {
849 window.jsonpResults = function(data) {
850 ok( data.data, "JSON results returned (GET, custom callback function)" );
855 url: "data/jsonp.php",
857 jsonpCallback: "jsonpResults"
861 test("jQuery.ajax() - JSONP, Remote", function() {
865 function plus(){ if ( ++count == 4 ) start(); }
867 var base = window.location.href.replace(/[^\/]*$/, "");
872 url: base + "data/jsonp.php",
874 success: function(data){
875 ok( data.data, "JSON results returned (GET, no callback)" );
878 error: function(data){
879 ok( false, "Ajax error JSON (GET, no callback)" );
885 url: base + "data/jsonp.php?callback=?",
887 success: function(data){
888 ok( data.data, "JSON results returned (GET, url callback)" );
891 error: function(data){
892 ok( false, "Ajax error JSON (GET, url callback)" );
898 url: base + "data/jsonp.php",
901 success: function(data){
902 ok( data.data, "JSON results returned (GET, data callback)" );
905 error: function(data){
906 ok( false, "Ajax error JSON (GET, data callback)" );
912 url: base + "data/jsonp.php",
915 success: function(data){
916 ok( data.data, "JSON results returned (GET, data obj callback)" );
919 error: function(data){
920 ok( false, "Ajax error JSON (GET, data obj callback)" );
926 test("jQuery.ajax() - script, Remote", function() {
929 var base = window.location.href.replace(/[^\/]*$/, "");
934 url: base + "data/test.js",
936 success: function(data){
937 ok( foobar, "Script results returned (GET, no callback)" );
943 test("jQuery.ajax() - script, Remote with POST", function() {
946 var base = window.location.href.replace(/[^\/]*$/, "");
951 url: base + "data/test.js",
954 success: function(data, status){
955 ok( foobar, "Script results returned (POST, no callback)" );
956 equals( status, "success", "Script results returned (POST, no callback)" );
959 error: function(xhr) {
960 ok( false, "ajax error, status code: " + xhr.status );
966 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
969 var base = window.location.href.replace(/[^\/]*$/, "");
970 base = base.replace(/^.*?\/\//, "//");
975 url: base + "data/test.js",
977 success: function(data){
978 ok( foobar, "Script results returned (GET, no callback)" );
984 test("jQuery.ajax() - malformed JSON", function() {
990 url: "data/badjson.js",
993 ok( false, "Success." );
996 error: function(xhr, msg, detailedMsg) {
997 equals( "parsererror", msg, "A parse error occurred." );
998 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
1004 test("jQuery.ajax() - script by content-type", function() {
1010 url: "data/script.php",
1011 data: { header: "script" },
1012 success: function() {
1018 test("jQuery.ajax() - json by content-type", function() {
1024 url: "data/json.php",
1025 data: { header: "json", json: "array" },
1026 success: function( json ) {
1027 ok( json.length >= 2, "Check length");
1028 equals( json[0].name, 'John', 'Check JSON: first, name' );
1029 equals( json[0].age, 21, 'Check JSON: first, age' );
1030 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1031 equals( json[1].age, 25, 'Check JSON: second, age' );
1037 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
1040 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
1041 ok( json.length >= 2, "Check length");
1042 equals( json[0].name, 'John', 'Check JSON: first, name' );
1043 equals( json[0].age, 21, 'Check JSON: first, age' );
1044 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
1045 equals( json[1].age, 25, 'Check JSON: second, age' );
1050 test("jQuery.getJSON(String, Function) - JSON object", function() {
1053 jQuery.getJSON(url("data/json.php"), function(json) {
1054 if (json && json.data) {
1055 equals( json.data.lang, 'en', 'Check JSON: lang' );
1056 equals( json.data.length, 25, 'Check JSON: length' );
1062 test("jQuery.getJSON - Using Native JSON", function() {
1065 var old = window.JSON;
1067 parse: function(str){
1068 ok( true, "Verifying that parse method was run" );
1074 jQuery.getJSON(url("data/json.php"), function(json) {
1076 equals( json, true, "Verifying return value" );
1081 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
1084 var base = window.location.href.replace(/[^\/]*$/, "");
1087 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1088 equals( json.data.lang, 'en', 'Check JSON: lang' );
1089 equals( json.data.length, 25, 'Check JSON: length' );
1094 test("jQuery.post - data", function() {
1098 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1099 jQuery('math', xml).each(function() {
1100 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1101 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1107 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1112 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1113 jQuery('math', xml).each(function() {
1114 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1115 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1117 if ( ++done === 2 ) start();
1120 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1121 jQuery('math', xml).each(function() {
1122 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1123 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1125 if ( ++done === 2 ) start();
1129 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1134 jQuery.ajaxSetup({timeout: 1000});
1136 var pass = function() {
1138 if ( passed == 2 ) {
1139 ok( true, 'Check local and global callbacks after timeout' );
1140 jQuery('#main').unbind("ajaxError");
1145 var fail = function(a,b,c) {
1146 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1150 jQuery('#main').ajaxError(pass);
1154 url: url("data/name.php?wait=5"),
1160 jQuery.ajaxSetup({timeout: 0});
1163 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1165 jQuery.ajaxSetup({timeout: 50});
1170 url: url("data/name.php?wait=1"),
1172 ok( false, 'Check for local timeout failed' );
1175 success: function() {
1176 ok( true, 'Check for local timeout' );
1182 jQuery.ajaxSetup({timeout: 0});
1185 test("jQuery.ajax - simple get", function() {
1190 url: url("data/name.php?name=foo"),
1191 success: function(msg){
1192 equals( msg, 'bar', 'Check for GET' );
1198 test("jQuery.ajax - simple post", function() {
1203 url: url("data/name.php"),
1205 success: function(msg){
1206 equals( msg, 'pan', 'Check for POST' );
1212 test("ajaxSetup()", function() {
1216 url: url("data/name.php?name=foo"),
1217 success: function(msg){
1218 equals( msg, 'bar', 'Check for GET' );
1226 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1229 url: "data/name.php?wait=1",
1231 error: function(request, status) {
1232 ok( status != null, "status shouldn't be null in error handler" );
1233 equals( "timeout", status );
1240 test("data option: evaluate function values (#2806)", function() {
1243 url: "data/echoQuery.php",
1249 success: function(result) {
1250 equals( result, "key=value" );
1256 test("data option: empty bodies for non-GET requests", function() {
1259 url: "data/echoData.php",
1262 success: function(result) {
1263 equals( result, "" );
1269 test("jQuery.ajax - If-Modified-Since support", function() {
1274 var url = "data/if_modified_since.php?ts=" + new Date();
1279 success: function(data, status) {
1280 equals(status, "success");
1285 success: function(data, status) {
1286 if ( data === "FAIL" ) {
1287 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1288 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1290 equals(status, "notmodified");
1291 ok(data == null, "response body should be empty")
1296 // Do this because opera simply refuses to implement 304 handling :(
1297 // A feature-driven way of detecting this would be appreciated
1298 // See: http://gist.github.com/599419
1299 ok(jQuery.browser.opera, "error");
1300 ok(jQuery.browser.opera, "error");
1306 // Do this because opera simply refuses to implement 304 handling :(
1307 // A feature-driven way of detecting this would be appreciated
1308 // See: http://gist.github.com/599419
1309 ok(jQuery.browser.opera, "error");
1315 test("jQuery.ajax - Etag support", function() {
1320 var url = "data/etag.php?ts=" + new Date();
1325 success: function(data, status) {
1326 equals(status, "success");
1331 success: function(data, status) {
1332 if ( data === "FAIL" ) {
1333 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1334 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1336 equals(status, "notmodified");
1337 ok(data == null, "response body should be empty")
1342 // Do this because opera simply refuses to implement 304 handling :(
1343 // A feature-driven way of detecting this would be appreciated
1344 // See: http://gist.github.com/599419
1345 ok(jQuery.browser.opera, "error");
1346 ok(jQuery.browser.opera, "error");
1352 // Do this because opera simply refuses to implement 304 handling :(
1353 // A feature-driven way of detecting this would be appreciated
1354 // See: http://gist.github.com/599419
1355 ok(jQuery.browser.opera, "error");
1361 test("jQuery.ajax - active counter", function() {
1362 ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active );