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(".load()) - 404 error callbacks", function() {
75 jQuery('#foo').ajaxStart(function(){
76 ok( true, "ajaxStart" );
77 }).ajaxStop(function(){
78 ok( true, "ajaxStop" );
80 }).ajaxSend(function(){
81 ok( true, "ajaxSend" );
82 }).ajaxComplete(function(){
83 ok( true, "ajaxComplete" );
84 }).ajaxError(function(){
85 ok( true, "ajaxError" );
86 }).ajaxSuccess(function(){
87 ok( false, "ajaxSuccess" );
90 jQuery("<div/>").load("data/404.html", function(){
95 test("jQuery.ajax() - abort", function() {
99 jQuery('#foo').ajaxStart(function(){
100 ok( true, "ajaxStart" );
101 }).ajaxStop(function(){
102 ok( true, "ajaxStop" );
104 }).ajaxSend(function(){
105 ok( true, "ajaxSend" );
106 }).ajaxComplete(function(){
107 ok( true, "ajaxComplete" );
110 var xhr = jQuery.ajax({
111 url: url("data/name.php?wait=5"),
112 beforeSend: function(){ ok(true, "beforeSend"); },
113 complete: function(){ ok(true, "complete"); }
119 test("Ajax events with context", function() {
123 var context = document.createElement("div");
126 equals( this, context, e.type );
129 function callback(msg){
131 equals( this, context, "context is preserved on callback " + msg );
135 function nocallback(msg){
137 equals( typeof this.url, "string", "context is settings on callback " + msg );
141 jQuery('#foo').add(context)
148 url: url("data/name.html"),
149 beforeSend: callback("beforeSend"),
150 success: callback("success"),
151 error: callback("error"),
153 callback("complete").call(this);
156 url: url("data/404.html"),
158 beforeSend: callback("beforeSend"),
159 error: callback("error"),
160 complete: function(){
161 callback("complete").call(this);
163 jQuery('#foo').add(context).unbind();
166 url: url("data/404.html"),
167 beforeSend: nocallback("beforeSend"),
168 error: nocallback("error"),
169 complete: function(){
170 nocallback("complete").call(this);
181 test("jQuery.ajax context modification", function() {
189 url: url("data/name.html"),
191 beforeSend: function(){
194 complete: function() {
199 equals( obj.test, "foo", "Make sure the original object is maintained." );
202 test("jQuery.ajax() - disabled globals", function() {
206 jQuery('#foo').ajaxStart(function(){
207 ok( false, "ajaxStart" );
208 }).ajaxStop(function(){
209 ok( false, "ajaxStop" );
210 }).ajaxSend(function(){
211 ok( false, "ajaxSend" );
212 }).ajaxComplete(function(){
213 ok( false, "ajaxComplete" );
214 }).ajaxError(function(){
215 ok( false, "ajaxError" );
216 }).ajaxSuccess(function(){
217 ok( false, "ajaxSuccess" );
222 url: url("data/name.html"),
223 beforeSend: function(){ ok(true, "beforeSend"); },
224 success: function(){ ok(true, "success"); },
225 error: function(){ ok(false, "error"); },
226 complete: function(){
227 ok(true, "complete");
228 setTimeout(function(){ start(); }, 13);
233 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
237 url: url("data/with_fries.xml"),
239 success: function(resp) {
240 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
241 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
242 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
248 test("jQuery.ajax - beforeSend", function() {
254 jQuery.ajaxSetup({ timeout: 0 });
257 url: url("data/name.html"),
258 beforeSend: function(xml) {
261 success: function(data) {
262 ok( check, "check beforeSend was executed" );
268 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
270 var request = jQuery.ajax({
271 url: url("data/name.html"),
272 beforeSend: function() {
273 ok( true, "beforeSend got called, canceling" );
276 success: function() {
277 ok( false, "request didn't get canceled" );
279 complete: function() {
280 ok( false, "request didn't get canceled" );
283 ok( false, "request didn't get canceled" );
286 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
289 window.foobar = null;
290 window.testFoo = undefined;
292 test("jQuery.ajax - dataType html", function() {
296 var verifyEvaluation = function() {
297 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
298 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
305 url: url("data/test.html"),
306 success: function(data) {
307 jQuery("#ap").html(data);
308 ok( data.match(/^html text/), 'Check content for datatype html' );
309 setTimeout(verifyEvaluation, 600);
314 test("serialize()", function() {
317 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
318 jQuery("#search").after(
319 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
320 '<input type="number" id="html5number" name="number" value="43" />'
323 equals( jQuery('#form').serialize(),
324 "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",
325 'Check form serialization as query string');
327 equals( jQuery('#form :input').serialize(),
328 "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",
329 'Check input serialization as query string');
331 equals( jQuery('#testForm').serialize(),
332 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
333 'Check form serialization as query string');
335 equals( jQuery('#testForm :input').serialize(),
336 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
337 'Check input serialization as query string');
339 equals( jQuery('#form, #testForm').serialize(),
340 "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&search=search&email=dave%40jquery.com&number=43&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
341 'Multiple form serialization as query string');
343 /* Temporarily disabled. Opera 10 has problems with form serialization.
344 equals( jQuery('#form, #testForm :input').serialize(),
345 "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=",
346 'Mixed form/input serialization as query string');
348 jQuery("#html5email, #html5number").remove();
351 test("jQuery.param()", function() {
354 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
356 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
357 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
359 params = {someName: [1, 2, 3], regularThing: "blah" };
360 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
362 params = {foo: ['a', 'b', 'c']};
363 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
365 params = {foo: ["baz", 42, "All your base are belong to us"] };
366 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
368 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
369 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" );
371 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?" };
372 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" );
374 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 ] };
375 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" );
377 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?" };
378 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" );
380 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." );
382 jQuery.ajaxSetup({ traditional: true });
384 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
385 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
387 params = {someName: [1, 2, 3], regularThing: "blah" };
388 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
390 params = {foo: ['a', 'b', 'c']};
391 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
393 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
394 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
396 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
397 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" );
399 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?" };
400 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" );
402 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 ] };
403 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)" );
405 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?" };
406 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" );
408 params = { param1: null };
409 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
412 test("synchronous request", function() {
414 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
417 test("synchronous request with callbacks", function() {
420 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
421 ok( /^{ "data"/.test( result ), "check returned text" );
424 test("pass-through request object", function() {
428 var target = "data/name.html";
429 var successCount = 0;
432 var success = function() {
435 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
437 errorEx += ": " + xml.status;
439 jQuery("#foo").one('ajaxStop', function () {
440 equals(successCount, 5, "Check all ajax calls successful");
441 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
442 jQuery("#foo").unbind('ajaxError');
447 ok( jQuery.get(url(target), success), "get" );
448 ok( jQuery.post(url(target), success), "post" );
449 ok( jQuery.getScript(url("data/test.js"), success), "script" );
450 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
451 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
454 test("ajax cache", function () {
461 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
462 var re = /_=(.*?)(&|$)/g;
464 for (var i = 0; i < 6; i++) {
465 var ret = re.exec(s.url);
471 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
472 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
477 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
478 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
479 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
480 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
481 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
482 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
487 * The assertions expect that the passed-in object will be modified,
488 * which shouldn't be the case. Fixes #5439.
489 test("global ajaxSettings", function() {
492 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
493 var orig = { url: "data/with_fries.xml" };
496 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
498 t = jQuery.extend({}, orig);
501 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
503 t = jQuery.extend({}, orig);
504 t.data = { zoo: 'a', ping: 'b' };
506 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' }" );
508 jQuery.ajaxSettings = tmp;
512 test("load(String)", function() {
514 stop(); // check if load can be called with only url
515 jQuery('#first').load("data/name.html", start);
518 test("load('url selector')", function() {
520 stop(); // check if load can be called with only url
521 jQuery('#first').load("data/test3.html div.user", function(){
522 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
527 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
530 jQuery.ajaxSetup({ dataType: "json" });
531 jQuery("#first").ajaxComplete(function (e, xml, s) {
532 equals( s.dataType, "html", "Verify the load() dataType was html" );
533 jQuery("#first").unbind("ajaxComplete");
534 jQuery.ajaxSetup({ dataType: "" });
537 jQuery('#first').load("data/test3.html");
540 test("load(String, Function) - simple: inject text into DOM", function() {
543 jQuery('#first').load(url("data/name.html"), function() {
544 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
549 test("load(String, Function) - check scripts", function() {
553 var verifyEvaluation = function() {
554 equals( foobar, "bar", 'Check if script src was evaluated after load' );
555 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
559 jQuery('#first').load(url('data/test.html'), function() {
560 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
561 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
562 equals( testFoo, "foo", 'Check if script was evaluated after load' );
563 setTimeout(verifyEvaluation, 600);
567 test("load(String, Function) - check file with only a script tag", function() {
571 jQuery('#first').load(url('data/test2.html'), function() {
572 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
573 equals( testFoo, "foo", 'Check if script was evaluated after load' );
579 test("load(String, Object, Function)", function() {
583 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
584 var $post = jQuery(this).find('#post');
585 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
586 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
591 test("load(String, String, Function)", function() {
595 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
596 var $get = jQuery(this).find('#get');
597 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
598 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
603 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
606 jQuery.get(url('data/dashboard.xml'), function(xml) {
608 jQuery('tab', xml).each(function() {
609 content.push(jQuery(this).text());
611 equals( content[0], 'blabla', 'Check first tab');
612 equals( content[1], 'blublu', 'Check second tab');
617 test("jQuery.getScript(String, Function) - with callback", function() {
620 jQuery.getScript(url("data/test.js"), function() {
621 equals( foobar, "bar", 'Check if script was evaluated' );
622 setTimeout(start, 100);
626 test("jQuery.getScript(String, Function) - no callback", function() {
629 jQuery.getScript(url("data/test.js"), function(){
634 test("jQuery.ajax() - JSONP, Local", function() {
638 function plus(){ if ( ++count == 8 ) start(); }
643 url: "data/jsonp.php",
645 success: function(data){
646 ok( data.data, "JSON results returned (GET, no callback)" );
649 error: function(data){
650 ok( false, "Ajax error JSON (GET, no callback)" );
656 url: "data/jsonp.php?callback=?",
658 success: function(data){
659 ok( data.data, "JSON results returned (GET, url callback)" );
662 error: function(data){
663 ok( false, "Ajax error JSON (GET, url callback)" );
669 url: "data/jsonp.php",
672 success: function(data){
673 ok( data.data, "JSON results returned (GET, data callback)" );
676 error: function(data){
677 ok( false, "Ajax error JSON (GET, data callback)" );
683 url: "data/jsonp.php",
686 success: function(data){
687 ok( data.data, "JSON results returned (GET, data obj callback)" );
690 error: function(data){
691 ok( false, "Ajax error JSON (GET, data obj callback)" );
697 url: "data/jsonp.php",
699 jsonpCallback: "jsonpResults",
700 success: function(data){
701 ok( data.data, "JSON results returned (GET, custom callback name)" );
704 error: function(data){
705 ok( false, "Ajax error JSON (GET, custom callback name)" );
712 url: "data/jsonp.php",
714 success: function(data){
715 ok( data.data, "JSON results returned (POST, no callback)" );
718 error: function(data){
719 ok( false, "Ajax error JSON (GET, data obj callback)" );
726 url: "data/jsonp.php",
729 success: function(data){
730 ok( data.data, "JSON results returned (POST, data callback)" );
733 error: function(data){
734 ok( false, "Ajax error JSON (POST, data callback)" );
741 url: "data/jsonp.php",
744 success: function(data){
745 ok( data.data, "JSON results returned (POST, data obj callback)" );
748 error: function(data){
749 ok( false, "Ajax error JSON (POST, data obj callback)" );
755 test("JSONP - Custom JSONP Callback", function() {
759 window.jsonpResults = function(data) {
760 ok( data.data, "JSON results returned (GET, custom callback function)" );
765 url: "data/jsonp.php",
767 jsonpCallback: "jsonpResults"
771 test("jQuery.ajax() - JSONP, Remote", function() {
775 function plus(){ if ( ++count == 4 ) start(); }
777 var base = window.location.href.replace(/[^\/]*$/, "");
782 url: base + "data/jsonp.php",
784 success: function(data){
785 ok( data.data, "JSON results returned (GET, no callback)" );
788 error: function(data){
789 ok( false, "Ajax error JSON (GET, no callback)" );
795 url: base + "data/jsonp.php?callback=?",
797 success: function(data){
798 ok( data.data, "JSON results returned (GET, url callback)" );
801 error: function(data){
802 ok( false, "Ajax error JSON (GET, url callback)" );
808 url: base + "data/jsonp.php",
811 success: function(data){
812 ok( data.data, "JSON results returned (GET, data callback)" );
815 error: function(data){
816 ok( false, "Ajax error JSON (GET, data callback)" );
822 url: base + "data/jsonp.php",
825 success: function(data){
826 ok( data.data, "JSON results returned (GET, data obj callback)" );
829 error: function(data){
830 ok( false, "Ajax error JSON (GET, data obj callback)" );
836 test("jQuery.ajax() - script, Remote", function() {
839 var base = window.location.href.replace(/[^\/]*$/, "");
844 url: base + "data/test.js",
846 success: function(data){
847 ok( foobar, "Script results returned (GET, no callback)" );
853 test("jQuery.ajax() - script, Remote with POST", function() {
856 var base = window.location.href.replace(/[^\/]*$/, "");
861 url: base + "data/test.js",
864 success: function(data, status){
865 ok( foobar, "Script results returned (POST, no callback)" );
866 equals( status, "success", "Script results returned (POST, no callback)" );
869 error: function(xhr) {
870 ok( false, "ajax error, status code: " + xhr.status );
876 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
879 var base = window.location.href.replace(/[^\/]*$/, "");
880 base = base.replace(/^.*?\/\//, "//");
885 url: base + "data/test.js",
887 success: function(data){
888 ok( foobar, "Script results returned (GET, no callback)" );
894 test("jQuery.ajax() - malformed JSON", function() {
900 url: "data/badjson.js",
903 ok( false, "Success." );
906 error: function(xhr, msg, detailedMsg) {
907 equals( "parsererror", msg, "A parse error occurred." );
908 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
914 test("jQuery.ajax() - script by content-type", function() {
920 url: "data/script.php",
921 data: { header: "script" },
922 success: function() {
928 test("jQuery.ajax() - json by content-type", function() {
934 url: "data/json.php",
935 data: { header: "json", json: "array" },
936 success: function( json ) {
937 ok( json.length >= 2, "Check length");
938 equals( json[0].name, 'John', 'Check JSON: first, name' );
939 equals( json[0].age, 21, 'Check JSON: first, age' );
940 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
941 equals( json[1].age, 25, 'Check JSON: second, age' );
947 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
950 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
951 ok( json.length >= 2, "Check length");
952 equals( json[0].name, 'John', 'Check JSON: first, name' );
953 equals( json[0].age, 21, 'Check JSON: first, age' );
954 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
955 equals( json[1].age, 25, 'Check JSON: second, age' );
960 test("jQuery.getJSON(String, Function) - JSON object", function() {
963 jQuery.getJSON(url("data/json.php"), function(json) {
964 if (json && json.data) {
965 equals( json.data.lang, 'en', 'Check JSON: lang' );
966 equals( json.data.length, 25, 'Check JSON: length' );
972 test("jQuery.getJSON - Using Native JSON", function() {
975 var old = window.JSON;
977 parse: function(str){
978 ok( true, "Verifying that parse method was run" );
984 jQuery.getJSON(url("data/json.php"), function(json) {
986 equals( json, true, "Verifying return value" );
991 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
994 var base = window.location.href.replace(/[^\/]*$/, "");
997 jQuery.getJSON(url(base + "data/json.php"), function(json) {
998 equals( json.data.lang, 'en', 'Check JSON: lang' );
999 equals( json.data.length, 25, 'Check JSON: length' );
1004 test("jQuery.post - data", function() {
1008 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1009 jQuery('math', xml).each(function() {
1010 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1011 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1017 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1022 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1023 jQuery('math', xml).each(function() {
1024 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1025 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1027 if ( ++done === 2 ) start();
1030 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1031 jQuery('math', xml).each(function() {
1032 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1033 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1035 if ( ++done === 2 ) start();
1039 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1044 jQuery.ajaxSetup({timeout: 1000});
1046 var pass = function() {
1048 if ( passed == 2 ) {
1049 ok( true, 'Check local and global callbacks after timeout' );
1050 jQuery('#main').unbind("ajaxError");
1055 var fail = function(a,b,c) {
1056 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1060 jQuery('#main').ajaxError(pass);
1064 url: url("data/name.php?wait=5"),
1070 jQuery.ajaxSetup({timeout: 0});
1073 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1075 jQuery.ajaxSetup({timeout: 50});
1080 url: url("data/name.php?wait=1"),
1082 ok( false, 'Check for local timeout failed' );
1085 success: function() {
1086 ok( true, 'Check for local timeout' );
1092 jQuery.ajaxSetup({timeout: 0});
1095 test("jQuery.ajax - simple get", function() {
1100 url: url("data/name.php?name=foo"),
1101 success: function(msg){
1102 equals( msg, 'bar', 'Check for GET' );
1108 test("jQuery.ajax - simple post", function() {
1113 url: url("data/name.php"),
1115 success: function(msg){
1116 equals( msg, 'pan', 'Check for POST' );
1122 test("ajaxSetup()", function() {
1126 url: url("data/name.php?name=foo"),
1127 success: function(msg){
1128 equals( msg, 'bar', 'Check for GET' );
1136 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1139 url: "data/name.php?wait=1",
1141 error: function(request, status) {
1142 ok( status != null, "status shouldn't be null in error handler" );
1143 equals( "timeout", status );
1150 test("data option: evaluate function values (#2806)", function() {
1153 url: "data/echoQuery.php",
1159 success: function(result) {
1160 equals( result, "key=value" );
1166 test("data option: empty bodies for non-GET requests", function() {
1169 url: "data/echoData.php",
1172 success: function(result) {
1173 equals( result, "" );
1179 test("jQuery.ajax - If-Modified-Since support", function() {
1184 var url = "data/if_modified_since.php?ts=" + new Date();
1189 success: function(data, status) {
1190 equals(status, "success");
1195 success: function(data, status) {
1196 if ( data === "FAIL" ) {
1197 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1198 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1200 equals(status, "notmodified");
1201 ok(data == null, "response body should be empty")
1210 test("jQuery.ajax - Etag support", function() {
1215 var url = "data/etag.php?ts=" + new Date();
1220 success: function(data, status) {
1221 equals(status, "success");
1226 success: function(data, status) {
1227 if ( data === "FAIL" ) {
1228 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1229 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1231 equals(status, "notmodified");
1232 ok(data == null, "response body should be empty")
1242 test("jQuery.ajax - active counter", function() {
1243 ok( jQuery.ajax.active == 0, "ajax active counter should be zero: " + jQuery.ajax.active );