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 context modification", function() {
191 url: url("data/name.html"),
193 beforeSend: function(){
196 complete: function() {
201 equals( obj.test, "foo", "Make sure the original object is maintained." );
204 test("jQuery.ajax() - disabled globals", function() {
208 jQuery('#foo').ajaxStart(function(){
209 ok( false, "ajaxStart" );
210 }).ajaxStop(function(){
211 ok( false, "ajaxStop" );
212 }).ajaxSend(function(){
213 ok( false, "ajaxSend" );
214 }).ajaxComplete(function(){
215 ok( false, "ajaxComplete" );
216 }).ajaxError(function(){
217 ok( false, "ajaxError" );
218 }).ajaxSuccess(function(){
219 ok( false, "ajaxSuccess" );
224 url: url("data/name.html"),
225 beforeSend: function(){ ok(true, "beforeSend"); },
226 success: function(){ ok(true, "success"); },
227 error: function(){ ok(false, "error"); },
228 complete: function(){
229 ok(true, "complete");
230 setTimeout(function(){ start(); }, 13);
235 test("jQuery.ajax - xml: non-namespace elements inside namespaced elements", function() {
239 url: url("data/with_fries.xml"),
241 success: function(resp) {
242 equals( jQuery("properties", resp).length, 1, 'properties in responseXML' );
243 equals( jQuery("jsconf", resp).length, 1, 'jsconf in responseXML' );
244 equals( jQuery("thing", resp).length, 2, 'things in responseXML' );
250 test("jQuery.ajax - beforeSend", function() {
256 jQuery.ajaxSetup({ timeout: 0 });
259 url: url("data/name.html"),
260 beforeSend: function(xml) {
263 success: function(data) {
264 ok( check, "check beforeSend was executed" );
270 test("jQuery.ajax - beforeSend, cancel request (#2688)", function() {
272 var request = jQuery.ajax({
273 url: url("data/name.html"),
274 beforeSend: function() {
275 ok( true, "beforeSend got called, canceling" );
278 success: function() {
279 ok( false, "request didn't get canceled" );
281 complete: function() {
282 ok( false, "request didn't get canceled" );
285 ok( false, "request didn't get canceled" );
288 ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" );
291 window.foobar = null;
292 window.testFoo = undefined;
294 test("jQuery.ajax - dataType html", function() {
298 var verifyEvaluation = function() {
299 equals( testFoo, "foo", 'Check if script was evaluated for datatype html' );
300 equals( foobar, "bar", 'Check if script src was evaluated for datatype html' );
307 url: url("data/test.html"),
308 success: function(data) {
309 jQuery("#ap").html(data);
310 ok( data.match(/^html text/), 'Check content for datatype html' );
311 setTimeout(verifyEvaluation, 600);
316 test("serialize()", function() {
319 // Add html5 elements only for serialize because selector can't yet find them on non-html5 browsers
320 jQuery("#search").after(
321 '<input type="email" id="html5email" name="email" value="dave@jquery.com" />'+
322 '<input type="number" id="html5number" name="number" value="43" />'
325 equals( jQuery('#form').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",
327 'Check form serialization as query string');
329 equals( jQuery('#form :input').serialize(),
330 "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",
331 'Check input serialization as query string');
333 equals( jQuery('#testForm').serialize(),
334 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
335 'Check form serialization as query string');
337 equals( jQuery('#testForm :input').serialize(),
338 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
339 'Check input serialization as query string');
341 equals( jQuery('#form, #testForm').serialize(),
342 "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=",
343 'Multiple form serialization as query string');
345 /* Temporarily disabled. Opera 10 has problems with form serialization.
346 equals( jQuery('#form, #testForm :input').serialize(),
347 "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=",
348 'Mixed form/input serialization as query string');
350 jQuery("#html5email, #html5number").remove();
353 test("jQuery.param()", function() {
356 equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
358 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
359 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
361 params = {someName: [1, 2, 3], regularThing: "blah" };
362 equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3®ularThing=blah", "with array" );
364 params = {foo: ['a', 'b', 'c']};
365 equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
367 params = {foo: ["baz", 42, "All your base are belong to us"] };
368 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
370 params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
371 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" );
373 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?" };
374 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" );
376 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 ] };
377 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" );
379 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?" };
380 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" );
382 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." );
384 jQuery.ajaxSetup({ traditional: true });
386 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
387 equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
389 params = {someName: [1, 2, 3], regularThing: "blah" };
390 equals( jQuery.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
392 params = {foo: ['a', 'b', 'c']};
393 equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
395 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
396 equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
398 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
399 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" );
401 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?" };
402 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" );
404 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 ] };
405 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)" );
407 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?" };
408 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" );
410 params = { param1: null };
411 equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
414 test("synchronous request", function() {
416 ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
419 test("synchronous request with callbacks", function() {
422 jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
423 ok( /^{ "data"/.test( result ), "check returned text" );
426 test("pass-through request object", function() {
430 var target = "data/name.html";
431 var successCount = 0;
434 var success = function() {
437 jQuery("#foo").ajaxError(function (e, xml, s, ex) {
439 errorEx += ": " + xml.status;
441 jQuery("#foo").one('ajaxStop', function () {
442 equals(successCount, 5, "Check all ajax calls successful");
443 equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")");
444 jQuery("#foo").unbind('ajaxError');
449 ok( jQuery.get(url(target), success), "get" );
450 ok( jQuery.post(url(target), success), "post" );
451 ok( jQuery.getScript(url("data/test.js"), success), "script" );
452 ok( jQuery.getJSON(url("data/json_obj.js"), success), "json" );
453 ok( jQuery.ajax({url: url(target), success: success}), "generic" );
456 test("ajax cache", function () {
463 jQuery("#firstp").bind("ajaxSuccess", function (e, xml, s) {
464 var re = /_=(.*?)(&|$)/g;
466 for (var i = 0; i < 6; i++) {
467 var ret = re.exec(s.url);
473 equals(i, 1, "Test to make sure only one 'no-cache' parameter is there");
474 ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced");
479 ok( jQuery.ajax({url: "data/text.php", cache:false}), "test with no parameters" );
480 ok( jQuery.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" );
481 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" );
482 ok( jQuery.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" );
483 ok( jQuery.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" );
484 ok( jQuery.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" );
489 * The assertions expect that the passed-in object will be modified,
490 * which shouldn't be the case. Fixes #5439.
491 test("global ajaxSettings", function() {
494 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
495 var orig = { url: "data/with_fries.xml" };
498 jQuery.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
500 t = jQuery.extend({}, orig);
503 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
505 t = jQuery.extend({}, orig);
506 t.data = { zoo: 'a', ping: 'b' };
508 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' }" );
510 jQuery.ajaxSettings = tmp;
514 test("load(String)", function() {
516 stop(); // check if load can be called with only url
517 jQuery('#first').load("data/name.html", start);
520 test("load('url selector')", function() {
522 stop(); // check if load can be called with only url
523 jQuery('#first').load("data/test3.html div.user", function(){
524 equals( jQuery(this).children("div").length, 2, "Verify that specific elements were injected" );
529 test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() {
532 jQuery.ajaxSetup({ dataType: "json" });
533 jQuery("#first").ajaxComplete(function (e, xml, s) {
534 equals( s.dataType, "html", "Verify the load() dataType was html" );
535 jQuery("#first").unbind("ajaxComplete");
536 jQuery.ajaxSetup({ dataType: "" });
539 jQuery('#first').load("data/test3.html");
542 test("load(String, Function) - simple: inject text into DOM", function() {
545 jQuery('#first').load(url("data/name.html"), function() {
546 ok( /^ERROR/.test(jQuery('#first').text()), 'Check if content was injected into the DOM' );
551 test("load(String, Function) - check scripts", function() {
555 var verifyEvaluation = function() {
556 equals( foobar, "bar", 'Check if script src was evaluated after load' );
557 equals( jQuery('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
561 jQuery('#first').load(url('data/test.html'), function() {
562 ok( jQuery('#first').html().match(/^html text/), 'Check content after loading html' );
563 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
564 equals( testFoo, "foo", 'Check if script was evaluated after load' );
565 setTimeout(verifyEvaluation, 600);
569 test("load(String, Function) - check file with only a script tag", function() {
573 jQuery('#first').load(url('data/test2.html'), function() {
574 equals( jQuery('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
575 equals( testFoo, "foo", 'Check if script was evaluated after load' );
581 test("load(String, Object, Function)", function() {
585 jQuery('<div />').load(url('data/params_html.php'), { foo:3, bar:'ok' }, function() {
586 var $post = jQuery(this).find('#post');
587 equals( $post.find('#foo').text(), '3', 'Check if a hash of data is passed correctly');
588 equals( $post.find('#bar').text(), 'ok', 'Check if a hash of data is passed correctly');
593 test("load(String, String, Function)", function() {
597 jQuery('<div />').load(url('data/params_html.php'), 'foo=3&bar=ok', function() {
598 var $get = jQuery(this).find('#get');
599 equals( $get.find('#foo').text(), '3', 'Check if a string of data is passed correctly');
600 equals( $get.find('#bar').text(), 'ok', 'Check if a of data is passed correctly');
605 test("jQuery.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
608 jQuery.get(url('data/dashboard.xml'), function(xml) {
610 jQuery('tab', xml).each(function() {
611 content.push(jQuery(this).text());
613 equals( content[0], 'blabla', 'Check first tab');
614 equals( content[1], 'blublu', 'Check second tab');
619 test("jQuery.getScript(String, Function) - with callback", function() {
622 jQuery.getScript(url("data/test.js"), function() {
623 equals( foobar, "bar", 'Check if script was evaluated' );
624 setTimeout(start, 100);
628 test("jQuery.getScript(String, Function) - no callback", function() {
631 jQuery.getScript(url("data/test.js"), function(){
636 test("jQuery.ajax() - JSONP, Local", function() {
640 function plus(){ if ( ++count == 8 ) start(); }
645 url: "data/jsonp.php",
647 success: function(data){
648 ok( data.data, "JSON results returned (GET, no callback)" );
651 error: function(data){
652 ok( false, "Ajax error JSON (GET, no callback)" );
658 url: "data/jsonp.php?callback=?",
660 success: function(data){
661 ok( data.data, "JSON results returned (GET, url callback)" );
664 error: function(data){
665 ok( false, "Ajax error JSON (GET, url callback)" );
671 url: "data/jsonp.php",
674 success: function(data){
675 ok( data.data, "JSON results returned (GET, data callback)" );
678 error: function(data){
679 ok( false, "Ajax error JSON (GET, data callback)" );
685 url: "data/jsonp.php",
688 success: function(data){
689 ok( data.data, "JSON results returned (GET, data obj callback)" );
692 error: function(data){
693 ok( false, "Ajax error JSON (GET, data obj callback)" );
699 url: "data/jsonp.php",
701 jsonpCallback: "jsonpResults",
702 success: function(data){
703 ok( data.data, "JSON results returned (GET, custom callback name)" );
706 error: function(data){
707 ok( false, "Ajax error JSON (GET, custom callback name)" );
714 url: "data/jsonp.php",
716 success: function(data){
717 ok( data.data, "JSON results returned (POST, no callback)" );
720 error: function(data){
721 ok( false, "Ajax error JSON (GET, data obj callback)" );
728 url: "data/jsonp.php",
731 success: function(data){
732 ok( data.data, "JSON results returned (POST, data callback)" );
735 error: function(data){
736 ok( false, "Ajax error JSON (POST, data callback)" );
743 url: "data/jsonp.php",
746 success: function(data){
747 ok( data.data, "JSON results returned (POST, data obj callback)" );
750 error: function(data){
751 ok( false, "Ajax error JSON (POST, data obj callback)" );
757 test("JSONP - Custom JSONP Callback", function() {
761 window.jsonpResults = function(data) {
762 ok( data.data, "JSON results returned (GET, custom callback function)" );
767 url: "data/jsonp.php",
769 jsonpCallback: "jsonpResults"
773 test("jQuery.ajax() - JSONP, Remote", function() {
777 function plus(){ if ( ++count == 4 ) start(); }
779 var base = window.location.href.replace(/\?.*$/, "");
784 url: base + "data/jsonp.php",
786 success: function(data){
787 ok( data.data, "JSON results returned (GET, no callback)" );
790 error: function(data){
791 ok( false, "Ajax error JSON (GET, no callback)" );
797 url: base + "data/jsonp.php?callback=?",
799 success: function(data){
800 ok( data.data, "JSON results returned (GET, url callback)" );
803 error: function(data){
804 ok( false, "Ajax error JSON (GET, url callback)" );
810 url: base + "data/jsonp.php",
813 success: function(data){
814 ok( data.data, "JSON results returned (GET, data callback)" );
817 error: function(data){
818 ok( false, "Ajax error JSON (GET, data callback)" );
824 url: base + "data/jsonp.php",
827 success: function(data){
828 ok( data.data, "JSON results returned (GET, data obj callback)" );
831 error: function(data){
832 ok( false, "Ajax error JSON (GET, data obj callback)" );
838 test("jQuery.ajax() - script, Remote", function() {
841 var base = window.location.href.replace(/\?.*$/, "");
846 url: base + "data/test.js",
848 success: function(data){
849 ok( foobar, "Script results returned (GET, no callback)" );
855 test("jQuery.ajax() - script, Remote with POST", function() {
858 var base = window.location.href.replace(/\?.*$/, "");
863 url: base + "data/test.js",
866 success: function(data, status){
867 ok( foobar, "Script results returned (POST, no callback)" );
868 equals( status, "success", "Script results returned (POST, no callback)" );
871 error: function(xhr) {
872 ok( false, "ajax error, status code: " + xhr.status );
878 test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
881 var base = window.location.href.replace(/\?.*$/, "");
882 base = base.replace(/^.*?\/\//, "//");
887 url: base + "data/test.js",
889 success: function(data){
890 ok( foobar, "Script results returned (GET, no callback)" );
896 test("jQuery.ajax() - malformed JSON", function() {
902 url: "data/badjson.js",
905 ok( false, "Success." );
908 error: function(xhr, msg, detailedMsg) {
909 equals( "parsererror", msg, "A parse error occurred." );
910 ok( /^Invalid JSON/.test(detailedMsg), "Detailed parsererror message provided" );
916 test("jQuery.ajax() - script by content-type", function() {
922 url: "data/script.php",
923 data: { header: "script" },
924 success: function() {
930 test("jQuery.ajax() - json by content-type", function() {
936 url: "data/json.php",
937 data: { header: "json", json: "array" },
938 success: function( json ) {
939 ok( json.length >= 2, "Check length");
940 equals( json[0].name, 'John', 'Check JSON: first, name' );
941 equals( json[0].age, 21, 'Check JSON: first, age' );
942 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
943 equals( json[1].age, 25, 'Check JSON: second, age' );
949 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
952 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
953 ok( json.length >= 2, "Check length");
954 equals( json[0].name, 'John', 'Check JSON: first, name' );
955 equals( json[0].age, 21, 'Check JSON: first, age' );
956 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
957 equals( json[1].age, 25, 'Check JSON: second, age' );
962 test("jQuery.getJSON(String, Function) - JSON object", function() {
965 jQuery.getJSON(url("data/json.php"), function(json) {
966 if (json && json.data) {
967 equals( json.data.lang, 'en', 'Check JSON: lang' );
968 equals( json.data.length, 25, 'Check JSON: length' );
974 test("jQuery.getJSON - Using Native JSON", function() {
977 var old = window.JSON;
979 parse: function(str){
980 ok( true, "Verifying that parse method was run" );
986 jQuery.getJSON(url("data/json.php"), function(json) {
988 equals( json, true, "Verifying return value" );
993 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
996 var base = window.location.href.replace(/\?.*$/, "");
999 jQuery.getJSON(url(base + "data/json.php"), function(json) {
1000 equals( json.data.lang, 'en', 'Check JSON: lang' );
1001 equals( json.data.length, 25, 'Check JSON: length' );
1006 test("jQuery.post - data", function() {
1010 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1011 jQuery('math', xml).each(function() {
1012 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1013 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1019 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1024 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1025 jQuery('math', xml).each(function() {
1026 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1027 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1029 if ( ++done === 2 ) start();
1032 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1033 jQuery('math', xml).each(function() {
1034 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1035 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1037 if ( ++done === 2 ) start();
1041 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1046 jQuery.ajaxSetup({timeout: 1000});
1048 var pass = function() {
1050 if ( passed == 2 ) {
1051 ok( true, 'Check local and global callbacks after timeout' );
1052 jQuery('#main').unbind("ajaxError");
1057 var fail = function(a,b,c) {
1058 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1062 jQuery('#main').ajaxError(pass);
1066 url: url("data/name.php?wait=5"),
1072 jQuery.ajaxSetup({timeout: 0});
1075 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1077 jQuery.ajaxSetup({timeout: 50});
1082 url: url("data/name.php?wait=1"),
1084 ok( false, 'Check for local timeout failed' );
1087 success: function() {
1088 ok( true, 'Check for local timeout' );
1094 jQuery.ajaxSetup({timeout: 0});
1097 test("jQuery.ajax - simple get", function() {
1102 url: url("data/name.php?name=foo"),
1103 success: function(msg){
1104 equals( msg, 'bar', 'Check for GET' );
1110 test("jQuery.ajax - simple post", function() {
1115 url: url("data/name.php"),
1117 success: function(msg){
1118 equals( msg, 'pan', 'Check for POST' );
1124 test("ajaxSetup()", function() {
1128 url: url("data/name.php?name=foo"),
1129 success: function(msg){
1130 equals( msg, 'bar', 'Check for GET' );
1138 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1141 url: "data/name.php?wait=1",
1143 error: function(request, status) {
1144 ok( status != null, "status shouldn't be null in error handler" );
1145 equals( "timeout", status );
1152 test("data option: evaluate function values (#2806)", function() {
1155 url: "data/echoQuery.php",
1161 success: function(result) {
1162 equals( result, "key=value" );
1168 test("jQuery.ajax - If-Modified-Since support", function() {
1173 var url = "data/if_modified_since.php?ts=" + new Date();
1178 success: function(data, status) {
1179 equals(status, "success");
1184 success: function(data, status) {
1185 if ( data === "FAIL" ) {
1186 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1187 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1189 equals(status, "notmodified");
1190 ok(data == null, "response body should be empty")
1199 test("jQuery.ajax - Etag support", function() {
1204 var url = "data/etag.php?ts=" + new Date();
1209 success: function(data, status) {
1210 equals(status, "success");
1215 success: function(data, status) {
1216 if ( data === "FAIL" ) {
1217 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1218 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1220 equals(status, "notmodified");
1221 ok(data == null, "response body should be empty")