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(".load()) - 404 error callbacks", 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" );
86 }).ajaxError(function(){
87 ok( true, "ajaxError" );
88 }).ajaxSuccess(function(){
89 ok( false, "ajaxSuccess" );
92 jQuery("<div/>").load("data/404.html", function(){
97 test("jQuery.ajax() - abort", function() {
101 jQuery('#foo').ajaxStart(function(){
102 ok( true, "ajaxStart" );
103 }).ajaxStop(function(){
104 ok( true, "ajaxStop" );
106 }).ajaxSend(function(){
107 ok( true, "ajaxSend" );
108 }).ajaxComplete(function(){
109 ok( true, "ajaxComplete" );
112 var xhr = jQuery.ajax({
113 url: url("data/name.php?wait=5"),
114 beforeSend: function(){ ok(true, "beforeSend"); },
115 complete: function(){ ok(true, "complete"); }
121 test("Ajax events with context", function() {
125 var context = document.createElement("div");
128 equals( this, context, e.type );
131 function callback(msg){
133 equals( this, context, "context is preserved on callback " + msg );
137 function nocallback(msg){
139 equals( typeof this.url, "string", "context is settings on callback " + msg );
143 jQuery('#foo').add(context)
150 url: url("data/name.html"),
151 beforeSend: callback("beforeSend"),
152 success: callback("success"),
153 error: callback("error"),
155 callback("complete").call(this);
158 url: url("data/404.html"),
160 beforeSend: callback("beforeSend"),
161 error: callback("error"),
162 complete: function(){
163 callback("complete").call(this);
165 jQuery('#foo').add(context).unbind();
168 url: url("data/404.html"),
169 beforeSend: nocallback("beforeSend"),
170 error: nocallback("error"),
171 complete: function(){
172 nocallback("complete").call(this);
183 test("jQuery.ajax() - disabled globals", function() {
187 jQuery('#foo').ajaxStart(function(){
188 ok( false, "ajaxStart" );
189 }).ajaxStop(function(){
190 ok( false, "ajaxStop" );
191 }).ajaxSend(function(){
192 ok( false, "ajaxSend" );
193 }).ajaxComplete(function(){
194 ok( false, "ajaxComplete" );
195 }).ajaxError(function(){
196 ok( false, "ajaxError" );
197 }).ajaxSuccess(function(){
198 ok( false, "ajaxSuccess" );
203 url: url("data/name.html"),
204 beforeSend: function(){ ok(true, "beforeSend"); },
205 success: function(){ ok(true, "success"); },
206 error: function(){ ok(false, "error"); },
207 complete: function(){
208 ok(true, "complete");
209 setTimeout(function(){ start(); }, 13);
214 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
218 url: url("data/with_fries.xml"),
220 success: function(resp) {
221 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
222 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
223 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
229 test("jQuery.ajax - beforeSend", function() {
235 jQuery.ajaxSetup({ timeout: 0 });
238 url: url("data/name.html"),
239 beforeSend: function(xml) {
242 success: function(data) {
243 ok( check, "check beforeSend was executed" );
249 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
251 var request = jQuery.ajax({
252 url: url("data/name.html"),
253 beforeSend: function() {
254 ok( true, "beforeSend got called, canceling" );
257 success: function() {
258 ok( false, "request didn't get canceled" );
260 complete: function() {
261 ok( false, "request didn't get canceled" );
264 ok( false, "request didn't get canceled" );
267 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
270 window.foobar = null;
271 window.testFoo = undefined;
273 test("jQuery.ajax - dataType html", function() {
277 var verifyEvaluation = function() {
278 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
279 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
286 url: url("data/test.html"),
287 success: function(data) {
288 jQuery("#ap").html(data);
289 ok( data.match(/^html text/), 'Check content for datatype html' );
290 setTimeout(verifyEvaluation, 600);
295 test("serialize()", function() {
298 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
299 jQuery("#search").after(
300 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
301 '<input type="number" id="html5number" name="number" value="43" />'
304 equals( jQuery('#form').serialize(),
305 "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",
306 'Check form serialization as query string');
308 equals( jQuery('#form :input').serialize(),
309 "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",
310 'Check input serialization as query string');
312 equals( jQuery('#testForm').serialize(),
313 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
314 'Check form serialization as query string');
316 equals( jQuery('#testForm :input').serialize(),
317 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
318 'Check input serialization as query string');
320 equals( jQuery('#form, #testForm').serialize(),
321 "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=",
322 'Multiple form serialization as query string');
324 /* Temporarily disabled. Opera 10 has problems with form serialization.
325 equals( jQuery('#form, #testForm :input').serialize(),
326 "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=",
327 'Mixed form/input serialization as query string');
329 jQuery("#html5email, #html5number").remove();
332 test("jQuery.param()", function() {
335 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
337 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
338 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
340 params = {someName: [1, 2, 3], regularThing: "blah" };
341 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
343 params = {foo: ['a', 'b', 'c']};
344 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
346 params = {foo: ["baz", 42, "All your base are belong to us"] };
347 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
349 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
350 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" );
352 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?" };
353 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" );
355 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 ] };
356 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" );
358 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?" };
359 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" );
361 jQuery.ajaxSetup({ traditional: true });
363 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
364 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
366 params = {someName: [1, 2, 3], regularThing: "blah" };
367 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
369 params = {foo: ['a', 'b', 'c']};
370 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
372 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
373 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
375 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
376 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" );
378 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?" };
379 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" );
381 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 ] };
382 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)" );
384 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?" };
385 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" );
387 params = { param1: null };
388 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
391 test("synchronous request", function() {
393 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
396 test("synchronous request with callbacks", function() {
399 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
400 ok( /^{ "data"/.test( result ), "check returned text" );
403 test("pass-through request object", function() {
407 var target = "data/name.html";
408 var successCount = 0;
411 var success = function() {
414 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
416 errorEx += ": " + xml.status;
418 jQuery("#foo").one('ajaxStop', function () {
419 equals(successCount, 5, "Check all ajax calls successful");
420 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
421 jQuery("#foo").unbind('ajaxError');
426 ok( jQuery.get(url(target), success), "get" );
427 ok( jQuery.post(url(target), success), "post" );
428 ok( jQuery.getScript(url("data/test.js"), success), "script" );
429 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
430 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
433 test("ajax cache", function () {
440 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
441 var re = /_=(.*?)(&|$)/g;
443 for (var i = 0; i < 6; i++) {
444 var ret = re.exec(s.url);
450 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
451 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
456 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
457 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
458 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
459 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
460 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
461 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
466 * The assertions expect that the passed-in object will be modified,
467 * which shouldn't be the case. Fixes #5439.
468 test("global ajaxSettings", function() {
471 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
472 var orig = { url: "data/with_fries.xml" };
475 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
477 t = jQuery.extend({}, orig);
480 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
482 t = jQuery.extend({}, orig);
483 t.data = { zoo: 'a', ping: 'b' };
485 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' }" );
487 jQuery.ajaxSettings = tmp;
491 test("load(String)", function() {
493 stop(); // check if load can be called with only url
494 jQuery('#first').load("data/name.html", start);
497 test("load('url selector')", function() {
499 stop(); // check if load can be called with only url
500 jQuery('#first').load("data/test3.html div.user", function(){
501 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
506 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
509 jQuery.ajaxSetup({ dataType: "json" });
510 jQuery("#first").ajaxComplete(function (e, xml, s) {
511 equals( s.dataType, "html", "Verify the load() dataType was html" );
512 jQuery("#first").unbind("ajaxComplete");
513 jQuery.ajaxSetup({ dataType: "" });
516 jQuery('#first').load("data/test3.html");
519 test("load(String, Function) - simple: inject text into DOM", function() {
522 jQuery('#first').load(url("data/name.html"), function() {
523 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
528 test("load(String, Function) - check scripts", function() {
532 var verifyEvaluation = function() {
533 equals( foobar, "bar", 'Check if script src was evaluated after load' );
534 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
538 jQuery('#first').load(url('data/test.html'), function() {
539 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
540 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
541 equals( testFoo, "foo", 'Check if script was evaluated after load' );
542 setTimeout(verifyEvaluation, 600);
546 test("load(String, Function) - check file with only a script tag", function() {
550 jQuery('#first').load(url('data/test2.html'), function() {
551 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
552 equals( testFoo, "foo", 'Check if script was evaluated after load' );
558 test("load(String, Object, Function)", function() {
562 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
563 var $post = jQuery(this).find('#post');
564 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
565 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
570 test("load(String, String, Function)", function() {
574 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
575 var $get = jQuery(this).find('#get');
576 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
577 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
582 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
585 jQuery.get(url('data/dashboard.xml'), function(xml) {
587 jQuery('tab', xml).each(function() {
588 content.push(jQuery(this).text());
590 equals( content[0], 'blabla', 'Check first tab');
591 equals( content[1], 'blublu', 'Check second tab');
596 test("jQuery.getScript(String, Function) - with callback", function() {
599 jQuery.getScript(url("data/test.js"), function() {
600 equals( foobar, "bar", 'Check if script was evaluated' );
601 setTimeout(start, 100);
605 test("jQuery.getScript(String, Function) - no callback", function() {
608 jQuery.getScript(url("data/test.js"), function(){
613 test("jQuery.ajax() - JSONP, Local", function() {
617 function plus(){ if ( ++count == 8 ) start(); }
622 url: "data/jsonp.php",
624 success: function(data){
625 ok( data.data, "JSON results returned (GET, no callback)" );
628 error: function(data){
629 ok( false, "Ajax error JSON (GET, no callback)" );
635 url: "data/jsonp.php?callback=?",
637 success: function(data){
638 ok( data.data, "JSON results returned (GET, url callback)" );
641 error: function(data){
642 ok( false, "Ajax error JSON (GET, url callback)" );
648 url: "data/jsonp.php",
651 success: function(data){
652 ok( data.data, "JSON results returned (GET, data callback)" );
655 error: function(data){
656 ok( false, "Ajax error JSON (GET, data callback)" );
662 url: "data/jsonp.php",
665 success: function(data){
666 ok( data.data, "JSON results returned (GET, data obj callback)" );
669 error: function(data){
670 ok( false, "Ajax error JSON (GET, data obj callback)" );
676 url: "data/jsonp.php",
678 jsonpCallback: "jsonpResults",
679 success: function(data){
680 ok( data.data, "JSON results returned (GET, custom callback name)" );
683 error: function(data){
684 ok( false, "Ajax error JSON (GET, custom callback name)" );
691 url: "data/jsonp.php",
693 success: function(data){
694 ok( data.data, "JSON results returned (POST, no callback)" );
697 error: function(data){
698 ok( false, "Ajax error JSON (GET, data obj callback)" );
705 url: "data/jsonp.php",
708 success: function(data){
709 ok( data.data, "JSON results returned (POST, data callback)" );
712 error: function(data){
713 ok( false, "Ajax error JSON (POST, data callback)" );
720 url: "data/jsonp.php",
723 success: function(data){
724 ok( data.data, "JSON results returned (POST, data obj callback)" );
727 error: function(data){
728 ok( false, "Ajax error JSON (POST, data obj callback)" );
734 test("JSONP - Custom JSONP Callback", function() {
738 window.jsonpResults = function(data) {
739 ok( data.data, "JSON results returned (GET, custom callback function)" );
744 url: "data/jsonp.php",
746 jsonpCallback: "jsonpResults"
750 test("jQuery.ajax() - JSONP, Remote", function() {
754 function plus(){ if ( ++count == 4 ) start(); }
756 var base = window.location.href.replace(/\?.*$/, "");
761 url: base + "data/jsonp.php",
763 success: function(data){
764 ok( data.data, "JSON results returned (GET, no callback)" );
767 error: function(data){
768 ok( false, "Ajax error JSON (GET, no callback)" );
774 url: base + "data/jsonp.php?callback=?",
776 success: function(data){
777 ok( data.data, "JSON results returned (GET, url callback)" );
780 error: function(data){
781 ok( false, "Ajax error JSON (GET, url callback)" );
787 url: base + "data/jsonp.php",
790 success: function(data){
791 ok( data.data, "JSON results returned (GET, data callback)" );
794 error: function(data){
795 ok( false, "Ajax error JSON (GET, data callback)" );
801 url: base + "data/jsonp.php",
804 success: function(data){
805 ok( data.data, "JSON results returned (GET, data obj callback)" );
808 error: function(data){
809 ok( false, "Ajax error JSON (GET, data obj callback)" );
815 test("jQuery.ajax() - script, Remote", function() {
818 var base = window.location.href.replace(/\?.*$/, "");
823 url: base + "data/test.js",
825 success: function(data){
826 ok( foobar, "Script results returned (GET, no callback)" );
832 test("jQuery.ajax() - script, Remote with POST", function() {
835 var base = window.location.href.replace(/\?.*$/, "");
840 url: base + "data/test.js",
843 success: function(data, status){
844 ok( foobar, "Script results returned (POST, no callback)" );
845 equals( status, "success", "Script results returned (POST, no callback)" );
848 error: function(xhr) {
849 ok( false, "ajax error, status code: " + xhr.status );
855 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
858 var base = window.location.href.replace(/\?.*$/, "");
859 base = base.replace(/^.*?\/\//, "//");
864 url: base + "data/test.js",
866 success: function(data){
867 ok( foobar, "Script results returned (GET, no callback)" );
873 test("jQuery.ajax() - malformed JSON", function() {
879 url: "data/badjson.js",
882 ok( false, "Success." );
885 error: function(xhr, msg) {
886 equals( "parsererror", msg, "A parse error occurred." );
892 test("jQuery.ajax() - script by content-type", function() {
898 url: "data/script.php",
899 data: { header: "script" },
900 success: function() {
906 test("jQuery.ajax() - json by content-type", function() {
912 url: "data/json.php",
913 data: { header: "json", json: "array" },
914 success: function( json ) {
915 ok( json.length >= 2, "Check length");
916 equals( json[0].name, 'John', 'Check JSON: first, name' );
917 equals( json[0].age, 21, 'Check JSON: first, age' );
918 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
919 equals( json[1].age, 25, 'Check JSON: second, age' );
925 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
928 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
929 ok( json.length >= 2, "Check length");
930 equals( json[0].name, 'John', 'Check JSON: first, name' );
931 equals( json[0].age, 21, 'Check JSON: first, age' );
932 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
933 equals( json[1].age, 25, 'Check JSON: second, age' );
938 test("jQuery.getJSON(String, Function) - JSON object", function() {
941 jQuery.getJSON(url("data/json.php"), function(json) {
942 if (json && json.data) {
943 equals( json.data.lang, 'en', 'Check JSON: lang' );
944 equals( json.data.length, 25, 'Check JSON: length' );
950 test("jQuery.getJSON - Using Native JSON", function() {
953 var old = window.JSON;
955 parse: function(str){
956 ok( true, "Verifying that parse method was run" );
962 jQuery.getJSON(url("data/json.php"), function(json) {
964 equals( json, true, "Verifying return value" );
969 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
972 var base = window.location.href.replace(/\?.*$/, "");
975 jQuery.getJSON(url(base + "data/json.php"), function(json) {
976 equals( json.data.lang, 'en', 'Check JSON: lang' );
977 equals( json.data.length, 25, 'Check JSON: length' );
982 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
987 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
988 jQuery('math', xml).each(function() {
989 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
990 equals( jQuery('result', this).text(), '3', 'Check for XML' );
992 if ( ++done === 2 ) start();
995 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
996 jQuery('math', xml).each(function() {
997 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
998 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1000 if ( ++done === 2 ) start();
1004 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1009 jQuery.ajaxSetup({timeout: 1000});
1011 var pass = function() {
1013 if ( passed == 2 ) {
1014 ok( true, 'Check local and global callbacks after timeout' );
1015 jQuery('#main').unbind("ajaxError");
1020 var fail = function(a,b,c) {
1021 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1025 jQuery('#main').ajaxError(pass);
1029 url: url("data/name.php?wait=5"),
1035 jQuery.ajaxSetup({timeout: 0});
1038 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1040 jQuery.ajaxSetup({timeout: 50});
1045 url: url("data/name.php?wait=1"),
1047 ok( false, 'Check for local timeout failed' );
1050 success: function() {
1051 ok( true, 'Check for local timeout' );
1057 jQuery.ajaxSetup({timeout: 0});
1060 test("jQuery.ajax - simple get", function() {
1065 url: url("data/name.php?name=foo"),
1066 success: function(msg){
1067 equals( msg, 'bar', 'Check for GET' );
1073 test("jQuery.ajax - simple post", function() {
1078 url: url("data/name.php"),
1080 success: function(msg){
1081 equals( msg, 'pan', 'Check for POST' );
1087 test("ajaxSetup()", function() {
1091 url: url("data/name.php?name=foo"),
1092 success: function(msg){
1093 equals( msg, 'bar', 'Check for GET' );
1101 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1104 url: "data/name.php?wait=1",
1106 error: function(request, status) {
1107 ok( status != null, "status shouldn't be null in error handler" );
1108 equals( "timeout", status );
1115 test("data option: evaluate function values (#2806)", function() {
1118 url: "data/echoQuery.php",
1124 success: function(result) {
1125 equals( result, "key=value" );
1131 test("jQuery.ajax - If-Modified-Since support", function() {
1136 var url = "data/if_modified_since.php?ts=" + new Date();
1141 success: function(data, status) {
1142 equals(status, "success");
1147 success: function(data, status) {
1148 if ( data === "FAIL" ) {
1149 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1150 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1152 equals(status, "notmodified");
1153 ok(data == null, "response body should be empty")
1162 test("jQuery.ajax - Etag support", function() {
1167 var url = "data/etag.php?ts=" + new Date();
1172 success: function(data, status) {
1173 equals(status, "success");
1178 success: function(data, status) {
1179 if ( data === "FAIL" ) {
1180 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1181 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1183 equals(status, "notmodified");
1184 ok(data == null, "response body should be empty")