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 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) {
907 equals( "parsererror", msg, "A parse error occurred." );
913 test("jQuery.ajax() - script by content-type", function() {
919 url: "data/script.php",
920 data: { header: "script" },
921 success: function() {
927 test("jQuery.ajax() - json by content-type", function() {
933 url: "data/json.php",
934 data: { header: "json", json: "array" },
935 success: function( json ) {
936 ok( json.length >= 2, "Check length");
937 equals( json[0].name, 'John', 'Check JSON: first, name' );
938 equals( json[0].age, 21, 'Check JSON: first, age' );
939 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
940 equals( json[1].age, 25, 'Check JSON: second, age' );
946 test("jQuery.getJSON(String, Hash, Function) - JSON array", function() {
949 jQuery.getJSON(url("data/json.php"), {json: "array"}, function(json) {
950 ok( json.length >= 2, "Check length");
951 equals( json[0].name, 'John', 'Check JSON: first, name' );
952 equals( json[0].age, 21, 'Check JSON: first, age' );
953 equals( json[1].name, 'Peter', 'Check JSON: second, name' );
954 equals( json[1].age, 25, 'Check JSON: second, age' );
959 test("jQuery.getJSON(String, Function) - JSON object", function() {
962 jQuery.getJSON(url("data/json.php"), function(json) {
963 if (json && json.data) {
964 equals( json.data.lang, 'en', 'Check JSON: lang' );
965 equals( json.data.length, 25, 'Check JSON: length' );
971 test("jQuery.getJSON - Using Native JSON", function() {
974 var old = window.JSON;
976 parse: function(str){
977 ok( true, "Verifying that parse method was run" );
983 jQuery.getJSON(url("data/json.php"), function(json) {
985 equals( json, true, "Verifying return value" );
990 test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
993 var base = window.location.href.replace(/\?.*$/, "");
996 jQuery.getJSON(url(base + "data/json.php"), function(json) {
997 equals( json.data.lang, 'en', 'Check JSON: lang' );
998 equals( json.data.length, 25, 'Check JSON: length' );
1003 test("jQuery.post - data", function() {
1007 jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
1008 jQuery('math', xml).each(function() {
1009 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1010 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1016 test("jQuery.post(String, Hash, Function) - simple with xml", function() {
1021 jQuery.post(url("data/name.php"), {xml: "5-2"}, function(xml){
1022 jQuery('math', xml).each(function() {
1023 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1024 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1026 if ( ++done === 2 ) start();
1029 jQuery.post(url("data/name.php?xml=5-2"), {}, function(xml){
1030 jQuery('math', xml).each(function() {
1031 equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
1032 equals( jQuery('result', this).text(), '3', 'Check for XML' );
1034 if ( ++done === 2 ) start();
1038 test("jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() {
1043 jQuery.ajaxSetup({timeout: 1000});
1045 var pass = function() {
1047 if ( passed == 2 ) {
1048 ok( true, 'Check local and global callbacks after timeout' );
1049 jQuery('#main').unbind("ajaxError");
1054 var fail = function(a,b,c) {
1055 ok( false, 'Check for timeout failed ' + a + ' ' + b );
1059 jQuery('#main').ajaxError(pass);
1063 url: url("data/name.php?wait=5"),
1069 jQuery.ajaxSetup({timeout: 0});
1072 test("jQuery.ajaxSetup({timeout: Number}) with localtimeout", function() {
1074 jQuery.ajaxSetup({timeout: 50});
1079 url: url("data/name.php?wait=1"),
1081 ok( false, 'Check for local timeout failed' );
1084 success: function() {
1085 ok( true, 'Check for local timeout' );
1091 jQuery.ajaxSetup({timeout: 0});
1094 test("jQuery.ajax - simple get", function() {
1099 url: url("data/name.php?name=foo"),
1100 success: function(msg){
1101 equals( msg, 'bar', 'Check for GET' );
1107 test("jQuery.ajax - simple post", function() {
1112 url: url("data/name.php"),
1114 success: function(msg){
1115 equals( msg, 'pan', 'Check for POST' );
1121 test("ajaxSetup()", function() {
1125 url: url("data/name.php?name=foo"),
1126 success: function(msg){
1127 equals( msg, 'bar', 'Check for GET' );
1135 test("custom timeout does not set error message when timeout occurs, see #970", function() {
1138 url: "data/name.php?wait=1",
1140 error: function(request, status) {
1141 ok( status != null, "status shouldn't be null in error handler" );
1142 equals( "timeout", status );
1149 test("data option: evaluate function values (#2806)", function() {
1152 url: "data/echoQuery.php",
1158 success: function(result) {
1159 equals( result, "key=value" );
1165 test("jQuery.ajax - If-Modified-Since support", function() {
1170 var url = "data/if_modified_since.php?ts=" + new Date();
1175 success: function(data, status) {
1176 equals(status, "success");
1181 success: function(data, status) {
1182 if ( data === "FAIL" ) {
1183 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1184 ok(true, "Opera is incapable of doing .setRequestHeader('If-Modified-Since').");
1186 equals(status, "notmodified");
1187 ok(data == null, "response body should be empty")
1196 test("jQuery.ajax - Etag support", function() {
1201 var url = "data/etag.php?ts=" + new Date();
1206 success: function(data, status) {
1207 equals(status, "success");
1212 success: function(data, status) {
1213 if ( data === "FAIL" ) {
1214 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1215 ok(true, "Opera is incapable of doing .setRequestHeader('If-None-Match').");
1217 equals(status, "notmodified");
1218 ok(data == null, "response body should be empty")