例子:
Example: 保存数据到服务器,成功时显示信息。
|
1
2
3
4
5
6
7
|
$.ajax({ method: "POST", url: "some.php", data: { name: "John", location: "Boston" }}).done(function( msg ) { alert( "Data Saved: " + msg );}); |
Example: 装入一个 HTML 网页最新版本。
|
1
2
3
4
5
6
|
$.ajax({ url: "test.html", cache: false}).done(function( html ) { $("#results").append(html);}); |
Example: 发送 XML 数据至服务器。设置 processData 选项为 false,防止自动转换数据格式。
|
1
2
3
4
5
6
7
8
|
var xmlDocument = [create xml document];var xmlRequest = $.ajax({ url: "page.php", processData: false, data: xmlDocument});xmlRequest.done(handleResponse); |
Example: 发送id作为数据发送到服务器, 保存一些数据到服务器上, 并通一旦它的完成知用户。 如果请求失败,则提醒用户。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var menuId = $("ul.nav").first().attr("id");var request = $.ajax({ url: "script.php", method: "POST", data: {id : menuId}, dataType: "html"});request.done(function(msg) { $("#log").html( msg );});request.fail(function(jqXHR, textStatus) { alert( "Request failed: " + textStatus );}); |
Example: 获取json数据
1 $.ajax({ 2 method: "GET", 3 url: "service.php", 4 dataType: "json", 5 success:function(){}, 6 error:function(){} 7 });
Example: 载入并执行一个JavaScript文件.
|
1
2
3
4
5
|
$.ajax({ method: "GET", url: "test.js", dataType: "script"}); |