Hàm gọi lại được thực thi nếu yêu cầu thành công. Bắt buộc nếu cung cấp
1, nhưng có thể là
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
2 trong trường hợp đó
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
loại dữ liệu
Loại.
Loại dữ liệu mong đợi từ máy chủ. Vỡ nợ. Đoán thông minh [xml, json, tập lệnh, văn bản, html]
phiên bản thêm. 1. 12 và 2. 2
cài đặt
Loại.
Một tập hợp các cặp khóa/giá trị cấu hình yêu cầu Ajax. Tất cả các thuộc tính ngoại trừ
3 là tùy chọn. Có thể đặt mặc định cho bất kỳ tùy chọn nào với $. cài đặt ajax[]. Xem danh sách đầy đủ tất cả các cài đặt. Loại sẽ tự động được đặt thành$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
4$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
Đây là một hàm Ajax tốc ký, tương đương với
1
2
3
4
5
6
7
$.ajax[{
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
}];
Hàm gọi lại
5 được truyền dữ liệu trả về, dữ liệu này sẽ là phần tử gốc XML hoặc chuỗi văn bản tùy thuộc vào loại MIME của phản hồi. Nó cũng được chuyển trạng thái văn bản của phản hồi
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
Kể từ jQuery 1. 5, hàm gọi lại
5 cũng được chuyển a [trong jQuery 1. 4, nó đã được thông qua đối tượng
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
7]
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
Hầu hết các triển khai sẽ chỉ định trình xử lý thành công
1
2
3
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
Ví dụ này tìm nạp đoạn mã HTML được yêu cầu và chèn đoạn mã đó vào trang
Các trang được tìm nạp bằng
4 không bao giờ được lưu trong bộ nhớ cache, vì vậy các tùy chọn
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
9 và
$.post[ "ajax/test.html", function[ data ] {
$[ ".result" ].html[ data ];
}];
0 trong
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
1 không có hiệu lực đối với các yêu cầu này
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
Đối tượng jqXHR
Kể từ jQuery 1. 5, tất cả các phương thức Ajax của jQuery đều trả về một siêu đối tượng của đối tượng
2. Đối tượng jQuery XHR này, hay "jqXHR," được trả về bởi
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
3 triển khai giao diện Promise, cung cấp cho nó tất cả các thuộc tính, phương thức và hành vi của một Promise [xem Đối tượng bị trì hoãn để biết thêm thông tin].
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
4 [đối với thành công],
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
5 [đối với lỗi] và
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
6 [đối với hoàn thành, cho dù thành công hay lỗi; được thêm vào trong jQuery 1. 6] các phương thức lấy một đối số hàm được gọi khi yêu cầu kết thúc. Để biết thông tin về các đối số mà hàm này nhận được, hãy xem phần của tài liệu
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
7
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
Giao diện Promise cũng cho phép các phương thức Ajax của jQuery, bao gồm
8, xâu chuỗi nhiều lệnh gọi lại
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
9,
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
0 và
$.post[ "test.php" ];
1 trên một yêu cầu và thậm chí chỉ định các lệnh gọi lại này sau khi yêu cầu có thể đã hoàn thành. Nếu yêu cầu đã hoàn thành, cuộc gọi lại sẽ được kích hoạt ngay lập tức
$.post[ "test.php" ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
Thông báo Khấu hao
Các phương thức gọi lại
2,
$.post[ "test.php" ];
3 và
$.post[ "test.php" ];
4 đã bị xóa kể từ jQuery 3. 0. Bạn có thể sử dụng
$.post[ "test.php" ];
4,
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
5 và
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
6 để thay thế
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post[ "example.php", function[] {
alert[ "success" ];
}]
.done[function[] {
alert[ "second success" ];
}]
.fail[function[] {
alert[ "error" ];
}]
.always[function[] {
alert[ "finished" ];
}];
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always[function[] {
alert[ "second finished" ];
}];
Ghi chú bổ sung
- Do các hạn chế về bảo mật của trình duyệt, hầu hết các yêu cầu "Ajax" đều phải tuân theo cùng một chính sách gốc;
- Nếu một yêu cầu với jQuery. post[] trả về mã lỗi, nó sẽ bị lỗi âm thầm trừ khi tập lệnh cũng được gọi là toàn cầu. phương thức ajaxError[]. Ngoài ra, kể từ jQuery 1. 5, phương thức
8 của đối tượng$.post[ "test.php" ];
9 do jQuery trả về. post[] cũng có sẵn để xử lý lỗi$.post[ "test.php" ];
ví dụ
Yêu cầu kiểm tra. trang php, nhưng bỏ qua kết quả trả về
1
$.post[ "test.php" ];
Yêu cầu kiểm tra. php và gửi một số dữ liệu bổ sung [trong khi vẫn bỏ qua kết quả trả về]
1
$.post[ "test.php", { name: "John", time: "2pm" } ];
Truyền mảng dữ liệu đến máy chủ [trong khi vẫn bỏ qua kết quả trả về]
1
$.post[ "test.php", { 'choices[]': [ "Jon", "Susan" ] } ];
Gửi dữ liệu biểu mẫu bằng các yêu cầu Ajax
1
$.post[ "test.php", $[ "#testform" ].serialize[] ];
Thông báo kết quả từ yêu cầu kiểm tra. php [HTML hoặc XML, tùy thuộc vào nội dung được trả về]
1
2
3
$.post[ "test.php", function[ data ] {
alert[ "Data Loaded: " + data ];
}];
Thông báo kết quả từ yêu cầu kiểm tra. php với tải trọng dữ liệu bổ sung [HTML hoặc XML, tùy thuộc vào nội dung được trả về]
1
2
3
4
$.post[ "test.php", { name: "John", time: "2pm" }]
.done[function[ data ] {
alert[ "Data Loaded: " + data ];
}];
Đăng bài kiểm tra. php và nhận nội dung đã được trả về ở định dạng json ["John","time"=>"2pm"]];