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 setTimeout(function(){
18 jQuery('#foo').ajaxStart(function(){
19 ok( true, "ajaxStart" );
20 }).ajaxStop(function(){
21 ok( true, "ajaxStop" );
23 }).ajaxSend(function(){
24 ok( true, "ajaxSend" );
25 }).ajaxComplete(function(){
26 ok( true, "ajaxComplete" );
27 }).ajaxError(function(){
28 ok( false, "ajaxError" );
29 }).ajaxSuccess(function(){
30 ok( true, "ajaxSuccess" );
34 url: url("data/name.html"),
35 beforeSend: function(){ ok(true, "beforeSend"); },
36 success: function(){ ok(true, "success"); },
37 error: function(){ ok(false, "error"); },
38 complete: function(){ ok(true, "complete"); }
43 test("jQuery.ajax() - error callbacks", function() {
47 jQuery('#foo').ajaxStart(function(){
48 ok( true, "ajaxStart" );
49 }).ajaxStop(function(){
50 ok( true, "ajaxStop" );
52 }).ajaxSend(function(){
53 ok( true, "ajaxSend" );
54 }).ajaxComplete(function(){
55 ok( true, "ajaxComplete" );
56 }).ajaxError(function(){
57 ok( true, "ajaxError" );
58 }).ajaxSuccess(function(){
59 ok( false, "ajaxSuccess" );
62 jQuery.ajaxSetup({ timeout: 500 });
65 url: url("data/name.php?wait=5"),
66 beforeSend: function(){ ok(true, "beforeSend"); },
67 success: function(){ ok(false, "success"); },
68 error: function(){ ok(true, "error"); },
69 complete: function(){ ok(true, "complete"); }
73 test("jQuery.ajax() - abort", function() {
77 jQuery('#foo').ajaxStart(function(){
78 ok( true, "ajaxStart" );
79 }).ajaxStop(function(){
80 ok( true, "ajaxStop" );
82 }).ajaxSend(function(){
83 ok( true, "ajaxSend" );
84 }).ajaxComplete(function(){
85 ok( true, "ajaxComplete" );
88 var xhr = jQuery.ajax({
89 url: url("data/name.php?wait=5"),
90 beforeSend: function(){ ok(true, "beforeSend"); },
91 complete: function(){ ok(true, "complete"); }
97 test("Ajax events with context", function() {
101 var context = document.createElement("div");
104 equals( this, context, e.type );
107 function callback(msg){
109 equals( this, context, "context is preserved on callback " + msg );
113 function nocallback(msg){
115 equals( typeof this.url, "string", "context is settings on callback " + msg );
119 jQuery('#foo').add(context)
126 url: url("data/name.html"),
127 beforeSend: callback("beforeSend"),
128 success: callback("success"),
129 error: callback("error"),
131 callback("complete").call(this);
134 url: url("data/404.html"),
136 beforeSend: callback("beforeSend"),
137 error: callback("error"),
138 complete: function(){
139 callback("complete").call(this);
141 jQuery('#foo').add(context).unbind();
144 url: url("data/404.html"),
145 beforeSend: nocallback("beforeSend"),
146 error: nocallback("error"),
147 complete: function(){
148 nocallback("complete").call(this);
159 test("jQuery.ajax() - disabled globals", function() {
163 jQuery('#foo').ajaxStart(function(){
164 ok( false, "ajaxStart" );
165 }).ajaxStop(function(){
166 ok( false, "ajaxStop" );
167 }).ajaxSend(function(){
168 ok( false, "ajaxSend" );
169 }).ajaxComplete(function(){
170 ok( false, "ajaxComplete" );
171 }).ajaxError(function(){
172 ok( false, "ajaxError" );
173 }).ajaxSuccess(function(){
174 ok( false, "ajaxSuccess" );
179 url: url("data/name.html"),
180 beforeSend: function(){ ok(true, "beforeSend"); },
181 success: function(){ ok(true, "success"); },
182 error: function(){ ok(false, "error"); },
183 complete: function(){
184 ok(true, "complete");
185 setTimeout(function(){ start(); }, 13);
190 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
194 url: url("data/with_fries.xml"),
196 success: function(resp) {
197 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
198 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
199 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
205 test("jQuery.ajax - beforeSend", function() {
211 jQuery.ajaxSetup({ timeout: 0 });
214 url: url("data/name.html"),
215 beforeSend: function(xml) {
218 success: function(data) {
219 ok( check, "check beforeSend was executed" );
225 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
227 var request = jQuery.ajax({
228 url: url("data/name.html"),
229 beforeSend: function() {
230 ok( true, "beforeSend got called, canceling" );
233 success: function() {
234 ok( false, "request didn't get canceled" );
236 complete: function() {
237 ok( false, "request didn't get canceled" );
240 ok( false, "request didn't get canceled" );
243 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
246 window.foobar = null;
247 window.testFoo = undefined;
249 test("jQuery.ajax - dataType html", function() {
253 var verifyEvaluation = function() {
254 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
255 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
262 url: url("data/test.html"),
263 success: function(data) {
264 jQuery("#ap").html(data);
265 ok( data.match(/^html text/), 'Check content for datatype html' );
266 setTimeout(verifyEvaluation, 600);
271 test("serialize()", function() {
274 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
275 jQuery("#search").after(
276 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
277 '<input type="number" id="html5number" name="number" value="43" />'
280 equals( jQuery('#form').serialize(),
281 "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",
282 'Check form serialization as query string');
284 equals( jQuery('#form :input').serialize(),
285 "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",
286 'Check input serialization as query string');
288 equals( jQuery('#testForm').serialize(),
289 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
290 'Check form serialization as query string');
292 equals( jQuery('#testForm :input').serialize(),
293 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
294 'Check input serialization as query string');
296 equals( jQuery('#form, #testForm').serialize(),
297 "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=",
298 'Multiple form serialization as query string');
300 /* Temporarily disabled. Opera 10 has problems with form serialization.
301 equals( jQuery('#form, #testForm :input').serialize(),
302 "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=",
303 'Mixed form/input serialization as query string');
305 jQuery("#html5email, #html5number").remove();
308 test("jQuery.param()", function() {
311 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
313 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
314 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
316 params = {someName: [1, 2, 3], regularThing: "blah" };
317 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
319 params = {foo: ['a', 'b', 'c']};
320 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
322 params = {foo: ["baz", 42, "All your base are belong to us"] };
323 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
325 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
326 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" );
328 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?" };
329 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" );
331 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 ] };
332 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" );
334 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?" };
335 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" );
337 jQuery.ajaxSetup({ traditional: true });
339 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
340 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
342 params = {someName: [1, 2, 3], regularThing: "blah" };
343 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
345 params = {foo: ['a', 'b', 'c']};
346 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
348 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
349 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
351 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
352 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" );
354 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?" };
355 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" );
357 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 ] };
358 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)" );
360 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?" };
361 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" );
363 params = { param1: null };
364 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
367 test("synchronous request", function() {
369 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
372 test("synchronous request with callbacks", function() {
375 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
376 ok( /^{ "data"/.test( result ), "check returned text" );
379 test("pass-through request object", function() {
383 var target = "data/name.html";
384 var successCount = 0;
387 var success = function() {
390 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
392 errorEx += ": " + xml.status;
394 jQuery("#foo").one('ajaxStop', function () {
395 equals(successCount, 5, "Check all ajax calls successful");
396 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
397 jQuery("#foo").unbind('ajaxError');
402 ok( jQuery.get(url(target), success), "get" );
403 ok( jQuery.post(url(target), success), "post" );
404 ok( jQuery.getScript(url("data/test.js"), success), "script" );
405 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
406 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
409 test("ajax cache", function () {
416 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
417 var re = /_=(.*?)(&|$)/g;
419 for (var i = 0; i < 6; i++) {
420 var ret = re.exec(s.url);
426 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
427 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
432 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
433 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
434 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
435 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
436 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
437 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
442 * The assertions expect that the passed-in object will be modified,
443 * which shouldn't be the case. Fixes #5439.
444 test("global ajaxSettings", function() {
447 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
448 var orig = { url: "data/with_fries.xml" };
451 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
453 t = jQuery.extend({}, orig);
456 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
458 t = jQuery.extend({}, orig);
459 t.data = { zoo: 'a', ping: 'b' };
461 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' }" );
463 jQuery.ajaxSettings = tmp;
467 test("load(String)", function() {
469 stop(); // check if load can be called with only url
470 jQuery('#first').load("data/name.html", start);
473 test("load('url selector')", function() {
475 stop(); // check if load can be called with only url
476 jQuery('#first').load("data/test3.html div.user", function(){
477 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
482 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
485 jQuery.ajaxSetup({ dataType: "json" });
486 jQuery("#first").ajaxComplete(function (e, xml, s) {
487 equals( s.dataType, "html", "Verify the load() dataType was html" );
488 jQuery("#first").unbind("ajaxComplete");
489 jQuery.ajaxSetup({ dataType: "" });
492 jQuery('#first').load("data/test3.html");
495 test("load(String, Function) - simple: inject text into DOM", function() {
498 jQuery('#first').load(url("data/name.html"), function() {
499 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
504 test("load(String, Function) - check scripts", function() {
508 var verifyEvaluation = function() {
509 equals( foobar, "bar", 'Check if script src was evaluated after load' );
510 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
514 jQuery('#first').load(url('data/test.html'), function() {
515 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
516 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
517 equals( testFoo, "foo", 'Check if script was evaluated after load' );
518 setTimeout(verifyEvaluation, 600);
522 test("load(String, Function) - check file with only a script tag", function() {
526 jQuery('#first').load(url('data/test2.html'), function() {
527 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
528 equals( testFoo, "foo", 'Check if script was evaluated after load' );
534 test("load(String, Object, Function)", function() {
538 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
539 var $post = jQuery(this).find('#post');
540 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
541 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
546 test("load(String, String, Function)", function() {
550 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
551 var $get = jQuery(this).find('#get');
552 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
553 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
558 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
561 jQuery.get(url('data/dashboard.xml'), function(xml) {
563 jQuery('tab', xml).each(function() {
564 content.push(jQuery(this).text());
566 equals( content[0], 'blabla', 'Check first tab');
567 equals( content[1], 'blublu', 'Check second tab');
572 test("jQuery.getScript(String, Function) - with callback", function() {
575 jQuery.getScript(url("data/test.js"), function() {
576 equals( foobar, "bar", 'Check if script was evaluated' );
577 setTimeout(start, 100);
581 test("jQuery.getScript(String, Function) - no callback", function() {
584 jQuery.getScript(url("data/test.js"), function(){
589 test("jQuery.ajax() - JSONP, Local", function() {
593 function plus(){ if ( ++count == 8 ) start(); }
598 url: "data/jsonp.php",
600 success: function(data){
601 ok( data.data, "JSON results returned (GET, no callback)" );
604 error: function(data){
605 ok( false, "Ajax error JSON (GET, no callback)" );
611 url: "data/jsonp.php?callback=?",
613 success: function(data){
614 ok( data.data, "JSON results returned (GET, url callback)" );
617 error: function(data){
618 ok( false, "Ajax error JSON (GET, url callback)" );
624 url: "data/jsonp.php",
627 success: function(data){
628 ok( data.data, "JSON results returned (GET, data callback)" );
631 error: function(data){
632 ok( false, "Ajax error JSON (GET, data callback)" );
638 url: "data/jsonp.php",
641 success: function(data){
642 ok( data.data, "JSON results returned (GET, data obj callback)" );
645 error: function(data){
646 ok( false, "Ajax error JSON (GET, data obj callback)" );
652 url: "data/jsonp.php",
654 jsonpCallback: "jsonpResults",
655 success: function(data){
656 ok( data.data, "JSON results returned (GET, custom callback name)" );
659 error: function(data){
660 ok( false, "Ajax error JSON (GET, custom callback name)" );
667 url: "data/jsonp.php",
669 success: function(data){
670 ok( data.data, "JSON results returned (POST, no callback)" );
673 error: function(data){
674 ok( false, "Ajax error JSON (GET, data obj callback)" );
681 url: "data/jsonp.php",
684 success: function(data){
685 ok( data.data, "JSON results returned (POST, data callback)" );
688 error: function(data){
689 ok( false, "Ajax error JSON (POST, data callback)" );
696 url: "data/jsonp.php",
699 success: function(data){
700 ok( data.data, "JSON results returned (POST, data obj callback)" );
703 error: function(data){
704 ok( false, "Ajax error JSON (POST, data obj callback)" );
710 test("JSONP - Custom JSONP Callback", function() {
714 window.jsonpResults = function(data) {
715 ok( data.data, "JSON results returned (GET, custom callback function)" );
720 url: "data/jsonp.php",
722 jsonpCallback: "jsonpResults"
726 test("jQuery.ajax() - JSONP, Remote", function() {
730 function plus(){ if ( ++count == 4 ) start(); }
732 var base = window.location.href.replace(/\?.*$/, "");
737 url: base + "data/jsonp.php",
739 success: function(data){
740 ok( data.data, "JSON results returned (GET, no callback)" );
743 error: function(data){
744 ok( false, "Ajax error JSON (GET, no callback)" );
750 url: base + "data/jsonp.php?callback=?",
752 success: function(data){
753 ok( data.data, "JSON results returned (GET, url callback)" );
756 error: function(data){
757 ok( false, "Ajax error JSON (GET, url callback)" );
763 url: base + "data/jsonp.php",
766 success: function(data){
767 ok( data.data, "JSON results returned (GET, data callback)" );
770 error: function(data){
771 ok( false, "Ajax error JSON (GET, data callback)" );
777 url: base + "data/jsonp.php",
780 success: function(data){
781 ok( data.data, "JSON results returned (GET, data obj callback)" );
784 error: function(data){
785 ok( false, "Ajax error JSON (GET, data obj callback)" );
791 test("jQuery.ajax() - script, Remote", function() {
794 var base = window.location.href.replace(/\?.*$/, "");
799 url: base + "data/test.js",
801 success: function(data){
802 ok( foobar, "Script results returned (GET, no callback)" );
808 test("jQuery.ajax() - script, Remote with POST", function() {
811 var base = window.location.href.replace(/\?.*$/, "");
816 url: base + "data/test.js",
819 success: function(data, status){
820 ok( foobar, "Script results returned (POST, no callback)" );
821 equals( status, "success", "Script results returned (POST, no callback)" );
824 error: function(xhr) {
825 ok( false, "ajax error, status code: " + xhr.status );
831 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
834 var base = window.location.href.replace(/\?.*$/, "");
835 base = base.replace(/^.*?\/\//, "//");
840 url: base + "data/test.js",
842 success: function(data){
843 ok( foobar, "Script results returned (GET, no callback)" );
849 test("jQuery.ajax() - malformed JSON", function() {
855 url: "data/badjson.js",
858 ok( false, "Success." );
861 error: function(xhr, msg) {
862 equals( "parsererror", msg, "A parse error occurred." );
868 test("jQuery.ajax() - script by content-type", function() {
874 url: "data/script.php",
875 data: { header: "script" },
876 success: function() {
882 test("jQuery.ajax() - json by content-type", function() {
888 url: "data/json.php",
889 data: { header: "json", json: "array" },
890 success: function( json ) {
891 ok( json.length >= 2, "Check length");
892 equals( json[0].name, 'John', 'Check JSON: first, name' );
893 equals( json[0].age, 21, 'Check JSON: first, age' );
894 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
895 equals( json[1].age, 25, 'Check JSON: second, age' );
901 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
904 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
905 ok( json.length >= 2, "Check length");
906 equals( json[0].name, 'John', 'Check JSON: first, name' );
907 equals( json[0].age, 21, 'Check JSON: first, age' );
908 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
909 equals( json[1].age, 25, 'Check JSON: second, age' );
914 test("jQuery.getJSON(String, Function) - JSON object", function() {
917 jQuery.getJSON(url("data/json.php"), function(json) {
918 if (json && json.data) {
919 equals( json.data.lang, 'en', 'Check JSON: lang' );
920 equals( json.data.length, 25, 'Check JSON: length' );
926 test("jQuery.getJSON - Using Native JSON", function() {
929 var old = window.JSON;
931 parse: function(str){
932 ok( true, "Verifying that parse method was run" );
938 jQuery.getJSON(url("data/json.php"), function(json) {
940 equals( json, true, "Verifying return value" );
945 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
948 var base = window.location.href.replace(/\?.*$/, "");
951 jQuery.getJSON(url(base + "data/json.php"), function(json) {
952 equals( json.data.lang, 'en', 'Check JSON: lang' );
953 equals( json.data.length, 25, 'Check JSON: length' );
958 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
963 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
964 jQuery('math', xml).each(function() {
965 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
966 equals( jQuery('result', this).text(), '3', 'Check for XML' );
968 if ( ++done === 2 ) start();
971 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
972 jQuery('math', xml).each(function() {
973 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
974 equals( jQuery('result', this).text(), '3', 'Check for XML' );
976 if ( ++done === 2 ) start();
980 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
985 jQuery.ajaxSetup({timeout: 1000});
987 var pass = function() {
990 ok( true, 'Check local and global callbacks after timeout' );
991 jQuery('#main').unbind("ajaxError");
996 var fail = function(a,b,c) {
997 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1001 jQuery('#main').ajaxError(pass);
1005 url: url("data/name.php?wait=5"),
1011 jQuery.ajaxSetup({timeout: 0});
1014 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1016 jQuery.ajaxSetup({timeout: 50});
1021 url: url("data/name.php?wait=1"),
1023 ok( false, 'Check for local timeout failed' );
1026 success: function() {
1027 ok( true, 'Check for local timeout' );
1033 jQuery.ajaxSetup({timeout: 0});
1036 test("jQuery.ajax - simple get", function() {
1041 url: url("data/name.php?name=foo"),
1042 success: function(msg){
1043 equals( msg, 'bar', 'Check for GET' );
1049 test("jQuery.ajax - simple post", function() {
1054 url: url("data/name.php"),
1056 success: function(msg){
1057 equals( msg, 'pan', 'Check for POST' );
1063 test("ajaxSetup()", function() {
1067 url: url("data/name.php?name=foo"),
1068 success: function(msg){
1069 equals( msg, 'bar', 'Check for GET' );
1077 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1080 url: "data/name.php?wait=1",
1082 error: function(request, status) {
1083 ok( status != null, "status shouldn't be null in error handler" );
1084 equals( "timeout", status );
1091 test("data option: evaluate function values (#2806)", function() {
1094 url: "data/echoQuery.php",
1100 success: function(result) {
1101 equals( result, "key=value" );
1107 test("jQuery.ajax - If-Modified-Since support", function() {
1112 var url = "data/if_modified_since.php?ts=" + new Date();
1117 success: function(data, status) {
1118 equals(status, "success");
1123 success: function(data, status) {
1124 if ( data === "FAIL" ) {
1125 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1126 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1128 equals(status, "notmodified");
1129 ok(data == null, "response body should be empty")
1138 test("jQuery.ajax - Etag support", function() {
1143 var url = "data/etag.php?ts=" + new Date();
1148 success: function(data, status) {
1149 equals(status, "success");
1154 success: function(data, status) {
1155 if ( data === "FAIL" ) {
1156 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1157 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1159 equals(status, "notmodified");
1160 ok(data == null, "response body should be empty")