📄 ajax.js
字号:
});test("$.ajax() - JSONP, Local", function() { expect(7); var count = 0; function plus(){ if ( ++count == 7 ) start(); } stop(); $.ajax({ url: "data/jsonp.php", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (GET, no callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, no callback)" ); plus(); } }); $.ajax({ url: "data/jsonp.php?callback=?", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (GET, url callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, url callback)" ); plus(); } }); $.ajax({ url: "data/jsonp.php", dataType: "jsonp", data: "callback=?", success: function(data){ ok( data.data, "JSON results returned (GET, data callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, data callback)" ); plus(); } }); $.ajax({ url: "data/jsonp.php", dataType: "jsonp", jsonp: "callback", success: function(data){ ok( data.data, "JSON results returned (GET, data obj callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, data obj callback)" ); plus(); } }); $.ajax({ type: "POST", url: "data/jsonp.php", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (POST, no callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, data obj callback)" ); plus(); } }); $.ajax({ type: "POST", url: "data/jsonp.php", data: "callback=?", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (POST, data callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (POST, data callback)" ); plus(); } }); $.ajax({ type: "POST", url: "data/jsonp.php", jsonp: "callback", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (POST, data obj callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (POST, data obj callback)" ); plus(); } });});test("$.ajax() - JSONP, Remote", function() { expect(4); var count = 0; function plus(){ if ( ++count == 4 ) start(); } var base = window.location.href.replace(/\?.*$/, ""); stop(); $.ajax({ url: base + "data/jsonp.php", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (GET, no callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, no callback)" ); plus(); } }); $.ajax({ url: base + "data/jsonp.php?callback=?", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (GET, url callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, url callback)" ); plus(); } }); $.ajax({ url: base + "data/jsonp.php", dataType: "jsonp", data: "callback=?", success: function(data){ ok( data.data, "JSON results returned (GET, data callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, data callback)" ); plus(); } }); $.ajax({ url: base + "data/jsonp.php", dataType: "jsonp", jsonp: "callback", success: function(data){ ok( data.data, "JSON results returned (GET, data obj callback)" ); plus(); }, error: function(data){ ok( false, "Ajax error JSON (GET, data obj callback)" ); plus(); } });});test("$.ajax() - script, Remote", function() { expect(2); var base = window.location.href.replace(/\?.*$/, ""); stop(); window.foobar = null; $.ajax({ url: base + "data/test.js", dataType: "script", success: function(data){ ok( foobar, "Script results returned (GET, no callback)" ); start(); } });});test("$.ajax() - script, Remote with POST", function() { expect(3); var base = window.location.href.replace(/\?.*$/, ""); stop(); window.foobar = null; $.ajax({ url: base + "data/test.js", type: "POST", dataType: "script", success: function(data, status){ ok( foobar, "Script results returned (GET, no callback)" ); equals( status, "success", "Script results returned (GET, no callback)" ); start(); } });});test("$.ajax() - script, Remote with scheme-less URL", function() { expect(2); var base = window.location.href.replace(/\?.*$/, ""); base = base.replace(/^.*?\/\//, "//"); stop(); window.foobar = null; $.ajax({ url: base + "data/test.js", dataType: "script", success: function(data){ ok( foobar, "Script results returned (GET, no callback)" ); start(); } });});test("$.getJSON(String, Hash, Function) - JSON array", function() { expect(4); stop(); $.getJSON(url("data/json.php"), {json: "array"}, function(json) { equals( json[0].name, 'John', 'Check JSON: first, name' ); equals( json[0].age, 21, 'Check JSON: first, age' ); equals( json[1].name, 'Peter', 'Check JSON: second, name' ); equals( json[1].age, 25, 'Check JSON: second, age' ); start(); });});test("$.getJSON(String, Function) - JSON object", function() { expect(2); stop(); $.getJSON(url("data/json.php"), function(json) { equals( json.data.lang, 'en', 'Check JSON: lang' ); equals( json.data.length, 25, 'Check JSON: length' ); start(); });});test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() { expect(2); var base = window.location.href.replace(/\?.*$/, ""); stop(); $.getJSON(url(base + "data/json.php"), function(json) { equals( json.data.lang, 'en', 'Check JSON: lang' ); equals( json.data.length, 25, 'Check JSON: length' ); start(); });});test("$.post(String, Hash, Function) - simple with xml", function() { expect(4); stop(); $.post(url("data/name.php"), {xml: "5-2"}, function(xml){ $('math', xml).each(function() { equals( $('calculation', this).text(), '5-2', 'Check for XML' ); equals( $('result', this).text(), '3', 'Check for XML' ); }); }); $.post(url("data/name.php?xml=5-2"), {}, function(xml){ $('math', xml).each(function() { equals( $('calculation', this).text(), '5-2', 'Check for XML' ); equals( $('result', this).text(), '3', 'Check for XML' ); }); start(); });});test("$.ajaxSetup({timeout: Number}) - with global timeout", function() { stop(); var passed = 0; $.ajaxSetup({timeout: 1000}); var pass = function() { passed++; if ( passed == 2 ) { ok( true, 'Check local and global callbacks after timeout' ); $('#main').unbind("ajaxError"); start(); } }; var fail = function(a,b,c) { ok( false, 'Check for timeout failed ' + a + ' ' + b ); start(); }; $('#main').ajaxError(pass); $.ajax({ type: "GET", url: url("data/name.php?wait=5"), error: pass, success: fail }); // reset timeout $.ajaxSetup({timeout: 0});});test("$.ajaxSetup({timeout: Number}) with localtimeout", function() { stop(); $.ajaxSetup({timeout: 50}); $.ajax({ type: "GET", timeout: 5000, url: url("data/name.php?wait=1"), error: function() { ok( false, 'Check for local timeout failed' ); start(); }, success: function() { ok( true, 'Check for local timeout' ); start(); } }); // reset timeout $.ajaxSetup({timeout: 0});});test("$.ajax - simple get", function() { expect(1); stop(); $.ajax({ type: "GET", url: url("data/name.php?name=foo"), success: function(msg){ equals( msg, 'bar', 'Check for GET' ); start(); } });});test("$.ajax - simple post", function() { expect(1); stop(); $.ajax({ type: "POST", url: url("data/name.php"), data: "name=peter", success: function(msg){ equals( msg, 'pan', 'Check for POST' ); start(); } });});test("ajaxSetup()", function() { expect(1); stop(); $.ajaxSetup({ url: url("data/name.php?name=foo"), success: function(msg){ equals( msg, 'bar', 'Check for GET' ); start(); } }); $.ajax();});test("custom timeout does not set error message when timeout occurs, see #970", function() { stop(); $.ajax({ url: "data/name.php?wait=10", timeout: 500, error: function(request, status) { ok( status != null, "status shouldn't be null in error handler" ); equals( "timeout", status ); start(); } });});test("data option: evaluate function values (#2806)", function() { stop(); $.ajax({ url: "data/echoQuery.php", data: { key: function() { return "value"; } }, success: function(result) { equals( result, "key=value" ); start(); } })});}//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -